From 5e2e2f19e06b9f4a01c6da83a2297eb18fe2b546 Mon Sep 17 00:00:00 2001 From: ritchie Date: Thu, 14 May 2015 09:02:50 +0200 Subject: reponces now created by factorymethods that can be overwritten, solves #94 --- .../fi/iki/elonen/HttpChunkedResponseTest.java | 57 ++--- .../java/fi/iki/elonen/HttpDeleteRequestTest.java | 50 ++--- .../java/fi/iki/elonen/HttpGetRequestTest.java | 236 +++++++++++---------- .../java/fi/iki/elonen/HttpHeadRequestTest.java | 210 +++++++++--------- .../test/java/fi/iki/elonen/HttpKeepAliveTest.java | 29 +-- .../test/java/fi/iki/elonen/HttpParsingTest.java | 33 ++- .../java/fi/iki/elonen/HttpPostRequestTest.java | 173 +++++++-------- .../java/fi/iki/elonen/HttpPutRequestTest.java | 18 +- .../test/java/fi/iki/elonen/HttpServerTest.java | 196 +++++++++-------- .../java/fi/iki/elonen/HttpSessionHeadersTest.java | 18 +- .../java/fi/iki/elonen/InvalidRequestTest.java | 58 ++--- .../elonen/integration/CookieIntegrationTest.java | 106 ++++----- .../integration/GetAndPostIntegrationTest.java | 124 ++++++----- .../elonen/integration/IntegrationTestBase.java | 28 +-- .../integration/PutStreamIntegrationTest.java | 56 ++--- .../fi/iki/elonen/integration/ShutdownTest.java | 41 ++-- 16 files changed, 726 insertions(+), 707 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/fi/iki/elonen/HttpChunkedResponseTest.java b/core/src/test/java/fi/iki/elonen/HttpChunkedResponseTest.java index 779e43f..d740b18 100644 --- a/core/src/test/java/fi/iki/elonen/HttpChunkedResponseTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpChunkedResponseTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,14 +33,34 @@ package fi.iki.elonen; * #L% */ +import static fi.iki.elonen.NanoHTTPD.Response.Status.OK; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PipedInputStream; -import static fi.iki.elonen.NanoHTTPD.Response.Status.OK; - public class HttpChunkedResponseTest extends HttpServerTest { + private static class ChunkedInputStream extends PipedInputStream { + + int chunk = 0; + + String[] chunks; + + private ChunkedInputStream(String[] chunks) { + this.chunks = chunks; + } + + @Override + public synchronized int read(byte[] buffer) throws IOException { + // Too implementation-linked, but... + for (int i = 0; i < this.chunks[this.chunk].length(); ++i) { + buffer[i] = (byte) this.chunks[this.chunk].charAt(i); + } + return this.chunks[this.chunk++].length(); + } + } + @org.junit.Test public void thatChunkedContentIsChunked() throws Exception { PipedInputStream pipedInputStream = new ChunkedInputStream(new String[]{ @@ -65,31 +85,12 @@ public class HttpChunkedResponseTest extends HttpServerTest { "0", "" }; - testServer.response = new NanoHTTPD.Response(OK, "what/ever", pipedInputStream); - testServer.response.setChunkedTransfer(true); + this.testServer.response = new NanoHTTPD(0) { + }.newChunkedResponse(OK, "what/ever", pipedInputStream); + this.testServer.response.setChunkedTransfer(true); ByteArrayOutputStream byteArrayOutputStream = invokeServer("GET / HTTP/1.0"); assertResponse(byteArrayOutputStream, expected); } - - private static class ChunkedInputStream extends PipedInputStream { - - int chunk = 0; - - String[] chunks; - - private ChunkedInputStream(String[] chunks) { - this.chunks = chunks; - } - - @Override - public synchronized int read(byte[] buffer) throws IOException { - // Too implementation-linked, but... - for (int i = 0; i < chunks[chunk].length(); ++i) { - buffer[i] = (byte) chunks[chunk].charAt(i); - } - return chunks[chunk++].length(); - } - } } diff --git a/core/src/test/java/fi/iki/elonen/HttpDeleteRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpDeleteRequestTest.java index 4756b1d..00b8953 100644 --- a/core/src/test/java/fi/iki/elonen/HttpDeleteRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpDeleteRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,21 +33,19 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; - import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.util.List; -import static junit.framework.Assert.*; +import org.junit.Test; public class HttpDeleteRequestTest extends HttpServerTest { @Test public void testDeleteRequestThatDoesntSendBackResponseBody_EmptyString() throws Exception { - testServer.response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, ""); + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, ""); - ByteArrayOutputStream outputStream = invokeServer("DELETE " + URI + " HTTP/1.1"); + ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1"); String[] expected = { "HTTP/1.1 204 No Content", @@ -62,10 +60,11 @@ public class HttpDeleteRequestTest extends HttpServerTest { } @Test - public void testDeleteRequestThatDoesntSendBackResponseBody_NullString() throws Exception { - testServer.response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (String) null); + public void testDeleteRequestThatDoesntSendBackResponseBody_NullInputStream() throws Exception { + this.testServer.response = new NanoHTTPD(0) { + }.newChunkedResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (InputStream) null); - ByteArrayOutputStream outputStream = invokeServer("DELETE " + URI + " HTTP/1.1"); + ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1"); String[] expected = { "HTTP/1.1 204 No Content", @@ -80,10 +79,11 @@ public class HttpDeleteRequestTest extends HttpServerTest { } @Test - public void testDeleteRequestThatDoesntSendBackResponseBody_NullInputStream() throws Exception { - testServer.response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (InputStream) null); + public void testDeleteRequestThatDoesntSendBackResponseBody_NullString() throws Exception { + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(NanoHTTPD.Response.Status.NO_CONTENT, NanoHTTPD.MIME_HTML, (String) null); - ByteArrayOutputStream outputStream = invokeServer("DELETE " + URI + " HTTP/1.1"); + ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1"); String[] expected = { "HTTP/1.1 204 No Content", @@ -98,13 +98,14 @@ public class HttpDeleteRequestTest extends HttpServerTest { } @Test - public void testDeleteRequestThatSendsBackResponseBody_Success() throws Exception { - testServer.response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "application/xml", ""); + public void testDeleteRequestThatSendsBackResponseBody_Accepted() throws Exception { + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(NanoHTTPD.Response.Status.ACCEPTED, "application/xml", ""); - ByteArrayOutputStream outputStream = invokeServer("DELETE " + URI + " HTTP/1.1"); + ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1"); String[] expected = { - "HTTP/1.1 200 OK", + "HTTP/1.1 202 Accepted", "Content-Type: application/xml", "Date: .*", "Connection: keep-alive", @@ -117,13 +118,14 @@ public class HttpDeleteRequestTest extends HttpServerTest { } @Test - public void testDeleteRequestThatSendsBackResponseBody_Accepted() throws Exception { - testServer.response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.ACCEPTED, "application/xml", ""); + public void testDeleteRequestThatSendsBackResponseBody_Success() throws Exception { + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "application/xml", ""); - ByteArrayOutputStream outputStream = invokeServer("DELETE " + URI + " HTTP/1.1"); + ByteArrayOutputStream outputStream = invokeServer("DELETE " + HttpServerTest.URI + " HTTP/1.1"); String[] expected = { - "HTTP/1.1 202 Accepted", + "HTTP/1.1 200 OK", "Content-Type: application/xml", "Date: .*", "Connection: keep-alive", diff --git a/core/src/test/java/fi/iki/elonen/HttpGetRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpGetRequestTest.java index 9980d2e..0566502 100644 --- a/core/src/test/java/fi/iki/elonen/HttpGetRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpGetRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,173 +33,177 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.util.List; -import static junit.framework.Assert.*; +import org.junit.Test; public class HttpGetRequestTest extends HttpServerTest { @Test - public void testFullyQualifiedWorkingGetRequest() throws Exception { - ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1"); - - String[] expected = { - "HTTP/1.1 200 OK", - "Content-Type: text/html", - "Date: .*", - "Connection: keep-alive", - "Content-Length: 0", - "" - }; - - assertResponse(outputStream, expected); + public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { + invokeServer("GET " + HttpServerTest.URI + "?foo&bar= HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("foo").size()); + assertTrue(this.testServer.decodedParamters.get("bar") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("bar").size()); + assertEquals("", this.testServer.decodedParamters.get("bar").get(0)); } @Test - public void testOutputOfServeSentBackToClient() throws Exception { - String responseBody = "Success!"; - testServer.response = new NanoHTTPD.Response(responseBody); - ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1"); - - String[] expected = { - "HTTP/1.1 200 OK", - "Content-Type: text/html", - "Date: .*", - "Connection: keep-alive", - "Content-Length: 8", - "", - responseBody - }; + public void testDecodingMixtureOfParameters() { + invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(2, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); + assertTrue(this.testServer.decodedParamters.get("zot") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("zot").size()); + assertTrue(this.testServer.decodedParamters.get("zim") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("zim").size()); + assertEquals("", this.testServer.decodedParamters.get("zim").get(0)); + } - assertResponse(outputStream, expected); + @Test + public void testDecodingParametersFromParameterMap() { + invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); + assertEquals(this.testServer.decodedParamters, this.testServer.decodedParamtersFromParameter); } + // -------------------------------------------------------------------------------------------------------- + // // + @Test - public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { - invokeServer("GET " + URI + " HTTP/1.1"); - assertNotNull(testServer.parms); - assertNotNull(testServer.header); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + public void testDecodingParametersWithSingleValue() { + invokeServer("GET " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); + assertEquals("foo=bar&baz=zot", this.testServer.queryParameterString); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("baz").size()); + assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); } @Test - public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { - String userAgent = "jUnit 4.8.2 Unit Test"; - invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); - assertEquals(userAgent, testServer.header.get("user-agent")); - assertEquals(NanoHTTPD.Method.GET, testServer.method); - assertEquals(URI, testServer.uri); + public void testDecodingParametersWithSingleValueAndMissingValue() { + invokeServer("GET " + HttpServerTest.URI + "?foo&baz=zot HTTP/1.1"); + assertEquals("foo&baz=zot", this.testServer.queryParameterString); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("foo").size()); + assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("baz").size()); + assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); } @Test - public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { - String userAgent = "jUnit 4.8.2 Unit Test"; - String accept = "text/html"; - invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); - assertEquals(userAgent, testServer.header.get("user-agent")); - assertEquals(accept, testServer.header.get("accept")); + public void testDecodingSingleFieldRepeated() { + invokeServer("GET " + HttpServerTest.URI + "?foo=bar&foo=baz HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(2, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); } @Test - public void testSingleGetParameter() { - invokeServer("GET " + URI + "?foo=bar HTTP/1.1"); - assertEquals("bar", testServer.parms.get("foo")); + public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { + invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); + assertNotNull(this.testServer.parms); + assertNotNull(this.testServer.header); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } @Test - public void testSingleGetParameterWithNoValue() { - invokeServer("GET " + URI + "?foo HTTP/1.1"); - assertEquals("", testServer.parms.get("foo")); + public void testFullyQualifiedWorkingGetRequest() throws Exception { + ByteArrayOutputStream outputStream = invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); + + String[] expected = { + "HTTP/1.1 200 OK", + "Content-Type: text/html", + "Date: .*", + "Connection: keep-alive", + "Content-Length: 0", + "" + }; + + assertResponse(outputStream, expected); } @Test public void testMultipleGetParameters() { - invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1"); - assertEquals("bar", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); + invokeServer("GET " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); + assertEquals("bar", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); } @Test public void testMultipleGetParametersWithMissingValue() { - invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1"); - assertEquals("", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); + invokeServer("GET " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1"); + assertEquals("", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); } @Test public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { - invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); - assertEquals("", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); - assertEquals("text/html", testServer.header.get("accept")); + invokeServer("GET " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); + assertEquals("", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); + assertEquals("text/html", this.testServer.header.get("accept")); } @Test - public void testDecodingParametersWithSingleValue() { - invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1"); - assertEquals("foo=bar&baz=zot", testServer.queryParameterString); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(1, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertTrue(testServer.decodedParamters.get("baz") instanceof List); - assertEquals(1, testServer.decodedParamters.get("baz").size()); - assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); + public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { + String userAgent = "jUnit 4.8.2 Unit Test"; + String accept = "text/html"; + invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); + assertEquals(userAgent, this.testServer.header.get("user-agent")); + assertEquals(accept, this.testServer.header.get("accept")); } @Test - public void testDecodingParametersWithSingleValueAndMissingValue() { - invokeServer("GET " + URI + "?foo&baz=zot HTTP/1.1"); - assertEquals("foo&baz=zot", testServer.queryParameterString); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(0, testServer.decodedParamters.get("foo").size()); - assertTrue(testServer.decodedParamters.get("baz") instanceof List); - assertEquals(1, testServer.decodedParamters.get("baz").size()); - assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); - } + public void testOutputOfServeSentBackToClient() throws Exception { + String responseBody = "Success!"; + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(responseBody); + ByteArrayOutputStream outputStream = invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1"); - @Test - public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { - invokeServer("GET " + URI + "?foo&bar= HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(0, testServer.decodedParamters.get("foo").size()); - assertTrue(testServer.decodedParamters.get("bar") instanceof List); - assertEquals(1, testServer.decodedParamters.get("bar").size()); - assertEquals("", testServer.decodedParamters.get("bar").get(0)); + String[] expected = { + "HTTP/1.1 200 OK", + "Content-Type: text/html", + "Date: .*", + "Connection: keep-alive", + "Content-Length: 8", + "", + responseBody + }; + + assertResponse(outputStream, expected); } @Test - public void testDecodingSingleFieldRepeated() { - invokeServer("GET " + URI + "?foo=bar&foo=baz HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(2, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); + public void testSingleGetParameter() { + invokeServer("GET " + HttpServerTest.URI + "?foo=bar HTTP/1.1"); + assertEquals("bar", this.testServer.parms.get("foo")); } @Test - public void testDecodingMixtureOfParameters() { - invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(2, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); - assertTrue(testServer.decodedParamters.get("zot") instanceof List); - assertEquals(0, testServer.decodedParamters.get("zot").size()); - assertTrue(testServer.decodedParamters.get("zim") instanceof List); - assertEquals(1, testServer.decodedParamters.get("zim").size()); - assertEquals("", testServer.decodedParamters.get("zim").get(0)); + public void testSingleGetParameterWithNoValue() { + invokeServer("GET " + HttpServerTest.URI + "?foo HTTP/1.1"); + assertEquals("", this.testServer.parms.get("foo")); } @Test - public void testDecodingParametersFromParameterMap() { - invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); - assertEquals(testServer.decodedParamters, testServer.decodedParamtersFromParameter); + public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { + String userAgent = "jUnit 4.8.2 Unit Test"; + invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); + assertEquals(userAgent, this.testServer.header.get("user-agent")); + assertEquals(NanoHTTPD.Method.GET, this.testServer.method); + assertEquals(HttpServerTest.URI, this.testServer.uri); } - // -------------------------------------------------------------------------------------------------------- - // // } diff --git a/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java index 2602fd9..4df669d 100644 --- a/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpHeadRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,12 +33,14 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.util.List; -import static junit.framework.Assert.*; +import org.junit.Test; public class HttpHeadRequestTest extends HttpServerTest { @@ -46,148 +48,150 @@ public class HttpHeadRequestTest extends HttpServerTest { public void setUp() { super.setUp(); String responseBody = "Success!"; - testServer.response = new NanoHTTPD.Response(responseBody); + this.testServer.response = new NanoHTTPD(0) { + }.newFixedLengthResponse(responseBody); } @Test - public void testHeadRequestDoesntSendBackResponseBody() throws Exception { - ByteArrayOutputStream outputStream = invokeServer("HEAD " + URI + " HTTP/1.1"); - - String[] expected = { - "HTTP/1.1 200 OK", - "Content-Type: text/html", - "Date: .*", - "Connection: keep-alive", - "Content-Length: 8", - "" - }; - - assertResponse(outputStream, expected); + public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo&bar= HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("foo").size()); + assertTrue(this.testServer.decodedParamters.get("bar") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("bar").size()); + assertEquals("", this.testServer.decodedParamters.get("bar").get(0)); } @Test - public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { - invokeServer("HEAD " + URI + " HTTP/1.1"); - assertNotNull(testServer.parms); - assertNotNull(testServer.header); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + public void testDecodingMixtureOfParameters() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(2, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); + assertTrue(this.testServer.decodedParamters.get("zot") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("zot").size()); + assertTrue(this.testServer.decodedParamters.get("zim") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("zim").size()); + assertEquals("", this.testServer.decodedParamters.get("zim").get(0)); } @Test - public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { - String userAgent = "jUnit 4.8.2 Unit Test"; - invokeServer("HEAD " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); - assertEquals(userAgent, testServer.header.get("user-agent")); - assertEquals(NanoHTTPD.Method.HEAD, testServer.method); - assertEquals(URI, testServer.uri); + public void testDecodingParametersFromParameterMap() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); + assertEquals(this.testServer.decodedParamters, this.testServer.decodedParamtersFromParameter); } + // -------------------------------------------------------------------------------------------------------- + // // + @Test - public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { - String userAgent = "jUnit 4.8.2 Unit Test"; - String accept = "text/html"; - invokeServer("HEAD " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); - assertEquals(userAgent, testServer.header.get("user-agent")); - assertEquals(accept, testServer.header.get("accept")); + public void testDecodingParametersWithSingleValue() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); + assertEquals("foo=bar&baz=zot", this.testServer.queryParameterString); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("baz").size()); + assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); } @Test - public void testSingleGetParameter() { - invokeServer("HEAD " + URI + "?foo=bar HTTP/1.1"); - assertEquals("bar", testServer.parms.get("foo")); + public void testDecodingParametersWithSingleValueAndMissingValue() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo&baz=zot HTTP/1.1"); + assertEquals("foo&baz=zot", this.testServer.queryParameterString); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(0, this.testServer.decodedParamters.get("foo").size()); + assertTrue(this.testServer.decodedParamters.get("baz") instanceof List); + assertEquals(1, this.testServer.decodedParamters.get("baz").size()); + assertEquals("zot", this.testServer.decodedParamters.get("baz").get(0)); } @Test - public void testSingleGetParameterWithNoValue() { - invokeServer("HEAD " + URI + "?foo HTTP/1.1"); - assertEquals("", testServer.parms.get("foo")); + public void testDecodingSingleFieldRepeated() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&foo=baz HTTP/1.1"); + assertTrue(this.testServer.decodedParamters.get("foo") instanceof List); + assertEquals(2, this.testServer.decodedParamters.get("foo").size()); + assertEquals("bar", this.testServer.decodedParamters.get("foo").get(0)); + assertEquals("baz", this.testServer.decodedParamters.get("foo").get(1)); } @Test - public void testMultipleGetParameters() { - invokeServer("HEAD " + URI + "?foo=bar&baz=zot HTTP/1.1"); - assertEquals("bar", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); + public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() { + invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1"); + assertNotNull(this.testServer.parms); + assertNotNull(this.testServer.header); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } @Test - public void testMultipleGetParametersWithMissingValue() { - invokeServer("HEAD " + URI + "?foo=&baz=zot HTTP/1.1"); - assertEquals("", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); + public void testHeadRequestDoesntSendBackResponseBody() throws Exception { + ByteArrayOutputStream outputStream = invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1"); + + String[] expected = { + "HTTP/1.1 200 OK", + "Content-Type: text/html", + "Date: .*", + "Connection: keep-alive", + "Content-Length: 8", + "" + }; + + assertResponse(outputStream, expected); } @Test - public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { - invokeServer("HEAD " + URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); - assertEquals("", testServer.parms.get("foo")); - assertEquals("zot", testServer.parms.get("baz")); - assertEquals("text/html", testServer.header.get("accept")); + public void testMultipleGetParameters() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar&baz=zot HTTP/1.1"); + assertEquals("bar", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); } @Test - public void testDecodingParametersWithSingleValue() { - invokeServer("HEAD " + URI + "?foo=bar&baz=zot HTTP/1.1"); - assertEquals("foo=bar&baz=zot", testServer.queryParameterString); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(1, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertTrue(testServer.decodedParamters.get("baz") instanceof List); - assertEquals(1, testServer.decodedParamters.get("baz").size()); - assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); + public void testMultipleGetParametersWithMissingValue() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1"); + assertEquals("", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); } @Test - public void testDecodingParametersWithSingleValueAndMissingValue() { - invokeServer("HEAD " + URI + "?foo&baz=zot HTTP/1.1"); - assertEquals("foo&baz=zot", testServer.queryParameterString); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(0, testServer.decodedParamters.get("foo").size()); - assertTrue(testServer.decodedParamters.get("baz") instanceof List); - assertEquals(1, testServer.decodedParamters.get("baz").size()); - assertEquals("zot", testServer.decodedParamters.get("baz").get(0)); + public void testMultipleGetParametersWithMissingValueAndRequestHeaders() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html"); + assertEquals("", this.testServer.parms.get("foo")); + assertEquals("zot", this.testServer.parms.get("baz")); + assertEquals("text/html", this.testServer.header.get("accept")); } @Test - public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() { - invokeServer("HEAD " + URI + "?foo&bar= HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(0, testServer.decodedParamters.get("foo").size()); - assertTrue(testServer.decodedParamters.get("bar") instanceof List); - assertEquals(1, testServer.decodedParamters.get("bar").size()); - assertEquals("", testServer.decodedParamters.get("bar").get(0)); + public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { + String userAgent = "jUnit 4.8.2 Unit Test"; + String accept = "text/html"; + invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept); + assertEquals(userAgent, this.testServer.header.get("user-agent")); + assertEquals(accept, this.testServer.header.get("accept")); } @Test - public void testDecodingSingleFieldRepeated() { - invokeServer("HEAD " + URI + "?foo=bar&foo=baz HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(2, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); + public void testSingleGetParameter() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo=bar HTTP/1.1"); + assertEquals("bar", this.testServer.parms.get("foo")); } @Test - public void testDecodingMixtureOfParameters() { - invokeServer("HEAD " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); - assertTrue(testServer.decodedParamters.get("foo") instanceof List); - assertEquals(2, testServer.decodedParamters.get("foo").size()); - assertEquals("bar", testServer.decodedParamters.get("foo").get(0)); - assertEquals("baz", testServer.decodedParamters.get("foo").get(1)); - assertTrue(testServer.decodedParamters.get("zot") instanceof List); - assertEquals(0, testServer.decodedParamters.get("zot").size()); - assertTrue(testServer.decodedParamters.get("zim") instanceof List); - assertEquals(1, testServer.decodedParamters.get("zim").size()); - assertEquals("", testServer.decodedParamters.get("zim").get(0)); + public void testSingleGetParameterWithNoValue() { + invokeServer("HEAD " + HttpServerTest.URI + "?foo HTTP/1.1"); + assertEquals("", this.testServer.parms.get("foo")); } @Test - public void testDecodingParametersFromParameterMap() { - invokeServer("HEAD " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1"); - assertEquals(testServer.decodedParamters, testServer.decodedParamtersFromParameter); + public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() { + String userAgent = "jUnit 4.8.2 Unit Test"; + invokeServer("HEAD " + HttpServerTest.URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n"); + assertEquals(userAgent, this.testServer.header.get("user-agent")); + assertEquals(NanoHTTPD.Method.HEAD, this.testServer.method); + assertEquals(HttpServerTest.URI, this.testServer.uri); } - // -------------------------------------------------------------------------------------------------------- - // // } diff --git a/core/src/test/java/fi/iki/elonen/HttpKeepAliveTest.java b/core/src/test/java/fi/iki/elonen/HttpKeepAliveTest.java index 829d5c5..98bdfcb 100644 --- a/core/src/test/java/fi/iki/elonen/HttpKeepAliveTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpKeepAliveTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -43,9 +43,11 @@ import org.junit.Test; public class HttpKeepAliveTest extends HttpServerTest { + private Throwable error = null; + @Test public void testManyGetRequests() throws Exception { - String request = "GET " + URI + " HTTP/1.1\r\n\r\n"; + String request = "GET " + HttpServerTest.URI + " HTTP/1.1\r\n\r\n"; String[] expected = { "HTTP/1.1 200 OK", "Content-Type: text/html", @@ -60,7 +62,7 @@ public class HttpKeepAliveTest extends HttpServerTest { @Test public void testManyPutRequests() throws Exception { String data = "BodyData 1\nLine 2"; - String request = "PUT " + URI + " HTTP/1.1\r\nContent-Length: " + data.length() + "\r\n\r\n" + data; + String request = "PUT " + HttpServerTest.URI + " HTTP/1.1\r\nContent-Length: " + data.length() + "\r\n\r\n" + data; String[] expected = { "HTTP/1.1 200 OK", "Content-Type: text/html", @@ -72,13 +74,11 @@ public class HttpKeepAliveTest extends HttpServerTest { testManyRequests(request, expected); } - private Throwable error = null; - /** * Issue the given request many times to check whether an error occurs. For * this test, a small stack size is used, since a stack overflow is among * the possible errors. - * + * * @param request * The request to issue * @param expected @@ -87,6 +87,7 @@ public class HttpKeepAliveTest extends HttpServerTest { public void testManyRequests(final String request, final String[] expected) throws Exception { Runnable r = new Runnable() { + @Override public void run() { try { PipedOutputStream requestStream = new PipedOutputStream(); @@ -94,7 +95,7 @@ public class HttpKeepAliveTest extends HttpServerTest { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); NanoHTTPD.DefaultTempFileManager tempFileManager = new NanoHTTPD.DefaultTempFileManager(); try { - NanoHTTPD.HTTPSession session = testServer.createSession(tempFileManager, inputStream, outputStream); + NanoHTTPD.HTTPSession session = HttpKeepAliveTest.this.testServer.createSession(tempFileManager, inputStream, outputStream); for (int i = 0; i < 2048; i++) { requestStream.write(request.getBytes()); requestStream.flush(); @@ -105,16 +106,16 @@ public class HttpKeepAliveTest extends HttpServerTest { tempFileManager.clear(); } } catch (Throwable t) { - error = t; + HttpKeepAliveTest.this.error = t; } } }; Thread t = new Thread(null, r, "Request Thread", 1 << 17); t.start(); t.join(); - if (error != null) { - fail("" + error); - error.printStackTrace(); + if (this.error != null) { + fail("" + this.error); + this.error.printStackTrace(); } } } diff --git a/core/src/test/java/fi/iki/elonen/HttpParsingTest.java b/core/src/test/java/fi/iki/elonen/HttpParsingTest.java index 9879bbe..fd819b6 100644 --- a/core/src/test/java/fi/iki/elonen/HttpParsingTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpParsingTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,34 +33,31 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; - -import java.net.URLDecoder; -import java.net.URLEncoder; - import static junit.framework.Assert.assertEquals; +import org.junit.Test; + public class HttpParsingTest extends HttpServerTest { + @Test + public void testMultibyteCharacterSupport() throws Exception { + String expected = "Chinese \u738b Letters"; + String input = "Chinese+%e7%8e%8b+Letters"; + assertEquals(expected, this.testServer.decodePercent(input)); + } + @Test public void testNormalCharacters() throws Exception { for (int i = 0x20; i < 0x80; i++) { String hex = Integer.toHexString(i); String input = "%" + hex; char expected = (char) i; - assertEquals("" + expected, testServer.decodePercent(input)); + assertEquals("" + expected, this.testServer.decodePercent(input)); } } - @Test - public void testMultibyteCharacterSupport() throws Exception { - String expected = "Chinese \u738b Letters"; - String input = "Chinese+%e7%8e%8b+Letters"; - assertEquals(expected, testServer.decodePercent(input)); - } - @Test public void testPlusInQueryParams() throws Exception { - assertEquals("foo bar", testServer.decodePercent("foo+bar")); + assertEquals("foo bar", this.testServer.decodePercent("foo+bar")); } } diff --git a/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java index c83a18d..346eb2d 100644 --- a/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,16 +33,15 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; +import static junit.framework.Assert.assertEquals; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.UUID; -import static junit.framework.Assert.assertEquals; +import org.junit.Test; public class HttpPostRequestTest extends HttpServerTest { @@ -60,122 +59,126 @@ public class HttpPostRequestTest extends HttpServerTest { public static final String VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS = "Test raw data & Result value"; - @Test - public void testSimpleRawPostData() throws Exception { - String header = "POST " + URI + " HTTP/1.1\n"; - String content = VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS + "\n"; + /** + * contains common preparation steps for testing POST with Multipart Form + * + * @param fileName + * Name of file to be uploaded + * @param fileContent + * Content of file to be uploaded + * @return input String with POST request complete information including + * header, length and content + */ + private String preparePostWithMultipartForm(String fileName, String fileContent) { + String divider = UUID.randomUUID().toString(); + String header = "POST " + HttpServerTest.URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data, boundary=" + divider + "\n"; + String content = + "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD + "\"; filename=\"" + fileName + "\"\n" + + "Content-Type: image/jpeg\r\n" + "\r\n" + fileContent + "\r\n" + "--" + divider + "--\n"; int size = content.length() + header.length(); int contentLengthHeaderValueSize = String.valueOf(size).length(); - int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length(); - String input = header + CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; + int contentLength = size + contentLengthHeaderValueSize + HttpPostRequestTest.CONTENT_LENGTH.length(); + String input = header + HttpPostRequestTest.CONTENT_LENGTH + (contentLength + 5) + "\r\n\r\n" + content; + + return input; + } + + @Test + public void testPostWithMultipartFormUpload() throws Exception { + String filename = "GrandCanyon.txt"; + String fileContent = HttpPostRequestTest.VALUE; + String input = preparePostWithMultipartForm(filename, fileContent); + invokeServer(input); - assertEquals(0, testServer.parms.size()); - assertEquals(1, testServer.files.size()); - assertEquals(VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS, testServer.files.get(POST_RAW_CONTENT_FILE_ENTRY)); + + assertEquals(1, this.testServer.parms.size()); + BufferedReader reader = new BufferedReader(new FileReader(this.testServer.files.get(HttpPostRequestTest.FIELD))); + List lines = readLinesFromFile(reader); + assertLinesOfText(new String[]{ + fileContent + }, lines); } @Test - public void testSimplePostWithSingleMultipartFormField() throws Exception { - String divider = UUID.randomUUID().toString(); - String header = "POST " + URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data; boundary=" + divider + "\n"; - String content = "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + FIELD + "\"\n" + "\n" + VALUE + "\n" + "--" + divider + "--\n"; - int size = content.length() + header.length(); - int contentLengthHeaderValueSize = String.valueOf(size).length(); - int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length(); - String input = header + CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; + public void testPostWithMultipartFormUploadFilenameHasSpaces() throws Exception { + String fileNameWithSpace = "Grand Canyon.txt"; + String fileContent = HttpPostRequestTest.VALUE; + String input = preparePostWithMultipartForm(fileNameWithSpace, fileContent); + invokeServer(input); - assertEquals(1, testServer.parms.size()); - assertEquals(VALUE, testServer.parms.get(FIELD)); + String fileNameAfter = new ArrayList(this.testServer.parms.values()).get(0); + + assertEquals(fileNameWithSpace, fileNameAfter); } @Test public void testPostWithMultipleMultipartFormFields() throws Exception { String divider = UUID.randomUUID().toString(); - String header = "POST " + URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data; boundary=" + divider + "\n"; + String header = "POST " + HttpServerTest.URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data; boundary=" + divider + "\n"; String content = - "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + FIELD + "\"\n" + "\n" + VALUE + "\n" + "--" + divider + "\n" - + "Content-Disposition: form-data; name=\"" + FIELD2 + "\"\n" + "\n" + VALUE2 + "\n" + "--" + divider + "--\n"; + "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD + "\"\n" + "\n" + HttpPostRequestTest.VALUE + "\n" + "--" + + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD2 + "\"\n" + "\n" + HttpPostRequestTest.VALUE2 + "\n" + "--" + + divider + "--\n"; int size = content.length() + header.length(); int contentLengthHeaderValueSize = String.valueOf(size).length(); - int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length(); - String input = header + CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; + int contentLength = size + contentLengthHeaderValueSize + HttpPostRequestTest.CONTENT_LENGTH.length(); + String input = header + HttpPostRequestTest.CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; invokeServer(input); - assertEquals(2, testServer.parms.size()); - assertEquals(VALUE, testServer.parms.get(FIELD)); - assertEquals(VALUE2, testServer.parms.get(FIELD2)); + assertEquals(2, this.testServer.parms.size()); + assertEquals(HttpPostRequestTest.VALUE, this.testServer.parms.get(HttpPostRequestTest.FIELD)); + assertEquals(HttpPostRequestTest.VALUE2, this.testServer.parms.get(HttpPostRequestTest.FIELD2)); } @Test public void testPostWithMultipleMultipartFormFieldsWhereContentTypeWasSeparatedByComma() throws Exception { String divider = UUID.randomUUID().toString(); - String header = "POST " + URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data, boundary=" + divider + "\n"; + String header = "POST " + HttpServerTest.URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data, boundary=" + divider + "\n"; String content = - "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + FIELD + "\"\n" + "\n" + VALUE + "\n" + "--" + divider + "\n" - + "Content-Disposition: form-data; name=\"" + FIELD2 + "\"\n" + "\n" + VALUE2 + "\n" + "--" + divider + "--\n"; + "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD + "\"\n" + "\n" + HttpPostRequestTest.VALUE + "\n" + "--" + + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD2 + "\"\n" + "\n" + HttpPostRequestTest.VALUE2 + "\n" + "--" + + divider + "--\n"; int size = content.length() + header.length(); int contentLengthHeaderValueSize = String.valueOf(size).length(); - int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length(); - String input = header + CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; + int contentLength = size + contentLengthHeaderValueSize + HttpPostRequestTest.CONTENT_LENGTH.length(); + String input = header + HttpPostRequestTest.CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; invokeServer(input); - assertEquals(2, testServer.parms.size()); - assertEquals(VALUE, testServer.parms.get(FIELD)); - assertEquals(VALUE2, testServer.parms.get(FIELD2)); + assertEquals(2, this.testServer.parms.size()); + assertEquals(HttpPostRequestTest.VALUE, this.testServer.parms.get(HttpPostRequestTest.FIELD)); + assertEquals(HttpPostRequestTest.VALUE2, this.testServer.parms.get(HttpPostRequestTest.FIELD2)); } @Test - public void testPostWithMultipartFormUpload() throws Exception { - String filename = "GrandCanyon.txt"; - String fileContent = VALUE; - String input = preparePostWithMultipartForm(filename, fileContent); - + public void testSimplePostWithSingleMultipartFormField() throws Exception { + String divider = UUID.randomUUID().toString(); + String header = "POST " + HttpServerTest.URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data; boundary=" + divider + "\n"; + String content = + "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + HttpPostRequestTest.FIELD + "\"\n" + "\n" + HttpPostRequestTest.VALUE + "\n" + "--" + + divider + "--\n"; + int size = content.length() + header.length(); + int contentLengthHeaderValueSize = String.valueOf(size).length(); + int contentLength = size + contentLengthHeaderValueSize + HttpPostRequestTest.CONTENT_LENGTH.length(); + String input = header + HttpPostRequestTest.CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; invokeServer(input); - assertEquals(1, testServer.parms.size()); - BufferedReader reader = new BufferedReader(new FileReader(testServer.files.get(FIELD))); - List lines = readLinesFromFile(reader); - assertLinesOfText(new String[]{ - fileContent - }, lines); + assertEquals(1, this.testServer.parms.size()); + assertEquals(HttpPostRequestTest.VALUE, this.testServer.parms.get(HttpPostRequestTest.FIELD)); } @Test - public void testPostWithMultipartFormUploadFilenameHasSpaces() throws Exception { - String fileNameWithSpace = "Grand Canyon.txt"; - String fileContent = VALUE; - String input = preparePostWithMultipartForm(fileNameWithSpace, fileContent); - - invokeServer(input); - - String fileNameAfter = new ArrayList(testServer.parms.values()).get(0); - - assertEquals(fileNameWithSpace, fileNameAfter); - } - - /** - * contains common preparation steps for testing POST with Multipart Form - * - * @param fileName - * Name of file to be uploaded - * @param fileContent - * Content of file to be uploaded - * @return input String with POST request complete information including - * header, length and content - */ - private String preparePostWithMultipartForm(String fileName, String fileContent) { - String divider = UUID.randomUUID().toString(); - String header = "POST " + URI + " HTTP/1.1\nContent-Type: " + "multipart/form-data, boundary=" + divider + "\n"; - String content = - "--" + divider + "\n" + "Content-Disposition: form-data; name=\"" + FIELD + "\"; filename=\"" + fileName + "\"\n" + "Content-Type: image/jpeg\r\n" + "\r\n" - + fileContent + "\r\n" + "--" + divider + "--\n"; + public void testSimpleRawPostData() throws Exception { + String header = "POST " + HttpServerTest.URI + " HTTP/1.1\n"; + String content = HttpPostRequestTest.VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS + "\n"; int size = content.length() + header.length(); int contentLengthHeaderValueSize = String.valueOf(size).length(); - int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length(); - String input = header + CONTENT_LENGTH + (contentLength + 5) + "\r\n\r\n" + content; - - return input; + int contentLength = size + contentLengthHeaderValueSize + HttpPostRequestTest.CONTENT_LENGTH.length(); + String input = header + HttpPostRequestTest.CONTENT_LENGTH + (contentLength + 4) + "\r\n\r\n" + content; + invokeServer(input); + assertEquals(0, this.testServer.parms.size()); + assertEquals(1, this.testServer.files.size()); + assertEquals(HttpPostRequestTest.VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS, this.testServer.files.get(HttpPostRequestTest.POST_RAW_CONTENT_FILE_ENTRY)); } } diff --git a/core/src/test/java/fi/iki/elonen/HttpPutRequestTest.java b/core/src/test/java/fi/iki/elonen/HttpPutRequestTest.java index e2755f9..9cc037a 100644 --- a/core/src/test/java/fi/iki/elonen/HttpPutRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpPutRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,20 +33,20 @@ package fi.iki.elonen; * #L% */ -import org.junit.Test; +import static junit.framework.Assert.assertTrue; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.FileReader; import java.util.List; -import static junit.framework.Assert.*; +import org.junit.Test; public class HttpPutRequestTest extends HttpServerTest { @Test public void testPutRequestSendsContent() throws Exception { - ByteArrayOutputStream outputStream = invokeServer("PUT " + URI + " HTTP/1.1\r\n\r\nBodyData 1\nLine 2"); + ByteArrayOutputStream outputStream = invokeServer("PUT " + HttpServerTest.URI + " HTTP/1.1\r\n\r\nBodyData 1\nLine 2"); String[] expectedOutput = { "HTTP/1.1 200 OK", @@ -59,14 +59,14 @@ public class HttpPutRequestTest extends HttpServerTest { assertResponse(outputStream, expectedOutput); - assertTrue(testServer.files.containsKey("content")); + assertTrue(this.testServer.files.containsKey("content")); BufferedReader reader = null; try { String[] expectedInputToServeMethodViaFile = { "BodyData 1", "Line 2" }; - reader = new BufferedReader(new FileReader(testServer.files.get("content"))); + reader = new BufferedReader(new FileReader(this.testServer.files.get("content"))); List lines = readLinesFromFile(reader); assertLinesOfText(expectedInputToServeMethodViaFile, lines); } finally { diff --git a/core/src/test/java/fi/iki/elonen/HttpServerTest.java b/core/src/test/java/fi/iki/elonen/HttpServerTest.java index f7fbb7b..3e3c7a0 100644 --- a/core/src/test/java/fi/iki/elonen/HttpServerTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpServerTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,104 +33,35 @@ package fi.iki.elonen; * #L% */ -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.*; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; import java.net.InetAddress; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import static junit.framework.Assert.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** * @author Paul S. Hawke (paul.hawke@gmail.com) On: 3/10/13 at 8:32 PM */ public class HttpServerTest { - public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html"; - - protected TestServer testServer; - - private TestTempFileManager tempFileManager; - - @Before - public void setUp() { - testServer = new TestServer(); - tempFileManager = new TestTempFileManager(); - } - - @After - public void tearDown() { - tempFileManager._clear(); - } - - @Test - public void testServerExists() { - assertNotNull(testServer); - } - - protected void assertResponse(ByteArrayOutputStream outputStream, String[] expected) throws IOException { - List lines = getOutputLines(outputStream); - assertLinesOfText(expected, lines); - } - - protected void assertLinesOfText(String[] expected, List lines) { - // assertEquals(expected.length, lines.size()); - for (int i = 0; i < expected.length; i++) { - String line = lines.get(i); - assertTrue("Output line " + i + " doesn't match expectation.\n" + " Output: " + line + "\n" + "Expected: " + expected[i], line.matches(expected[i])); - } - } - - protected ByteArrayOutputStream invokeServer(String request) { - ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes()); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - NanoHTTPD.HTTPSession session = testServer.createSession(tempFileManager, inputStream, outputStream); - try { - session.execute(); - } catch (IOException e) { - fail("" + e); - e.printStackTrace(); - } - return outputStream; - } - - protected List getOutputLines(ByteArrayOutputStream outputStream) throws IOException { - BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString())); - return readLinesFromFile(reader); - } - - protected List readLinesFromFile(BufferedReader reader) throws IOException { - List lines = new ArrayList(); - String line = ""; - while (line != null) { - line = reader.readLine(); - if (line != null) { - lines.add(line.trim()); - } - } - return lines; - } - - public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager { - - public void _clear() { - super.clear(); - } - - @Override - public void clear() { - // ignore - } - } - public static class TestServer extends NanoHTTPD { - public Response response = new Response(""); + public Response response = newFixedLengthResponse(""); public String uri; @@ -160,6 +91,11 @@ public class HttpServerTest { return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); } + @Override + public String decodePercent(String str) { + return super.decodePercent(str); + } + @Override public Response serve(IHTTPSession session) { this.uri = session.getUri(); @@ -168,19 +104,91 @@ public class HttpServerTest { this.parms = session.getParms(); this.files = new HashMap(); try { - session.parseBody(files); + session.parseBody(this.files); } catch (Exception e) { e.printStackTrace(); } - queryParameterString = session.getQueryParameterString(); - this.decodedParamtersFromParameter = decodeParameters(queryParameterString); + this.queryParameterString = session.getQueryParameterString(); + this.decodedParamtersFromParameter = decodeParameters(this.queryParameterString); this.decodedParamters = decodeParameters(session.getQueryParameterString()); - return response; + return this.response; + } + } + + public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager { + + public void _clear() { + super.clear(); } @Override - public String decodePercent(String str) { - return super.decodePercent(str); + public void clear() { + // ignore } } + + public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html"; + + protected TestServer testServer; + + private TestTempFileManager tempFileManager; + + protected void assertLinesOfText(String[] expected, List lines) { + // assertEquals(expected.length, lines.size()); + for (int i = 0; i < expected.length; i++) { + String line = lines.get(i); + assertTrue("Output line " + i + " doesn't match expectation.\n" + " Output: " + line + "\n" + "Expected: " + expected[i], line.matches(expected[i])); + } + } + + protected void assertResponse(ByteArrayOutputStream outputStream, String[] expected) throws IOException { + List lines = getOutputLines(outputStream); + assertLinesOfText(expected, lines); + } + + protected List getOutputLines(ByteArrayOutputStream outputStream) throws IOException { + BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString())); + return readLinesFromFile(reader); + } + + protected ByteArrayOutputStream invokeServer(String request) { + ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + NanoHTTPD.HTTPSession session = this.testServer.createSession(this.tempFileManager, inputStream, outputStream); + try { + session.execute(); + } catch (IOException e) { + fail("" + e); + e.printStackTrace(); + } + return outputStream; + } + + protected List readLinesFromFile(BufferedReader reader) throws IOException { + List lines = new ArrayList(); + String line = ""; + while (line != null) { + line = reader.readLine(); + if (line != null) { + lines.add(line.trim()); + } + } + return lines; + } + + @Before + public void setUp() { + this.testServer = new TestServer(); + this.tempFileManager = new TestTempFileManager(); + } + + @After + public void tearDown() { + this.tempFileManager._clear(); + } + + @Test + public void testServerExists() { + assertNotNull(this.testServer); + } } diff --git a/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java b/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java index 3402533..39e40c6 100644 --- a/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpSessionHeadersTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,14 +33,14 @@ package fi.iki.elonen; * #L% */ -import org.junit.Ignore; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.net.InetAddress; -import static org.junit.Assert.assertEquals; +import org.junit.Ignore; +import org.junit.Test; public class HttpSessionHeadersTest extends HttpServerTest { @@ -56,7 +56,7 @@ public class HttpSessionHeadersTest extends HttpServerTest { @Test @Ignore public void testHeadersRemoteIp() throws Exception { - ByteArrayInputStream inputStream = new ByteArrayInputStream(DUMMY_REQUEST_CONTENT.getBytes()); + ByteArrayInputStream inputStream = new ByteArrayInputStream(HttpSessionHeadersTest.DUMMY_REQUEST_CONTENT.getBytes()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); String[] ipAddresses = { "127.0.0.1", @@ -65,7 +65,7 @@ public class HttpSessionHeadersTest extends HttpServerTest { }; for (String ipAddress : ipAddresses) { InetAddress inetAddress = InetAddress.getByName(ipAddress); - NanoHTTPD.HTTPSession session = testServer.createSession(TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress); + NanoHTTPD.HTTPSession session = this.testServer.createSession(HttpSessionHeadersTest.TEST_TEMP_FILE_MANAGER, inputStream, outputStream, inetAddress); assertEquals(ipAddress, session.getHeaders().get("remote-addr")); assertEquals(ipAddress, session.getHeaders().get("http-client-ip")); } diff --git a/core/src/test/java/fi/iki/elonen/InvalidRequestTest.java b/core/src/test/java/fi/iki/elonen/InvalidRequestTest.java index 2c40cd1..2178d63 100644 --- a/core/src/test/java/fi/iki/elonen/InvalidRequestTest.java +++ b/core/src/test/java/fi/iki/elonen/InvalidRequestTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -32,48 +32,48 @@ package fi.iki.elonen; * OF THE POSSIBILITY OF SUCH DAMAGE. * #L% */ -import org.junit.Test; - import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; +import org.junit.Test; + public class InvalidRequestTest extends HttpServerTest { @Test public void testGetRequestWithoutProtocol() { - invokeServer("GET " + URI + "\r\nX-Important-Header: foo"); + invokeServer("GET " + HttpServerTest.URI + "\r\nX-Important-Header: foo"); - assertNotNull(testServer.parms); - assertTrue(testServer.header.size() > 0); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + assertNotNull(this.testServer.parms); + assertTrue(this.testServer.header.size() > 0); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } @Test - public void testPostRequestWithoutProtocol() { - invokeServer("POST " + URI + "\r\nContent-Length: 123"); - assertNotNull(testServer.parms); - assertTrue(testServer.header.size() > 0); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + public void testGetRequestWithProtocol() { + invokeServer("GET " + HttpServerTest.URI + " HTTP/1.1\r\nX-Important-Header: foo"); + + assertNotNull(this.testServer.parms); + assertTrue(this.testServer.header.size() > 0); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } @Test - public void testGetRequestWithProtocol() { - invokeServer("GET " + URI + " HTTP/1.1\r\nX-Important-Header: foo"); - - assertNotNull(testServer.parms); - assertTrue(testServer.header.size() > 0); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + public void testPostRequestWithoutProtocol() { + invokeServer("POST " + HttpServerTest.URI + "\r\nContent-Length: 123"); + assertNotNull(this.testServer.parms); + assertTrue(this.testServer.header.size() > 0); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } @Test public void testPostRequestWithProtocol() { - invokeServer("POST " + URI + " HTTP/1.1\r\nContent-Length: 123"); - assertNotNull(testServer.parms); - assertTrue(testServer.header.size() > 0); - assertNotNull(testServer.files); - assertNotNull(testServer.uri); + invokeServer("POST " + HttpServerTest.URI + " HTTP/1.1\r\nContent-Length: 123"); + assertNotNull(this.testServer.parms); + assertTrue(this.testServer.header.size() > 0); + assertNotNull(this.testServer.files); + assertNotNull(this.testServer.uri); } } diff --git a/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java index f765a38..f085f3e 100644 --- a/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,7 +33,13 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + import org.apache.http.client.CookieStore; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; @@ -41,40 +47,64 @@ import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.cookie.BasicClientCookie; import org.junit.Test; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - -import static org.junit.Assert.*; +import fi.iki.elonen.NanoHTTPD; /** * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:10 PM */ public class CookieIntegrationTest extends IntegrationTestBase { - @Test - public void testNoCookies() throws Exception { - HttpGet httpget = new HttpGet("http://localhost:8192/"); - ResponseHandler responseHandler = new BasicResponseHandler(); - httpclient.execute(httpget, responseHandler); + public static class CookieTestServer extends NanoHTTPD { - CookieStore cookies = httpclient.getCookieStore(); - assertEquals(0, cookies.getCookies().size()); + List cookiesReceived = new ArrayList(); + + List cookiesToSend = new ArrayList(); + + public CookieTestServer() { + super(8192); + } + + @Override + public Response serve(IHTTPSession session) { + CookieHandler cookies = session.getCookies(); + for (String cookieName : cookies) { + this.cookiesReceived.add(new Cookie(cookieName, cookies.read(cookieName))); + } + for (Cookie c : this.cookiesToSend) { + cookies.set(c); + } + return newFixedLengthResponse("Cookies!"); + } + } + + @Override + public CookieTestServer createTestServer() { + return new CookieTestServer(); } @Test public void testCookieSentBackToClient() throws Exception { - testServer.cookiesToSend.add(new NanoHTTPD.Cookie("name", "value", 30)); + this.testServer.cookiesToSend.add(new NanoHTTPD.Cookie("name", "value", 30)); HttpGet httpget = new HttpGet("http://localhost:8192/"); ResponseHandler responseHandler = new BasicResponseHandler(); - httpclient.execute(httpget, responseHandler); + this.httpclient.execute(httpget, responseHandler); - CookieStore cookies = httpclient.getCookieStore(); + CookieStore cookies = this.httpclient.getCookieStore(); assertEquals(1, cookies.getCookies().size()); assertEquals("name", cookies.getCookies().get(0).getName()); assertEquals("value", cookies.getCookies().get(0).getValue()); } + @Test + public void testNoCookies() throws Exception { + HttpGet httpget = new HttpGet("http://localhost:8192/"); + ResponseHandler responseHandler = new BasicResponseHandler(); + this.httpclient.execute(httpget, responseHandler); + + CookieStore cookies = this.httpclient.getCookieStore(); + assertEquals(0, cookies.getCookies().size()); + } + @Test public void testServerReceivesCookiesSentFromClient() throws Exception { BasicClientCookie clientCookie = new BasicClientCookie("name", "value"); @@ -82,40 +112,12 @@ public class CookieIntegrationTest extends IntegrationTestBase responseHandler = new BasicResponseHandler(); - httpclient.execute(httpget, responseHandler); - - assertEquals(1, testServer.cookiesReceived.size()); - assertTrue(testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value")); - } + this.httpclient.execute(httpget, responseHandler); - @Override - public CookieTestServer createTestServer() { - return new CookieTestServer(); - } - - public static class CookieTestServer extends NanoHTTPD { - - List cookiesReceived = new ArrayList(); - - List cookiesToSend = new ArrayList(); - - public CookieTestServer() { - super(8192); - } - - @Override - public Response serve(IHTTPSession session) { - CookieHandler cookies = session.getCookies(); - for (String cookieName : cookies) { - cookiesReceived.add(new Cookie(cookieName, cookies.read(cookieName))); - } - for (Cookie c : cookiesToSend) { - cookies.set(c); - } - return new Response("Cookies!"); - } + assertEquals(1, this.testServer.cookiesReceived.size()); + assertTrue(this.testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value")); } } diff --git a/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java index ea4041d..127aac5 100644 --- a/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,9 +33,14 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + import org.apache.http.NameValuePair; -import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; @@ -44,61 +49,61 @@ import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; +import fi.iki.elonen.NanoHTTPD; /** * @author Paul S. Hawke (paul.hawke@gmail.com) On: 5/19/13 at 5:36 PM */ public class GetAndPostIntegrationTest extends IntegrationTestBase { - @Test - public void testSimpleGetRequest() throws Exception { - testServer.response = "testSimpleGetRequest"; + public static class TestServer extends NanoHTTPD { - HttpGet httpget = new HttpGet("http://localhost:8192/"); - ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httpget, responseHandler); + public String response; - assertEquals("GET:testSimpleGetRequest", responseBody); - } + public TestServer() { + super(8192); + } - @Test - public void testGetRequestWithParameters() throws Exception { - testServer.response = "testGetRequestWithParameters"; + @Override + public Response serve(String uri, Method method, Map header, Map parms, Map files) { + StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + this.response); - HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male"); - ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httpget, responseHandler); + if (parms.size() > 1) { + parms.remove("NanoHttpd.QUERY_STRING"); + sb.append("-params=").append(parms.size()); + List p = new ArrayList(parms.keySet()); + Collections.sort(p); + for (String k : p) { + sb.append(';').append(k).append('=').append(parms.get(k)); + } + } - assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody); + return newFixedLengthResponse(sb.toString()); + } + } + + @Override + public TestServer createTestServer() { + return new TestServer(); } @Test - public void testPostWithNoParameters() throws Exception { - testServer.response = "testPostWithNoParameters"; + public void testGetRequestWithParameters() throws Exception { + this.testServer.response = "testGetRequestWithParameters"; - HttpPost httppost = new HttpPost("http://localhost:8192/"); + HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male"); ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httppost, responseHandler); + String responseBody = this.httpclient.execute(httpget, responseHandler); - assertEquals("POST:testPostWithNoParameters", responseBody); + assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody); } @Test public void testPostRequestWithFormEncodedParameters() throws Exception { - testServer.response = "testPostRequestWithFormEncodedParameters"; + this.testServer.response = "testPostRequestWithFormEncodedParameters"; HttpPost httppost = new HttpPost("http://localhost:8192/"); List postParameters = new ArrayList(); @@ -107,14 +112,14 @@ public class GetAndPostIntegrationTest extends IntegrationTestBase responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httppost, responseHandler); + String responseBody = this.httpclient.execute(httppost, responseHandler); assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody); } @Test public void testPostRequestWithMultipartEncodedParameters() throws Exception { - testServer.response = "testPostRequestWithMultipartEncodedParameters"; + this.testServer.response = "testPostRequestWithMultipartEncodedParameters"; HttpPost httppost = new HttpPost("http://localhost:8192/"); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); @@ -123,39 +128,30 @@ public class GetAndPostIntegrationTest extends IntegrationTestBase responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httppost, responseHandler); + String responseBody = this.httpclient.execute(httppost, responseHandler); assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody); } - @Override - public TestServer createTestServer() { - return new TestServer(); - } - - public static class TestServer extends NanoHTTPD { + @Test + public void testPostWithNoParameters() throws Exception { + this.testServer.response = "testPostWithNoParameters"; - public String response; + HttpPost httppost = new HttpPost("http://localhost:8192/"); + ResponseHandler responseHandler = new BasicResponseHandler(); + String responseBody = this.httpclient.execute(httppost, responseHandler); - public TestServer() { - super(8192); - } + assertEquals("POST:testPostWithNoParameters", responseBody); + } - @Override - public Response serve(String uri, Method method, Map header, Map parms, Map files) { - StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + response); + @Test + public void testSimpleGetRequest() throws Exception { + this.testServer.response = "testSimpleGetRequest"; - if (parms.size() > 1) { - parms.remove("NanoHttpd.QUERY_STRING"); - sb.append("-params=").append(parms.size()); - List p = new ArrayList(parms.keySet()); - Collections.sort(p); - for (String k : p) { - sb.append(';').append(k).append('=').append(parms.get(k)); - } - } + HttpGet httpget = new HttpGet("http://localhost:8192/"); + ResponseHandler responseHandler = new BasicResponseHandler(); + String responseBody = this.httpclient.execute(httpget, responseHandler); - return new Response(sb.toString()); - } + assertEquals("GET:testSimpleGetRequest", responseBody); } } diff --git a/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java b/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java index 9ad6b73..36de796 100644 --- a/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java +++ b/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,13 +33,13 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; -import org.apache.http.client.HttpClient; +import java.io.IOException; + import org.apache.http.impl.client.DefaultHttpClient; import org.junit.After; import org.junit.Before; -import java.io.IOException; +import fi.iki.elonen.NanoHTTPD; /** * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:02 PM @@ -50,12 +50,14 @@ public abstract class IntegrationTestBase { protected T testServer; + public abstract T createTestServer(); + @Before public void setUp() { - testServer = createTestServer(); - httpclient = new DefaultHttpClient(); + this.testServer = createTestServer(); + this.httpclient = new DefaultHttpClient(); try { - testServer.start(); + this.testServer.start(); } catch (IOException e) { e.printStackTrace(); } @@ -63,9 +65,7 @@ public abstract class IntegrationTestBase { @After public void tearDown() { - httpclient.getConnectionManager().shutdown(); - testServer.stop(); + this.httpclient.getConnectionManager().shutdown(); + this.testServer.stop(); } - - public abstract T createTestServer(); } diff --git a/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java index 6bdde4d..0232dc0 100644 --- a/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -49,34 +49,12 @@ import fi.iki.elonen.NanoHTTPD; public class PutStreamIntegrationTest extends IntegrationTestBase { - @Test - public void testSimplePutRequest() throws Exception { - String expected = "This HttpPut request has a content-length of 48."; - - HttpPut httpput = new HttpPut("http://localhost:8192/"); - httpput.setEntity(new ByteArrayEntity(expected.getBytes())); - ResponseHandler responseHandler = new BasicResponseHandler(); - String responseBody = httpclient.execute(httpput, responseHandler); - - assertEquals("PUT:" + expected, responseBody); - } - - @Override - public TestServer createTestServer() { - return new TestServer(); - } - public static class TestServer extends NanoHTTPD { public TestServer() { super(8192); } - @Override - public Response serve(String uri, Method method, Map headers, Map parms, Map files) { - throw new UnsupportedOperationException(); - } - @Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); @@ -89,11 +67,33 @@ public class PutStreamIntegrationTest extends IntegrationTestBase headers, Map parms, Map files) { + throw new UnsupportedOperationException(); + } + } + + @Override + public TestServer createTestServer() { + return new TestServer(); + } + + @Test + public void testSimplePutRequest() throws Exception { + String expected = "This HttpPut request has a content-length of 48."; + + HttpPut httpput = new HttpPut("http://localhost:8192/"); + httpput.setEntity(new ByteArrayEntity(expected.getBytes())); + ResponseHandler responseHandler = new BasicResponseHandler(); + String responseBody = this.httpclient.execute(httpput, responseHandler); + + assertEquals("PUT:" + expected, responseBody); } } diff --git a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java index fc3b57b..c650f52 100644 --- a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,10 +33,7 @@ package fi.iki.elonen.integration; * #L% */ -import static org.junit.Assert.*; -import fi.iki.elonen.NanoHTTPD; - -import org.junit.Test; +import static org.junit.Assert.fail; import java.io.IOException; import java.io.InputStream; @@ -44,8 +41,24 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import org.junit.Test; + +import fi.iki.elonen.NanoHTTPD; + public class ShutdownTest { + private class TestServer extends NanoHTTPD { + + public TestServer() { + super(8092); + } + + @Override + public Response serve(IHTTPSession session) { + return newFixedLengthResponse("Whatever"); + } + } + @Test public void connectionsAreClosedWhenServerStops() throws IOException { TestServer server = new TestServer(); @@ -71,16 +84,4 @@ public class ShutdownTest { in.close(); } - private class TestServer extends NanoHTTPD { - - public TestServer() { - super(8092); - } - - @Override - public Response serve(IHTTPSession session) { - return new Response("Whatever"); - } - } - } -- cgit v1.2.3