1d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchiepackage fi.iki.elonen.router;
2d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
3d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie/*
4d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * #%L
5d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * NanoHttpd nano application server
6d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * %%
7d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * Copyright (C) 2012 - 2015 nanohttpd
8d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * %%
9d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * Redistribution and use in source and binary forms, with or without modification,
10d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * are permitted provided that the following conditions are met:
11d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *
12d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * 1. Redistributions of source code must retain the above copyright notice, this
13d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *    list of conditions and the following disclaimer.
14d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *
15d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * 2. Redistributions in binary form must reproduce the above copyright notice,
16d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *    this list of conditions and the following disclaimer in the documentation
17d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *    and/or other materials provided with the distribution.
18d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *
19d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * 3. Neither the name of the nanohttpd nor the names of its contributors
20d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *    may be used to endorse or promote products derived from this software without
21d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *    specific prior written permission.
22d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie *
23d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * OF THE POSSIBILITY OF SUCH DAMAGE.
33d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie * #L%
34d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie */
35d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
36d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport java.io.ByteArrayOutputStream;
37d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport java.io.IOException;
38d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport java.io.InputStream;
39d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport java.io.PipedInputStream;
40d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport java.io.PipedOutputStream;
416a95983865c086f44853880e0454eff813bb8b45ritchieimport java.util.regex.Matcher;
426a95983865c086f44853880e0454eff813bb8b45ritchieimport java.util.regex.Pattern;
43d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
44d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.apache.http.HttpEntity;
45d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.apache.http.client.methods.CloseableHttpResponse;
46350ed420bc91cfa8b6f57478691777fd6499363britchieimport org.apache.http.client.methods.HttpDelete;
47d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.apache.http.client.methods.HttpGet;
48350ed420bc91cfa8b6f57478691777fd6499363britchieimport org.apache.http.client.methods.HttpPost;
49350ed420bc91cfa8b6f57478691777fd6499363britchieimport org.apache.http.client.methods.HttpPut;
50350ed420bc91cfa8b6f57478691777fd6499363britchieimport org.apache.http.client.methods.HttpTrace;
51d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.apache.http.impl.client.CloseableHttpClient;
52d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.apache.http.impl.client.HttpClients;
53d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.junit.AfterClass;
54d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.junit.Assert;
55d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.junit.BeforeClass;
56d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchieimport org.junit.Test;
57d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
582681f5f9a272a86cd75271747f29c8a2d98039c4ritchieimport fi.iki.elonen.NanoHTTPD;
59350ed420bc91cfa8b6f57478691777fd6499363britchieimport fi.iki.elonen.router.RouterNanoHTTPD.GeneralHandler;
60350ed420bc91cfa8b6f57478691777fd6499363britchieimport fi.iki.elonen.router.RouterNanoHTTPD.UriResource;
61350ed420bc91cfa8b6f57478691777fd6499363britchie
62d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchiepublic class TestNanolets {
63d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
64d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    private static PipedOutputStream stdIn;
65d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
66d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    private static Thread serverStartThread;
67d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
68d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    @BeforeClass
69d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    public static void setUp() throws Exception {
70d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        stdIn = new PipedOutputStream();
71d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        System.setIn(new PipedInputStream(stdIn));
72d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        serverStartThread = new Thread(new Runnable() {
73d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
74d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            @Override
75d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            public void run() {
76d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie                String[] args = {};
77d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie                AppNanolets.main(args);
78d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            }
79d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        });
80d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        serverStartThread.start();
81d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        // give the server some tine to start.
826a95983865c086f44853880e0454eff813bb8b45ritchie        Thread.sleep(200);
836a95983865c086f44853880e0454eff813bb8b45ritchie    }
846a95983865c086f44853880e0454eff813bb8b45ritchie
856a95983865c086f44853880e0454eff813bb8b45ritchie    public static void main(String[] args) {
866a95983865c086f44853880e0454eff813bb8b45ritchie        {
876a95983865c086f44853880e0454eff813bb8b45ritchie            String uri = "def";
886a95983865c086f44853880e0454eff813bb8b45ritchie            Pattern.compile("([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
896a95983865c086f44853880e0454eff813bb8b45ritchie            Pattern URI_PATTERN = Pattern.compile("([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
906a95983865c086f44853880e0454eff813bb8b45ritchie            System.out.println(URI_PATTERN.matcher(uri).matches());
916a95983865c086f44853880e0454eff813bb8b45ritchie        }
926a95983865c086f44853880e0454eff813bb8b45ritchie
936a95983865c086f44853880e0454eff813bb8b45ritchie        String uri = "photos/abc/def";
946a95983865c086f44853880e0454eff813bb8b45ritchie        Pattern URI_PATTERN = Pattern.compile("photos/([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)/([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
956a95983865c086f44853880e0454eff813bb8b45ritchie        Matcher matcher = URI_PATTERN.matcher(uri);
966a95983865c086f44853880e0454eff813bb8b45ritchie        System.out.println("--------------->" + "/" + uri);
976a95983865c086f44853880e0454eff813bb8b45ritchie        while (matcher.matches()) {
986a95983865c086f44853880e0454eff813bb8b45ritchie
996a95983865c086f44853880e0454eff813bb8b45ritchie            System.out.println(matcher.group());
1006a95983865c086f44853880e0454eff813bb8b45ritchie        }
1016a95983865c086f44853880e0454eff813bb8b45ritchie        // for (int index = 0; index < matcher.groupCount(); index++) {
1026a95983865c086f44853880e0454eff813bb8b45ritchie        // System.out.println(matcher.group());
1036a95983865c086f44853880e0454eff813bb8b45ritchie        // }
104d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    }
105d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
106d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    @Test
107350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doSomeBasicMethodTest() throws Exception {
108d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        CloseableHttpClient httpclient = HttpClients.createDefault();
109350ed420bc91cfa8b6f57478691777fd6499363britchie
110d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla");
111d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        CloseableHttpResponse response = httpclient.execute(httpget);
112d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        HttpEntity entity = response.getEntity();
113d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        String string = new String(readContents(entity), "UTF-8");
114d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        Assert.assertEquals(
115d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie                "<html><body>User handler. Method: GET<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
116d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        response.close();
117350ed420bc91cfa8b6f57478691777fd6499363britchie
118350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla");
119350ed420bc91cfa8b6f57478691777fd6499363britchie        response = httpclient.execute(httppost);
120350ed420bc91cfa8b6f57478691777fd6499363britchie        entity = response.getEntity();
121350ed420bc91cfa8b6f57478691777fd6499363britchie        string = new String(readContents(entity), "UTF-8");
122350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals(
123350ed420bc91cfa8b6f57478691777fd6499363britchie                "<html><body>User handler. Method: POST<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
124350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
125350ed420bc91cfa8b6f57478691777fd6499363britchie
126350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla");
127350ed420bc91cfa8b6f57478691777fd6499363britchie        response = httpclient.execute(httpgput);
128350ed420bc91cfa8b6f57478691777fd6499363britchie        entity = response.getEntity();
129350ed420bc91cfa8b6f57478691777fd6499363britchie        string = new String(readContents(entity), "UTF-8");
130350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals(
131350ed420bc91cfa8b6f57478691777fd6499363britchie                "<html><body>User handler. Method: PUT<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
132350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
133350ed420bc91cfa8b6f57478691777fd6499363britchie
134350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla");
135350ed420bc91cfa8b6f57478691777fd6499363britchie        response = httpclient.execute(httpdelete);
136350ed420bc91cfa8b6f57478691777fd6499363britchie        entity = response.getEntity();
137350ed420bc91cfa8b6f57478691777fd6499363britchie        string = new String(readContents(entity), "UTF-8");
138350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals(
139350ed420bc91cfa8b6f57478691777fd6499363britchie                "<html><body>User handler. Method: DELETE<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
140350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
141350ed420bc91cfa8b6f57478691777fd6499363britchie    }
142350ed420bc91cfa8b6f57478691777fd6499363britchie
143350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
144350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doNonRouterRequest() throws Exception {
145350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
146350ed420bc91cfa8b6f57478691777fd6499363britchie
147350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/test");
148350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
149350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
150350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
151350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("Return: java.lang.String.toString() -> ", string);
152350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
153350ed420bc91cfa8b6f57478691777fd6499363britchie    }
154350ed420bc91cfa8b6f57478691777fd6499363britchie
155350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
156350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doExceptionRequest() throws Exception {
157350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
158350ed420bc91cfa8b6f57478691777fd6499363britchie
159350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/interface");
160350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
161350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
162350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
163350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("Error: java.lang.InstantiationException : fi.iki.elonen.router.RouterNanoHTTPD$UriResponder", string);
164350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
165350ed420bc91cfa8b6f57478691777fd6499363britchie    }
166350ed420bc91cfa8b6f57478691777fd6499363britchie
167350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
168350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doDeletedRoute() throws Exception {
169350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
170350ed420bc91cfa8b6f57478691777fd6499363britchie
171350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/toBeDeleted");
172350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
173350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
174350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
175350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h3>Error 404: the requested page doesn't exist.</h3></body></html>", string);
176350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
177350ed420bc91cfa8b6f57478691777fd6499363britchie    }
178350ed420bc91cfa8b6f57478691777fd6499363britchie
179350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
180350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doUriSelection1() throws Exception {
181350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
182350ed420bc91cfa8b6f57478691777fd6499363britchie
183350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/user/help");
184350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
185350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
186350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
187350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h1>Url: /user/help</h1><br><p>no params in url</p><br>", string);
188350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
189350ed420bc91cfa8b6f57478691777fd6499363britchie    }
190350ed420bc91cfa8b6f57478691777fd6499363britchie
191350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
192350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doStreamOfData() throws Exception {
193350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
194350ed420bc91cfa8b6f57478691777fd6499363britchie
195350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/stream");
196350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
197350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
198350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
199350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("a stream of data ;-)", string);
200350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
201350ed420bc91cfa8b6f57478691777fd6499363britchie    }
202350ed420bc91cfa8b6f57478691777fd6499363britchie
203350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test(expected = IllegalStateException.class)
204350ed420bc91cfa8b6f57478691777fd6499363britchie    public void illegalMethod1() throws Exception {
205350ed420bc91cfa8b6f57478691777fd6499363britchie        new AppNanolets.UserHandler().getData();
206350ed420bc91cfa8b6f57478691777fd6499363britchie    }
207350ed420bc91cfa8b6f57478691777fd6499363britchie
208350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test(expected = IllegalStateException.class)
209350ed420bc91cfa8b6f57478691777fd6499363britchie    public void illegalMethod2() throws Exception {
210350ed420bc91cfa8b6f57478691777fd6499363britchie        new RouterNanoHTTPD.GeneralHandler().getText();
211350ed420bc91cfa8b6f57478691777fd6499363britchie    }
212350ed420bc91cfa8b6f57478691777fd6499363britchie
2136a95983865c086f44853880e0454eff813bb8b45ritchie    @Test(expected = IllegalStateException.class)
2146a95983865c086f44853880e0454eff813bb8b45ritchie    public void illegalMethod3() throws Exception {
2156a95983865c086f44853880e0454eff813bb8b45ritchie        new RouterNanoHTTPD.StaticPageHandler().getText();
2166a95983865c086f44853880e0454eff813bb8b45ritchie    }
2176a95983865c086f44853880e0454eff813bb8b45ritchie
2186a95983865c086f44853880e0454eff813bb8b45ritchie    @Test(expected = IllegalStateException.class)
2196a95983865c086f44853880e0454eff813bb8b45ritchie    public void illegalMethod4() throws Exception {
2206a95983865c086f44853880e0454eff813bb8b45ritchie        new RouterNanoHTTPD.StaticPageHandler().getMimeType();
2216a95983865c086f44853880e0454eff813bb8b45ritchie    }
2226a95983865c086f44853880e0454eff813bb8b45ritchie
2236a95983865c086f44853880e0454eff813bb8b45ritchie    @Test(expected = ClassCastException.class)
2246a95983865c086f44853880e0454eff813bb8b45ritchie    public void checkIniParameter1() throws Exception {
2256a95983865c086f44853880e0454eff813bb8b45ritchie        new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(String.class);
2266a95983865c086f44853880e0454eff813bb8b45ritchie        new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(Integer.class);
2276a95983865c086f44853880e0454eff813bb8b45ritchie    }
2286a95983865c086f44853880e0454eff813bb8b45ritchie
2296a95983865c086f44853880e0454eff813bb8b45ritchie    @Test
2306a95983865c086f44853880e0454eff813bb8b45ritchie    public void checkIniParameter2() throws Exception {
2316a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("init", new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(String.class));
2326a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertNull(new RouterNanoHTTPD.UriResource("browse", 100, null).initParameter(String.class));
2336a95983865c086f44853880e0454eff813bb8b45ritchie    }
2346a95983865c086f44853880e0454eff813bb8b45ritchie
235350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
236350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doGeneralParams() throws Exception {
237350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
238350ed420bc91cfa8b6f57478691777fd6499363britchie
239350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3¶m4=value4");
240350ed420bc91cfa8b6f57478691777fd6499363britchie
241350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
242350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
243350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
244350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h1>Url: /general/value1/value2</h1><br><p>Param 'param3' = value3</p><p>Param 'param4' = value4</p>", string);
245350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
246350ed420bc91cfa8b6f57478691777fd6499363britchie    }
247350ed420bc91cfa8b6f57478691777fd6499363britchie
248350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
249350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doIndexHandler() throws Exception {
250350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
251350ed420bc91cfa8b6f57478691777fd6499363britchie
252350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/index.html");
253350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
254350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
255350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
256350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h2>Hello world!</h3></body></html>", string);
257350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
258350ed420bc91cfa8b6f57478691777fd6499363britchie    }
259350ed420bc91cfa8b6f57478691777fd6499363britchie
260350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
261350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doMissingHandler() throws Exception {
262350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
263350ed420bc91cfa8b6f57478691777fd6499363britchie
264350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpGet httpget = new HttpGet("http://localhost:9090/photos/abc/def");
265350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httpget);
266350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
267350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
268350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h2>The uri is mapped in the router, but no handler is specified. <br> Status: Not implemented!</h3></body></html>", string);
269350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
270350ed420bc91cfa8b6f57478691777fd6499363britchie    }
271350ed420bc91cfa8b6f57478691777fd6499363britchie
272350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
273350ed420bc91cfa8b6f57478691777fd6499363britchie    public void uriToString() throws Exception {
274350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals(//
2756a95983865c086f44853880e0454eff813bb8b45ritchie                "UrlResource{uri='photos/:customer_id/:photo_id', urlParts=[customer_id, photo_id]}",//
2766a95983865c086f44853880e0454eff813bb8b45ritchie                new UriResource("/photos/:customer_id/:photo_id", 100, GeneralHandler.class).toString());
277350ed420bc91cfa8b6f57478691777fd6499363britchie    }
278350ed420bc91cfa8b6f57478691777fd6499363britchie
279350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
280350ed420bc91cfa8b6f57478691777fd6499363britchie    public void doOtherMethod() throws Exception {
281350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpClient httpclient = HttpClients.createDefault();
282350ed420bc91cfa8b6f57478691777fd6499363britchie
283350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpTrace httphead = new HttpTrace("http://localhost:9090/index.html");
284350ed420bc91cfa8b6f57478691777fd6499363britchie        CloseableHttpResponse response = httpclient.execute(httphead);
285350ed420bc91cfa8b6f57478691777fd6499363britchie        HttpEntity entity = response.getEntity();
286350ed420bc91cfa8b6f57478691777fd6499363britchie        String string = new String(readContents(entity), "UTF-8");
287350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("<html><body><h2>Hello world!</h3></body></html>", string);
288350ed420bc91cfa8b6f57478691777fd6499363britchie        response.close();
289350ed420bc91cfa8b6f57478691777fd6499363britchie    }
290350ed420bc91cfa8b6f57478691777fd6499363britchie
291350ed420bc91cfa8b6f57478691777fd6499363britchie    @Test
292350ed420bc91cfa8b6f57478691777fd6499363britchie    public void normalize() throws Exception {
293350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertNull(RouterNanoHTTPD.normalizeUri(null));
294350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("", RouterNanoHTTPD.normalizeUri("/"));
295350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy"));
296350ed420bc91cfa8b6f57478691777fd6499363britchie        Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy/"));
297d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    }
298d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
299d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    private byte[] readContents(HttpEntity entity) throws IOException {
300d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        InputStream instream = entity.getContent();
301d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        return readContents(instream);
302d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    }
303d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
304d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    private byte[] readContents(InputStream instream) throws IOException {
305d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        byte[] bytes;
306d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        ByteArrayOutputStream out = new ByteArrayOutputStream();
307d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
308d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        try {
309d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            byte[] buffer = new byte[1024];
310d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            int count;
311d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            while ((count = instream.read(buffer)) >= 0) {
312d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie                out.write(buffer, 0, count);
313d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            }
314d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            bytes = out.toByteArray();
315d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        } finally {
316d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie            instream.close();
317d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        }
318d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        return bytes;
319d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    }
320d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
3216a95983865c086f44853880e0454eff813bb8b45ritchie    @Test
3226a95983865c086f44853880e0454eff813bb8b45ritchie    public void staticFiles() throws Exception {
3236a95983865c086f44853880e0454eff813bb8b45ritchie        CloseableHttpClient httpclient = HttpClients.createDefault();
3246a95983865c086f44853880e0454eff813bb8b45ritchie
3256a95983865c086f44853880e0454eff813bb8b45ritchie        HttpTrace httphead = new HttpTrace("http://localhost:9090/browse/blabla.html");
3266a95983865c086f44853880e0454eff813bb8b45ritchie        CloseableHttpResponse response = httpclient.execute(httphead);
3276a95983865c086f44853880e0454eff813bb8b45ritchie        HttpEntity entity = response.getEntity();
3286a95983865c086f44853880e0454eff813bb8b45ritchie        String string = new String(readContents(entity), "UTF-8");
3296a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("<html><body><h3>just a page</h3></body></html>", string);
3306a95983865c086f44853880e0454eff813bb8b45ritchie        response.close();
3316a95983865c086f44853880e0454eff813bb8b45ritchie
3326a95983865c086f44853880e0454eff813bb8b45ritchie        httphead = new HttpTrace("http://localhost:9090/browse/dir/blabla.html");
3336a95983865c086f44853880e0454eff813bb8b45ritchie        response = httpclient.execute(httphead);
3346a95983865c086f44853880e0454eff813bb8b45ritchie        entity = response.getEntity();
3356a95983865c086f44853880e0454eff813bb8b45ritchie        string = new String(readContents(entity), "UTF-8");
3366a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("<html><body><h3>just an other page</h3></body></html>", string);
3376a95983865c086f44853880e0454eff813bb8b45ritchie        response.close();
3386a95983865c086f44853880e0454eff813bb8b45ritchie
3396a95983865c086f44853880e0454eff813bb8b45ritchie        httphead = new HttpTrace("http://localhost:9090/browse/dir/nanohttpd_logo.png");
3406a95983865c086f44853880e0454eff813bb8b45ritchie        response = httpclient.execute(httphead);
3416a95983865c086f44853880e0454eff813bb8b45ritchie        entity = response.getEntity();
3426a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("image/png", entity.getContentType().getValue());
3436a95983865c086f44853880e0454eff813bb8b45ritchie        response.close();
3446a95983865c086f44853880e0454eff813bb8b45ritchie
3456a95983865c086f44853880e0454eff813bb8b45ritchie        httphead = new HttpTrace("http://localhost:9090/browse/dir/xxx.html");
3466a95983865c086f44853880e0454eff813bb8b45ritchie        response = httpclient.execute(httphead);
3476a95983865c086f44853880e0454eff813bb8b45ritchie        entity = response.getEntity();
3486a95983865c086f44853880e0454eff813bb8b45ritchie        string = new String(readContents(entity), "UTF-8");
3496a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("<html><body><h3>Error 404: the requested page doesn't exist.</h3></body></html>", string);
3506a95983865c086f44853880e0454eff813bb8b45ritchie        response.close();
3516a95983865c086f44853880e0454eff813bb8b45ritchie
3526a95983865c086f44853880e0454eff813bb8b45ritchie        httphead = new HttpTrace("http://localhost:9090/browse/dir/");
3536a95983865c086f44853880e0454eff813bb8b45ritchie        response = httpclient.execute(httphead);
3546a95983865c086f44853880e0454eff813bb8b45ritchie        entity = response.getEntity();
3556a95983865c086f44853880e0454eff813bb8b45ritchie        string = new String(readContents(entity), "UTF-8");
3566a95983865c086f44853880e0454eff813bb8b45ritchie        Assert.assertEquals("<html><body><h3>just an index page</h3></body></html>", string);
3576a95983865c086f44853880e0454eff813bb8b45ritchie        response.close();
3582681f5f9a272a86cd75271747f29c8a2d98039c4ritchie
3592681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        httphead = new HttpTrace("http://localhost:9090/browse/exception.html");
3602681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        response = httpclient.execute(httphead);
3612681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        Assert.assertEquals(NanoHTTPD.Response.Status.REQUEST_TIMEOUT.getRequestStatus(), response.getStatusLine().getStatusCode());
3622681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        entity = response.getEntity();
3632681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        string = new String(readContents(entity), "UTF-8");
3642681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        Assert.assertEquals("", string);
3652681f5f9a272a86cd75271747f29c8a2d98039c4ritchie        response.close();
3666a95983865c086f44853880e0454eff813bb8b45ritchie    }
3676a95983865c086f44853880e0454eff813bb8b45ritchie
368d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    @AfterClass
369d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    public static void tearDown() throws Exception {
370d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        stdIn.write("\n\n".getBytes());
371d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        serverStartThread.join(2000);
372d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie        Assert.assertFalse(serverStartThread.isAlive());
373d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie    }
374d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie
375d66d5b8916ccfb23f894ff1a3936e3f7462701f1ritchie}
376