1 package fi.iki.elonen; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 import java.io.*; 8 import java.net.InetAddress; 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 import static junit.framework.Assert.*; 15 16 /** 17 * @author Paul S. Hawke (paul.hawke (at) gmail.com) 18 * On: 3/10/13 at 8:32 PM 19 */ 20 public class HttpServerTest { 21 public static final String URI = "http://www.myserver.org/pub/WWW/someFile.html"; 22 protected TestServer testServer; 23 private TestTempFileManager tempFileManager; 24 25 @Before 26 public void setUp() { 27 testServer = new TestServer(); 28 tempFileManager = new TestTempFileManager(); 29 } 30 31 @After 32 public void tearDown() { 33 tempFileManager._clear(); 34 } 35 36 @Test 37 public void testServerExists() { 38 assertNotNull(testServer); 39 } 40 41 protected void assertResponse(ByteArrayOutputStream outputStream, String[] expected) throws IOException { 42 List<String> lines = getOutputLines(outputStream); 43 assertLinesOfText(expected, lines); 44 } 45 46 protected void assertLinesOfText(String[] expected, List<String> lines) { 47 // assertEquals(expected.length, lines.size()); 48 for (int i = 0; i < expected.length; i++) { 49 String line = lines.get(i); 50 assertTrue("Output line " + i + " doesn't match expectation.\n" + 51 " Output: " + line + "\n" + 52 "Expected: " + expected[i], line.matches(expected[i])); 53 } 54 } 55 56 protected ByteArrayOutputStream invokeServer(String request) { 57 ByteArrayInputStream inputStream = new ByteArrayInputStream(request.getBytes()); 58 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 59 NanoHTTPD.HTTPSession session = testServer.createSession(tempFileManager, inputStream, outputStream); 60 try { 61 session.execute(); 62 } catch (IOException e) { 63 fail(""+e); 64 e.printStackTrace(); 65 } 66 return outputStream; 67 } 68 69 protected List<String> getOutputLines(ByteArrayOutputStream outputStream) throws IOException { 70 BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString())); 71 return readLinesFromFile(reader); 72 } 73 74 protected List<String> readLinesFromFile(BufferedReader reader) throws IOException { 75 List<String> lines = new ArrayList<String>(); 76 String line = ""; 77 while (line != null) { 78 line = reader.readLine(); 79 if (line != null) { 80 lines.add(line.trim()); 81 } 82 } 83 return lines; 84 } 85 86 public static class TestTempFileManager extends NanoHTTPD.DefaultTempFileManager { 87 public void _clear() { 88 super.clear(); 89 } 90 91 @Override 92 public void clear() { 93 // ignore 94 } 95 } 96 97 public static class TestServer extends NanoHTTPD { 98 public Response response = new Response(""); 99 public String uri; 100 public Method method; 101 public Map<String, String> header; 102 public Map<String, String> parms; 103 public Map<String, String> files; 104 public Map<String, List<String>> decodedParamters; 105 public Map<String, List<String>> decodedParamtersFromParameter; 106 public String queryParameterString; 107 108 public TestServer() { 109 super(8192); 110 } 111 112 public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { 113 return new HTTPSession(tempFileManager, inputStream, outputStream); 114 } 115 116 public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { 117 return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); 118 } 119 120 @Override public Response serve(IHTTPSession session) { 121 this.uri = session.getUri(); 122 this.method = session.getMethod(); 123 this.header = session.getHeaders(); 124 this.parms = session.getParms(); 125 this.files = new HashMap<String, String>(); 126 try { 127 session.parseBody(files); 128 } catch (Exception e) { 129 e.printStackTrace(); 130 } 131 queryParameterString = session.getQueryParameterString(); 132 this.decodedParamtersFromParameter = decodeParameters(queryParameterString); 133 this.decodedParamters = decodeParameters(session.getQueryParameterString()); 134 return response; 135 } 136 137 @Override 138 public String decodePercent(String str) { 139 return super.decodePercent(str); 140 } 141 } 142 } 143