1f869fff37123ba876b5ee78de4144b1e844a2f50ritchiepackage fi.iki.elonen; 2f869fff37123ba876b5ee78de4144b1e844a2f50ritchie 3f869fff37123ba876b5ee78de4144b1e844a2f50ritchie/* 4f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * #%L 5f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * NanoHttpd-Webserver 6f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * %% 7f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * Copyright (C) 2012 - 2015 nanohttpd 8f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * %% 9f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * Redistribution and use in source and binary forms, with or without modification, 10f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * are permitted provided that the following conditions are met: 11f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 12f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 1. Redistributions of source code must retain the above copyright notice, this 13f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * list of conditions and the following disclaimer. 14f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 15f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 2. Redistributions in binary form must reproduce the above copyright notice, 16f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * this list of conditions and the following disclaimer in the documentation 17f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * and/or other materials provided with the distribution. 18f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 19f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 3. Neither the name of the nanohttpd nor the names of its contributors 20f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * may be used to endorse or promote products derived from this software without 21f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * specific prior written permission. 22f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * 23f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 31f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 32f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * OF THE POSSIBILITY OF SUCH DAMAGE. 33f869fff37123ba876b5ee78de4144b1e844a2f50ritchie * #L% 34f869fff37123ba876b5ee78de4144b1e844a2f50ritchie */ 35f869fff37123ba876b5ee78de4144b1e844a2f50ritchie 368bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.ByteArrayOutputStream; 378bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.FileInputStream; 388bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.IOException; 398bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.InputStream; 408bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.PipedInputStream; 418bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport java.io.PipedOutputStream; 428bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 438bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.apache.http.HttpEntity; 448bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.apache.http.client.methods.CloseableHttpResponse; 458bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.apache.http.client.methods.HttpGet; 468bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.apache.http.impl.client.CloseableHttpClient; 478bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.apache.http.impl.client.HttpClients; 488bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.junit.AfterClass; 498bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.junit.Assert; 508bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchieimport org.junit.BeforeClass; 51f869fff37123ba876b5ee78de4144b1e844a2f50ritchieimport org.junit.Test; 52f869fff37123ba876b5ee78de4144b1e844a2f50ritchie 53709bd63d3d1d89e4bb35a2f721d3b2d07cec892dMatthieu Brouillardpublic class TestHttpServer extends AbstractTestHttpServer { 54f869fff37123ba876b5ee78de4144b1e844a2f50ritchie 558bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie private static PipedOutputStream stdIn; 568bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 578bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie private static Thread serverStartThread; 588bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 598bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie @BeforeClass 608bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public static void setUp() throws Exception { 618bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie stdIn = new PipedOutputStream(); 628bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie System.setIn(new PipedInputStream(stdIn)); 638bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie serverStartThread = new Thread(new Runnable() { 648bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 658bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie @Override 668bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public void run() { 678bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie String[] args = { 688bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "--host", 698bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "localhost", 708bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "--port", 718bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "9090", 728bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "--dir", 738bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie "src/test/resources" 748bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie }; 758bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie SimpleWebServer.main(args); 768bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 778bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie }); 788bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie serverStartThread.start(); 798bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie // give the server some tine to start. 808bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Thread.sleep(100); 818bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 828bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 838bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie @AfterClass 848bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public static void tearDown() throws Exception { 858bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie stdIn.write("\n\n".getBytes()); 86b1ab87229132a8291eff259cae48d1f6b0572c92ritchie serverStartThread.join(2000); 878bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertFalse(serverStartThread.isAlive()); 888bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 898bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 908bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie @Test 918bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public void doTest404() throws Exception { 928bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpClient httpclient = HttpClients.createDefault(); 938bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie HttpGet httpget = new HttpGet("http://localhost:9090/xxx/yyy.html"); 948bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpResponse response = httpclient.execute(httpget); 958bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertEquals(404, response.getStatusLine().getStatusCode()); 968bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 978bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 988bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 998bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie @Test 1008bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public void doPlugin() throws Exception { 1018bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpClient httpclient = HttpClients.createDefault(); 1028bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie HttpGet httpget = new HttpGet("http://localhost:9090/index.xml"); 1038bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpResponse response = httpclient.execute(httpget); 1048bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie String string = new String(readContents(response.getEntity()), "UTF-8"); 1058bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertEquals("<xml/>", string); 1068bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1078bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1088bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie httpget = new HttpGet("http://localhost:9090/testdir/testdir/different.xml"); 1098bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response = httpclient.execute(httpget); 1108bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie string = new String(readContents(response.getEntity()), "UTF-8"); 1118bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertEquals("<xml/>", string); 1128bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1138bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 1148bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 115f869fff37123ba876b5ee78de4144b1e844a2f50ritchie @Test 1168bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie public void doSomeBasicTest() throws Exception { 1178bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpClient httpclient = HttpClients.createDefault(); 1188bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie HttpGet httpget = new HttpGet("http://localhost:9090/testdir/test.html"); 1198bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie CloseableHttpResponse response = httpclient.execute(httpget); 1208bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie HttpEntity entity = response.getEntity(); 1218bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie String string = new String(readContents(entity), "UTF-8"); 1228bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertEquals("<html>\n<head>\n<title>dummy</title>\n</head>\n<body>\n\t<h1>it works</h1>\n</body>\n</html>", string); 1238bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1248bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1258bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie httpget = new HttpGet("http://localhost:9090/"); 1268bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response = httpclient.execute(httpget); 1278bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie entity = response.getEntity(); 1288bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie string = new String(readContents(entity), "UTF-8"); 1298bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertTrue(string.indexOf("testdir") > 0); 1308bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1318bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1328bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie httpget = new HttpGet("http://localhost:9090/testdir"); 1338bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response = httpclient.execute(httpget); 1348bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie entity = response.getEntity(); 1358bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie string = new String(readContents(entity), "UTF-8"); 1368bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertTrue(string.indexOf("test.html") > 0); 1378bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1388bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1398bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie httpget = new HttpGet("http://localhost:9090/testdir/testpdf.pdf"); 1408bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response = httpclient.execute(httpget); 1418bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie entity = response.getEntity(); 1428bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1438bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie byte[] actual = readContents(entity); 1448bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie byte[] expected = readContents(new FileInputStream("src/test/resources/testdir/testpdf.pdf")); 1458bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie Assert.assertArrayEquals(expected, actual); 1468bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie response.close(); 1478bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie 1488bb6bfb4e22b3bbe1648758a64ba1c0ad8a58856ritchie } 149f869fff37123ba876b5ee78de4144b1e844a2f50ritchie} 150