1 package fi.iki.elonen;
2 
3 /*
4  * #%L
5  * NanoHttpd-Core
6  * %%
7  * Copyright (C) 2012 - 2015 nanohttpd
8  * %%
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this
13  *    list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the nanohttpd nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  * #L%
34  */
35 
36 import static junit.framework.Assert.assertNotNull;
37 import static junit.framework.Assert.assertTrue;
38 import static junit.framework.Assert.fail;
39 
40 import java.io.BufferedReader;
41 import java.io.ByteArrayInputStream;
42 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.io.StringReader;
47 import java.net.InetAddress;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 
53 import org.junit.After;
54 import org.junit.Before;
55 import org.junit.Test;
56 
57 /**
58  * @author Paul S. Hawke (paul.hawke@gmail.com) On: 3/10/13 at 8:32 PM
59  */
60 public class HttpServerTest {
61 
62     public static class TestServer extends NanoHTTPD {
63 
64         public Response response = newFixedLengthResponse("");
65 
66         public String uri;
67 
68         public Method method;
69 
70         public Map<String, String> header;
71 
72         public Map<String, String> parms;
73 
74         public Map<String, String> files;
75 
76         public Map<String, List<String>> decodedParamters;
77 
78         public Map<String, List<String>> decodedParamtersFromParameter;
79 
80         public String queryParameterString;
81 
TestServer()82         public TestServer() {
83             super(8192);
84         }
85 
TestServer(int port)86         public TestServer(int port) {
87             super(port);
88         }
89 
createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream)90         public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) {
91             return new HTTPSession(tempFileManager, inputStream, outputStream);
92         }
93 
createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress)94         public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) {
95             return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress);
96         }
97 
98         @Override
serve(IHTTPSession session)99         public Response serve(IHTTPSession session) {
100             this.uri = session.getUri();
101             this.method = session.getMethod();
102             this.header = session.getHeaders();
103             this.parms = session.getParms();
104             this.files = new HashMap<String, String>();
105             try {
106                 session.parseBody(this.files);
107             } catch (Exception e) {
108                 e.printStackTrace();
109             }
110             this.queryParameterString = session.getQueryParameterString();
111             this.decodedParamtersFromParameter = decodeParameters(this.queryParameterString);
112             this.decodedParamters = decodeParameters(session.getQueryParameterString());
113             return this.response;
114         }
115     }
116 
117     public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager {
118 
_clear()119         public void _clear() {
120             super.clear();
121         }
122 
123         @Override
clear()124         public void clear() {
125             // ignore
126         }
127     }
128 
129     public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html";
130 
131     protected TestServer testServer;
132 
133     protected TestTempFileManager tempFileManager;
134 
assertLinesOfText(String[] expected, List<String> lines)135     protected void assertLinesOfText(String[] expected, List<String> lines) {
136         // assertEquals(expected.length, lines.size());
137         for (int i = 0; i < expected.length; i++) {
138             String line = lines.get(i);
139             assertTrue("Output line " + i + " doesn't match expectation.\n" + "  Output: " + line + "\n" + "Expected: " + expected[i], line.matches(expected[i]));
140         }
141     }
142 
assertResponse(ByteArrayOutputStream outputStream, String[] expected)143     protected void assertResponse(ByteArrayOutputStream outputStream, String[] expected) throws IOException {
144         List<String> lines = getOutputLines(outputStream);
145         assertLinesOfText(expected, lines);
146     }
147 
getOutputLines(ByteArrayOutputStream outputStream)148     protected List<String> getOutputLines(ByteArrayOutputStream outputStream) throws IOException {
149         BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString()));
150         return readLinesFromFile(reader);
151     }
152 
invokeServer(String request)153     protected ByteArrayOutputStream invokeServer(String request) {
154         ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes());
155         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
156         NanoHTTPD.HTTPSession session = this.testServer.createSession(this.tempFileManager, inputStream, outputStream);
157         try {
158             session.execute();
159         } catch (IOException e) {
160             fail("" + e);
161             e.printStackTrace();
162         }
163         return outputStream;
164     }
165 
readLinesFromFile(BufferedReader reader)166     protected List<String> readLinesFromFile(BufferedReader reader) throws IOException {
167         List<String> lines = new ArrayList<String>();
168         String line = "";
169         while (line != null) {
170             line = reader.readLine();
171             if (line != null) {
172                 lines.add(line.trim());
173             }
174         }
175         return lines;
176     }
177 
178     @Before
setUp()179     public void setUp() throws Exception {
180         this.testServer = new TestServer();
181         this.tempFileManager = new TestTempFileManager();
182     }
183 
184     @After
tearDown()185     public void tearDown() {
186         this.tempFileManager._clear();
187     }
188 
189     @Test
testServerExists()190     public void testServerExists() {
191         assertNotNull(this.testServer);
192     }
193 }
194