diff options
Diffstat (limited to 'core/src/test/java/fi/iki/elonen/HttpServerTest.java')
-rw-r--r-- | core/src/test/java/fi/iki/elonen/HttpServerTest.java | 225 |
1 files changed, 138 insertions, 87 deletions
diff --git a/core/src/test/java/fi/iki/elonen/HttpServerTest.java b/core/src/test/java/fi/iki/elonen/HttpServerTest.java index ef209d1..e8be61e 100644 --- a/core/src/test/java/fi/iki/elonen/HttpServerTest.java +++ b/core/src/test/java/fi/iki/elonen/HttpServerTest.java @@ -1,41 +1,143 @@ package fi.iki.elonen; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +/* + * #%L + * NanoHttpd-Core + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * 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. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ -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 + * @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(); + public static class TestServer extends NanoHTTPD { + + public Response response = newFixedLengthResponse(""); + + public String uri; + + public Method method; + + public Map<String, String> header; + + public Map<String, String> parms; + + public Map<String, String> files; + + public Map<String, List<String>> decodedParamters; + + public Map<String, List<String>> decodedParamtersFromParameter; + + public String queryParameterString; + + public TestServer() { + super(8192); + } + + public TestServer(int port) { + super(port); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { + return new HTTPSession(tempFileManager, inputStream, outputStream); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { + return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); + } + + @Override + public Response serve(IHTTPSession session) { + this.uri = session.getUri(); + this.method = session.getMethod(); + this.header = session.getHeaders(); + this.parms = session.getParms(); + this.files = new HashMap<String, String>(); + try { + session.parseBody(this.files); + } catch (Exception e) { + e.printStackTrace(); + } + this.queryParameterString = session.getQueryParameterString(); + this.decodedParamtersFromParameter = decodeParameters(this.queryParameterString); + this.decodedParamters = decodeParameters(session.getQueryParameterString()); + return this.response; + } } - @After - public void tearDown() { - tempFileManager._clear(); + public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager { + + public void _clear() { + super.clear(); + } + + @Override + public void clear() { + // ignore + } } - @Test - public void testServerExists() { - assertNotNull(testServer); + public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html"; + + protected TestServer testServer; + + protected TestTempFileManager tempFileManager; + + protected void assertLinesOfText(String[] expected, List<String> 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 { @@ -43,34 +145,24 @@ public class HttpServerTest { assertLinesOfText(expected, lines); } - protected void assertLinesOfText(String[] expected, List<String> 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 List<String> 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 = testServer.createSession(tempFileManager, inputStream, outputStream); + NanoHTTPD.HTTPSession session = this.testServer.createSession(this.tempFileManager, inputStream, outputStream); try { session.execute(); } catch (IOException e) { - fail(""+e); + fail("" + e); e.printStackTrace(); } return outputStream; } - protected List<String> getOutputLines(ByteArrayOutputStream outputStream) throws IOException { - BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString())); - return readLinesFromFile(reader); - } - protected List<String> readLinesFromFile(BufferedReader reader) throws IOException { List<String> lines = new ArrayList<String>(); String line = ""; @@ -83,60 +175,19 @@ public class HttpServerTest { return lines; } - public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager { - public void _clear() { - super.clear(); - } - - @Override - public void clear() { - // ignore - } + @Before + public void setUp() throws Exception { + this.testServer = new TestServer(); + this.tempFileManager = new TestTempFileManager(); } - public static class TestServer extends NanoHTTPD { - public Response response = new Response(""); - public String uri; - public Method method; - public Map<String, String> header; - public Map<String, String> parms; - public Map<String, String> files; - public Map<String, List<String>> decodedParamters; - public Map<String, List<String>> decodedParamtersFromParameter; - public String queryParameterString; - - public TestServer() { - super(8192); - } - - public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { - return new HTTPSession(tempFileManager, inputStream, outputStream); - } - - public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { - return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); - } - - @Override public Response serve(IHTTPSession session) { - this.uri = session.getUri(); - this.method = session.getMethod(); - this.header = session.getHeaders(); - this.parms = session.getParms(); - this.files = new HashMap<String, String>(); - try { - session.parseBody(files); - } catch (Exception e) { - e.printStackTrace(); - } - queryParameterString = session.getQueryParameterString(); - this.decodedParamtersFromParameter = decodeParameters(queryParameterString); - this.decodedParamters = decodeParameters(session.getQueryParameterString()); - return response; - } + @After + public void tearDown() { + this.tempFileManager._clear(); + } - @Override - public String decodePercent(String str) { - return super.decodePercent(str); - } + @Test + public void testServerExists() { + assertNotNull(this.testServer); } } |