• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package fi.iki.elonen;
2 
3 /*
4  * #%L
5  * NanoHttpd-Webserver
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 java.io.ByteArrayOutputStream;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.PipedInputStream;
41 import java.io.PipedOutputStream;
42 
43 import org.apache.http.HttpEntity;
44 import org.apache.http.client.methods.CloseableHttpResponse;
45 import org.apache.http.client.methods.HttpGet;
46 import org.apache.http.impl.client.CloseableHttpClient;
47 import org.apache.http.impl.client.HttpClients;
48 import org.junit.AfterClass;
49 import org.junit.Assert;
50 import org.junit.BeforeClass;
51 import org.junit.Test;
52 
53 public class TestHttpServer extends AbstractTestHttpServer {
54 
55     private static PipedOutputStream stdIn;
56 
57     private static Thread serverStartThread;
58 
59     @BeforeClass
setUp()60     public static void setUp() throws Exception {
61         stdIn = new PipedOutputStream();
62         System.setIn(new PipedInputStream(stdIn));
63         serverStartThread = new Thread(new Runnable() {
64 
65             @Override
66             public void run() {
67                 String[] args = {
68                     "--host",
69                     "localhost",
70                     "--port",
71                     "9090",
72                     "--dir",
73                     "src/test/resources"
74                 };
75                 SimpleWebServer.main(args);
76             }
77         });
78         serverStartThread.start();
79         // give the server some tine to start.
80         Thread.sleep(100);
81     }
82 
83     @AfterClass
tearDown()84     public static void tearDown() throws Exception {
85         stdIn.write("\n\n".getBytes());
86         serverStartThread.join(2000);
87         Assert.assertFalse(serverStartThread.isAlive());
88     }
89 
90     @Test
doTest404()91     public void doTest404() throws Exception {
92         CloseableHttpClient httpclient = HttpClients.createDefault();
93         HttpGet httpget = new HttpGet("http://localhost:9090/xxx/yyy.html");
94         CloseableHttpResponse response = httpclient.execute(httpget);
95         Assert.assertEquals(404, response.getStatusLine().getStatusCode());
96         response.close();
97     }
98 
99     @Test
doPlugin()100     public void doPlugin() throws Exception {
101         CloseableHttpClient httpclient = HttpClients.createDefault();
102         HttpGet httpget = new HttpGet("http://localhost:9090/index.xml");
103         CloseableHttpResponse response = httpclient.execute(httpget);
104         String string = new String(readContents(response.getEntity()), "UTF-8");
105         Assert.assertEquals("<xml/>", string);
106         response.close();
107 
108         httpget = new HttpGet("http://localhost:9090/testdir/testdir/different.xml");
109         response = httpclient.execute(httpget);
110         string = new String(readContents(response.getEntity()), "UTF-8");
111         Assert.assertEquals("<xml/>", string);
112         response.close();
113     }
114 
115     @Test
doSomeBasicTest()116     public void doSomeBasicTest() throws Exception {
117         CloseableHttpClient httpclient = HttpClients.createDefault();
118         HttpGet httpget = new HttpGet("http://localhost:9090/testdir/test.html");
119         CloseableHttpResponse response = httpclient.execute(httpget);
120         HttpEntity entity = response.getEntity();
121         String string = new String(readContents(entity), "UTF-8");
122         Assert.assertEquals("<html>\n<head>\n<title>dummy</title>\n</head>\n<body>\n\t<h1>it works</h1>\n</body>\n</html>", string);
123         response.close();
124 
125         httpget = new HttpGet("http://localhost:9090/");
126         response = httpclient.execute(httpget);
127         entity = response.getEntity();
128         string = new String(readContents(entity), "UTF-8");
129         Assert.assertTrue(string.indexOf("testdir") > 0);
130         response.close();
131 
132         httpget = new HttpGet("http://localhost:9090/testdir");
133         response = httpclient.execute(httpget);
134         entity = response.getEntity();
135         string = new String(readContents(entity), "UTF-8");
136         Assert.assertTrue(string.indexOf("test.html") > 0);
137         response.close();
138 
139         httpget = new HttpGet("http://localhost:9090/testdir/testpdf.pdf");
140         response = httpclient.execute(httpget);
141         entity = response.getEntity();
142 
143         byte[] actual = readContents(entity);
144         byte[] expected = readContents(new FileInputStream("src/test/resources/testdir/testpdf.pdf"));
145         Assert.assertArrayEquals(expected, actual);
146         response.close();
147 
148     }
149 }
150