1 package fi.iki.elonen; 2 3 /* 4 * #%L 5 * NanoHttpd-Core 6 * %% 7 * Copyright (C) 2012 - 2015 nanohttpd 8 * %% 9 * Redistribution and use in source and binary forms, with or without modification, 10 * are permitted provided that the following conditions are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright notice, this 13 * list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the nanohttpd nor the names of its contributors 20 * may be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32 * OF THE POSSIBILITY OF SUCH DAMAGE. 33 * #L% 34 */ 35 36 import static junit.framework.Assert.assertEquals; 37 import static junit.framework.Assert.assertNotNull; 38 import static junit.framework.Assert.assertTrue; 39 40 import java.io.ByteArrayOutputStream; 41 import java.util.List; 42 43 import org.junit.Test; 44 45 public class HttpGetRequestTest extends HttpServerTest { 46 47 @Test 48 public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { 49 invokeServer("GET " + HttpServerTest.URI + "?foo&bar= HTTP/1.1"); 50 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 51 assertEquals(0, this.testServer.decodedParamters.get("foo").size()); 52 assertTrue(this.testServer.decodedParamters.get("bar") instanceof List); 53 assertEquals(1, this.testServer.decodedParamters.get("bar").size()); 54 assertEquals("", this.testServer.decodedParamters.get("bar").get(0)); 55 } 56 57 @Test 58 public void testDecodingMixtureOfParameters() { 59 invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 60 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 61 assertEquals(2, this.testServer.decodedParamters.get("foo").size()); 62 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 63 assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); 64 assertTrue(this.testServer.decodedParamters.get("zot") instanceof List); 65 assertEquals(0, this.testServer.decodedParamters.get("zot").size()); 66 assertTrue(this.testServer.decodedParamters.get("zim") instanceof List); 67 assertEquals(1, this.testServer.decodedParamters.get("zim").size()); 68 assertEquals("", this.testServer.decodedParamters.get("zim").get(0)); 69 } 70 71 @Test 72 public void testDecodingParametersFromParameterMap() { 73 invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 74 assertEquals(this.testServer.decodedParamters, this.testServer.decodedParamtersFromParameter); 75 } 76 77 // -------------------------------------------------------------------------------------------------------- 78 // // 79 80 @Test 81 public void testDecodingParametersWithSingleValue() { 82 invokeServer("GET " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); 83 assertEquals("foo=bar&baz=zot", this.testServer.queryParameterString); 84 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 85 assertEquals(1, this.testServer.decodedParamters.get("foo").size()); 86 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 87 assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); 88 assertEquals(1, this.testServer.decodedParamters.get("baz").size()); 89 assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); 90 } 91 92 @Test 93 public void testDecodingParametersWithSingleValueAndMissingValue() { 94 invokeServer("GET " + HttpServerTest.URI + "?foo&baz=zot HTTP/1.1"); 95 assertEquals("foo&baz=zot", this.testServer.queryParameterString); 96 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 97 assertEquals(0, this.testServer.decodedParamters.get("foo").size()); 98 assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); 99 assertEquals(1, this.testServer.decodedParamters.get("baz").size()); 100 assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); 101 } 102 103 @Test 104 public void testDecodingSingleFieldRepeated() { 105 invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz HTTP/1.1"); 106 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 107 assertEquals(2, this.testServer.decodedParamters.get("foo").size()); 108 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 109 assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); 110 } 111 112 @Test 113 public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { 114 invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); 115 assertNotNull(this.testServer.parms); 116 assertNotNull(this.testServer.header); 117 assertNotNull(this.testServer.files); 118 assertNotNull(this.testServer.uri); 119 } 120 121 @Test 122 public void testFullyQualifiedWorkingGetRequest() throws Exception { 123 ByteArrayOutputStream outputStream = invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); 124 125 String[] expected = { 126 "HTTP/1.1 200 OK", 127 "Content-Type: text/html", 128 "Date: .*", 129 "Connection: keep-alive", 130 "Content-Length: 0", 131 "" 132 }; 133 134 assertResponse(outputStream, expected); 135 } 136 137 @Test 138 public void testMultipleGetParameters() { 139 invokeServer("GET " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); 140 assertEquals("bar", this.testServer.parms.get("foo")); 141 assertEquals("zot", this.testServer.parms.get("baz")); 142 } 143 144 @Test 145 public void testMultipleGetParametersWithMissingValue() { 146 invokeServer("GET " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1"); 147 assertEquals("", this.testServer.parms.get("foo")); 148 assertEquals("zot", this.testServer.parms.get("baz")); 149 } 150 151 @Test 152 public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { 153 invokeServer("GET " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); 154 assertEquals("", this.testServer.parms.get("foo")); 155 assertEquals("zot", this.testServer.parms.get("baz")); 156 assertEquals("text/html", this.testServer.header.get("accept")); 157 } 158 159 @Test 160 public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 161 String userAgent = "jUnit 4.8.2 Unit Test"; 162 String accept = "text/html"; 163 invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); 164 assertEquals(userAgent, this.testServer.header.get("user-agent")); 165 assertEquals(accept, this.testServer.header.get("accept")); 166 } 167 168 @Test 169 public void testOutputOfServeSentBackToClient() throws Exception { 170 String responseBody = "Success!"; 171 this.testServer.response = NanoHTTPD.newFixedLengthResponse(responseBody); 172 ByteArrayOutputStream outputStream = invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); 173 174 String[] expected = { 175 "HTTP/1.1 200 OK", 176 "Content-Type: text/html", 177 "Date: .*", 178 "Connection: keep-alive", 179 "Content-Length: 8", 180 "", 181 responseBody 182 }; 183 184 assertResponse(outputStream, expected); 185 } 186 187 @Test 188 public void testSingleGetParameter() { 189 invokeServer("GET " + HttpServerTest.URI + "?foo=bar HTTP/1.1"); 190 assertEquals("bar", this.testServer.parms.get("foo")); 191 } 192 193 @Test 194 public void testSingleGetParameterWithNoValue() { 195 invokeServer("GET " + HttpServerTest.URI + "?foo HTTP/1.1"); 196 assertEquals("", this.testServer.parms.get("foo")); 197 } 198 199 @Test 200 public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 201 String userAgent = "jUnit 4.8.2 Unit Test"; 202 invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); 203 assertEquals(userAgent, this.testServer.header.get("user-agent")); 204 assertEquals(NanoHTTPD.Method.GET, this.testServer.method); 205 assertEquals(HttpServerTest.URI, this.testServer.uri); 206 } 207 208 } 209