1 package fi.iki.elonen; 2 3 import org.junit.Test; 4 5 import java.io.ByteArrayOutputStream; 6 import java.util.List; 7 8 import static junit.framework.Assert.*; 9 10 public class HttpGetRequestTest extends HttpServerTest { 11 12 @Test 13 public void testFullyQualifiedWorkingGetRequest() throws Exception { 14 ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1"); 15 16 String[] expected = { 17 "HTTP/1.1 200 OK", 18 "Content-Type: text/html", 19 "Date: .*", 20 "Connection: keep-alive", 21 "Content-Length: 0", 22 "" 23 }; 24 25 assertResponse(outputStream, expected); 26 } 27 28 @Test 29 public void testOutputOfServeSentBackToClient() throws Exception { 30 String responseBody = "Success!"; 31 testServer.response = new NanoHTTPD.Response(responseBody); 32 ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1"); 33 34 String[] expected = { 35 "HTTP/1.1 200 OK", 36 "Content-Type: text/html", 37 "Date: .*", 38 "Connection: keep-alive", 39 "Content-Length: 8", 40 "", 41 responseBody 42 }; 43 44 assertResponse(outputStream, expected); 45 } 46 47 @Test 48 public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { 49 invokeServer("GET " + URI + " HTTP/1.1"); 50 assertNotNull(testServer.parms); 51 assertNotNull(testServer.header); 52 assertNotNull(testServer.files); 53 assertNotNull(testServer.uri); 54 } 55 56 @Test 57 public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 58 String userAgent = "jUnit 4.8.2 Unit Test"; 59 invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); 60 assertEquals(userAgent, testServer.header.get("user-agent")); 61 assertEquals(NanoHTTPD.Method.GET, testServer.method); 62 assertEquals(URI, testServer.uri); 63 } 64 65 @Test 66 public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { 67 String userAgent = "jUnit 4.8.2 Unit Test"; 68 String accept = "text/html"; 69 invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); 70 assertEquals(userAgent, testServer.header.get("user-agent")); 71 assertEquals(accept, testServer.header.get("accept")); 72 } 73 74 @Test 75 public void testSingleGetParameter() { 76 invokeServer("GET " + URI + "?foo=bar HTTP/1.1"); 77 assertEquals("bar", testServer.parms.get("foo")); 78 } 79 80 @Test 81 public void testSingleGetParameterWithNoValue() { 82 invokeServer("GET " + URI + "?foo HTTP/1.1"); 83 assertEquals("", testServer.parms.get("foo")); 84 } 85 86 @Test 87 public void testMultipleGetParameters() { 88 invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1"); 89 assertEquals("bar", testServer.parms.get("foo")); 90 assertEquals("zot", testServer.parms.get("baz")); 91 } 92 93 @Test 94 public void testMultipleGetParametersWithMissingValue() { 95 invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1"); 96 assertEquals("", testServer.parms.get("foo")); 97 assertEquals("zot", testServer.parms.get("baz")); 98 } 99 100 @Test 101 public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { 102 invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); 103 assertEquals("", testServer.parms.get("foo")); 104 assertEquals("zot", testServer.parms.get("baz")); 105 assertEquals("text/html", testServer.header.get("accept")); 106 } 107 108 @Test 109 public void testDecodingParametersWithSingleValue() { 110 invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1"); 111 assertEquals("foo=bar&baz=zot", testServer.queryParameterString); 112 assertTrue(testServer.decodedParamters.get("foo") instanceof List); 113 assertEquals(1, testServer.decodedParamters.get("foo").size()); 114 assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); 115 assertTrue(testServer.decodedParamters.get("baz") instanceof List); 116 assertEquals(1, testServer.decodedParamters.get("baz").size()); 117 assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); 118 } 119 120 @Test 121 public void testDecodingParametersWithSingleValueAndMissingValue() { 122 invokeServer("GET " + URI + "?foo&baz=zot HTTP/1.1"); 123 assertEquals("foo&baz=zot", testServer.queryParameterString); 124 assertTrue(testServer.decodedParamters.get("foo") instanceof List); 125 assertEquals(0, testServer.decodedParamters.get("foo").size()); 126 assertTrue(testServer.decodedParamters.get("baz") instanceof List); 127 assertEquals(1, testServer.decodedParamters.get("baz").size()); 128 assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); 129 } 130 131 @Test 132 public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { 133 invokeServer("GET " + URI + "?foo&bar= HTTP/1.1"); 134 assertTrue(testServer.decodedParamters.get("foo") instanceof List); 135 assertEquals(0, testServer.decodedParamters.get("foo").size()); 136 assertTrue(testServer.decodedParamters.get("bar") instanceof List); 137 assertEquals(1, testServer.decodedParamters.get("bar").size()); 138 assertEquals("", testServer.decodedParamters.get("bar").get(0)); 139 } 140 141 @Test 142 public void testDecodingSingleFieldRepeated() { 143 invokeServer("GET " + URI + "?foo=bar&foo=baz HTTP/1.1"); 144 assertTrue(testServer.decodedParamters.get("foo") instanceof List); 145 assertEquals(2, testServer.decodedParamters.get("foo").size()); 146 assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); 147 assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); 148 } 149 150 @Test 151 public void testDecodingMixtureOfParameters() { 152 invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 153 assertTrue(testServer.decodedParamters.get("foo") instanceof List); 154 assertEquals(2, testServer.decodedParamters.get("foo").size()); 155 assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); 156 assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); 157 assertTrue(testServer.decodedParamters.get("zot") instanceof List); 158 assertEquals(0, testServer.decodedParamters.get("zot").size()); 159 assertTrue(testServer.decodedParamters.get("zim") instanceof List); 160 assertEquals(1, testServer.decodedParamters.get("zim").size()); 161 assertEquals("", testServer.decodedParamters.get("zim").get(0)); 162 } 163 164 @Test 165 public void testDecodingParametersFromParameterMap() { 166 invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); 167 assertEquals(testServer.decodedParamters, testServer.decodedParamtersFromParameter); 168 } 169 // -------------------------------------------------------------------------------------------------------- // 170 171 } 172