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 HttpHeadRequestTest extends HttpServerTest { 46 47 @Override 48 public void setUp() throws Exception { 49 super.setUp(); 50 String responseBody = "Success!"; 51 this.testServer.response = NanoHTTPD.newFixedLengthResponse(responseBody); 52 } 53 54 @Test 55 public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { 56 invokeServer("HEAD " + HttpServerTest.URI + "?foo&bar= HTTP/1.1"); 57 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 58 assertEquals(0, this.testServer.decodedParamters.get("foo").size()); 59 assertTrue(this.testServer.decodedParamters.get("bar") instanceof List); 60 assertEquals(1, this.testServer.decodedParamters.get("bar").size()); 61 assertEquals("", this.testServer.decodedParamters.get("bar").get(0)); 62 } 63 64 @Test 65 public void testDecodingMixtureOfParameters() { 66 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 67 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 68 assertEquals(2, this.testServer.decodedParamters.get("foo").size()); 69 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 70 assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); 71 assertTrue(this.testServer.decodedParamters.get("zot") instanceof List); 72 assertEquals(0, this.testServer.decodedParamters.get("zot").size()); 73 assertTrue(this.testServer.decodedParamters.get("zim") instanceof List); 74 assertEquals(1, this.testServer.decodedParamters.get("zim").size()); 75 assertEquals("", this.testServer.decodedParamters.get("zim").get(0)); 76 } 77 78 @Test 79 public void testDecodingParametersFromParameterMap() { 80 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 81 assertEquals(this.testServer.decodedParamters, this.testServer.decodedParamtersFromParameter); 82 } 83 84 // -------------------------------------------------------------------------------------------------------- 85 // // 86 87 @Test 88 public void testDecodingParametersWithSingleValue() { 89 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); 90 assertEquals("foo=bar&baz=zot", this.testServer.queryParameterString); 91 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 92 assertEquals(1, this.testServer.decodedParamters.get("foo").size()); 93 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 94 assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); 95 assertEquals(1, this.testServer.decodedParamters.get("baz").size()); 96 assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); 97 } 98 99 @Test 100 public void testDecodingParametersWithSingleValueAndMissingValue() { 101 invokeServer("HEAD " + HttpServerTest.URI + "?foo&baz=zot HTTP/1.1"); 102 assertEquals("foo&baz=zot", this.testServer.queryParameterString); 103 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 104 assertEquals(0, this.testServer.decodedParamters.get("foo").size()); 105 assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); 106 assertEquals(1, this.testServer.decodedParamters.get("baz").size()); 107 assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); 108 } 109 110 @Test 111 public void testDecodingSingleFieldRepeated() { 112 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz HTTP/1.1"); 113 assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); 114 assertEquals(2, this.testServer.decodedParamters.get("foo").size()); 115 assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); 116 assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); 117 } 118 119 @Test 120 public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { 121 invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1"); 122 assertNotNull(this.testServer.parms); 123 assertNotNull(this.testServer.header); 124 assertNotNull(this.testServer.files); 125 assertNotNull(this.testServer.uri); 126 } 127 128 @Test 129 public void testHeadRequestDoesntSendBackResponseBody() throws Exception { 130 ByteArrayOutputStream outputStream = invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1"); 131 132 String[] expected = { 133 "HTTP/1.1 200 OK", 134 "Content-Type: text/html", 135 "Date: .*", 136 "Connection: keep-alive", 137 "Content-Length: 8", 138 "" 139 }; 140 141 assertResponse(outputStream, expected); 142 } 143 144 @Test 145 public void testMultipleGetParameters() { 146 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); 147 assertEquals("bar", this.testServer.parms.get("foo")); 148 assertEquals("zot", this.testServer.parms.get("baz")); 149 } 150 151 @Test 152 public void testMultipleGetParametersWithMissingValue() { 153 invokeServer("HEAD " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1"); 154 assertEquals("", this.testServer.parms.get("foo")); 155 assertEquals("zot", this.testServer.parms.get("baz")); 156 } 157 158 @Test 159 public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { 160 invokeServer("HEAD " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); 161 assertEquals("", this.testServer.parms.get("foo")); 162 assertEquals("zot", this.testServer.parms.get("baz")); 163 assertEquals("text/html", this.testServer.header.get("accept")); 164 } 165 166 @Test 167 public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 168 String userAgent = "jUnit 4.8.2 Unit Test"; 169 String accept = "text/html"; 170 invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); 171 assertEquals(userAgent, this.testServer.header.get("user-agent")); 172 assertEquals(accept, this.testServer.header.get("accept")); 173 } 174 175 @Test 176 public void testSingleGetParameter() { 177 invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar HTTP/1.1"); 178 assertEquals("bar", this.testServer.parms.get("foo")); 179 } 180 181 @Test 182 public void testSingleGetParameterWithNoValue() { 183 invokeServer("HEAD " + HttpServerTest.URI + "?foo HTTP/1.1"); 184 assertEquals("", this.testServer.parms.get("foo")); 185 } 186 187 @Test 188 public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 189 String userAgent = "jUnit 4.8.2 Unit Test"; 190 invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); 191 assertEquals(userAgent, this.testServer.header.get("user-agent")); 192 assertEquals(NanoHTTPD.Method.HEAD, this.testServer.method); 193 assertEquals(HttpServerTest.URI, this.testServer.uri); 194 } 195 196 } 197