aboutsummaryrefslogtreecommitdiff
path: root/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java
blob: 7b1ae569f097836d8ee21689138c999c8d4c21f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package fi.iki.elonen.router;

/*
 * #%L
 * NanoHttpd nano application server
 * %%
 * Copyright (C) 2012 - 2015 nanohttpd
 * %%
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the nanohttpd nor the names of its contributors
 *    may be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.router.RouterNanoHTTPD.GeneralHandler;
import fi.iki.elonen.router.RouterNanoHTTPD.UriResource;

public class TestNanolets {

    private static PipedOutputStream stdIn;

    private static Thread serverStartThread;

    @BeforeClass
    public static void setUp() throws Exception {
        stdIn = new PipedOutputStream();
        System.setIn(new PipedInputStream(stdIn));
        serverStartThread = new Thread(new Runnable() {

            @Override
            public void run() {
                String[] args = {};
                AppNanolets.main(args);
            }
        });
        serverStartThread.start();
        // give the server some tine to start.
        Thread.sleep(200);
    }

    public static void main(String[] args) {
        {
            String uri = "def";
            Pattern.compile("([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
            Pattern URI_PATTERN = Pattern.compile("([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
            System.out.println(URI_PATTERN.matcher(uri).matches());
        }

        String uri = "photos/abc/def";
        Pattern URI_PATTERN = Pattern.compile("photos/([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)/([A-Za-z0-9\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]+)");
        Matcher matcher = URI_PATTERN.matcher(uri);
        System.out.println("--------------->" + "/" + uri);
        while (matcher.matches()) {

            System.out.println(matcher.group());
        }
        // for (int index = 0; index < matcher.groupCount(); index++) {
        // System.out.println(matcher.group());
        // }
    }

    @Test
    public void doSomeBasicMethodTest() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals(
                "<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);
        response.close();

        HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla");
        response = httpclient.execute(httppost);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals(
                "<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);
        response.close();

        HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla");
        response = httpclient.execute(httpgput);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals(
                "<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);
        response.close();

        HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla");
        response = httpclient.execute(httpdelete);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals(
                "<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);
        response.close();
    }

    @Test
    public void doNonRouterRequest() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/test");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("Return: java.lang.String.toString() -> ", string);
        response.close();
    }

    @Test
    public void doExceptionRequest() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/interface");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("Error: java.lang.InstantiationException : fi.iki.elonen.router.RouterNanoHTTPD$UriResponder", string);
        response.close();
    }

    @Test
    public void doDeletedRoute() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/toBeDeleted");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h3>Error 404: the requested page doesn't exist.</h3></body></html>", string);
        response.close();
    }

    @Test
    public void doUriSelection1() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/user/help");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h1>Url: /user/help</h1><br><p>no params in url</p><br>", string);
        response.close();
    }

    @Test
    public void doStreamOfData() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/stream");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("a stream of data ;-)", string);
        response.close();
    }

    @Test(expected = IllegalStateException.class)
    public void illegalMethod1() throws Exception {
        new AppNanolets.UserHandler().getData();
    }

    @Test(expected = IllegalStateException.class)
    public void illegalMethod2() throws Exception {
        new RouterNanoHTTPD.GeneralHandler().getText();
    }

    @Test(expected = IllegalStateException.class)
    public void illegalMethod3() throws Exception {
        new RouterNanoHTTPD.StaticPageHandler().getText();
    }

    @Test(expected = IllegalStateException.class)
    public void illegalMethod4() throws Exception {
        new RouterNanoHTTPD.StaticPageHandler().getMimeType();
    }

    @Test(expected = ClassCastException.class)
    public void checkIniParameter1() throws Exception {
        new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(String.class);
        new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(Integer.class);
    }

    @Test
    public void checkIniParameter2() throws Exception {
        Assert.assertEquals("init", new RouterNanoHTTPD.UriResource("browse", 100, null, "init").initParameter(String.class));
        Assert.assertNull(new RouterNanoHTTPD.UriResource("browse", 100, null).initParameter(String.class));
    }

    @Test
    public void doGeneralParams() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3&param4=value4");

        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h1>Url: /general/value1/value2</h1><br><p>Param 'param3' = value3</p><p>Param 'param4' = value4</p>", string);
        response.close();
    }

    @Test
    public void doIndexHandler() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/index.html");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h2>Hello world!</h3></body></html>", string);
        response.close();
    }

    @Test
    public void doMissingHandler() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet httpget = new HttpGet("http://localhost:9090/photos/abc/def");
        CloseableHttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        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);
        response.close();
    }

    @Test
    public void uriToString() throws Exception {
        Assert.assertEquals(//
                "UrlResource{uri='photos/:customer_id/:photo_id', urlParts=[customer_id, photo_id]}",//
                new UriResource("/photos/:customer_id/:photo_id", 100, GeneralHandler.class).toString());
    }

    @Test
    public void doOtherMethod() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpTrace httphead = new HttpTrace("http://localhost:9090/index.html");
        CloseableHttpResponse response = httpclient.execute(httphead);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h2>Hello world!</h3></body></html>", string);
        response.close();
    }

    @Test
    public void normalize() throws Exception {
        Assert.assertNull(RouterNanoHTTPD.normalizeUri(null));
        Assert.assertEquals("", RouterNanoHTTPD.normalizeUri("/"));
        Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy"));
        Assert.assertEquals("xxx/yyy", RouterNanoHTTPD.normalizeUri("/xxx/yyy/"));
    }

    private byte[] readContents(HttpEntity entity) throws IOException {
        InputStream instream = entity.getContent();
        return readContents(instream);
    }

    private byte[] readContents(InputStream instream) throws IOException {
        byte[] bytes;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            byte[] buffer = new byte[1024];
            int count;
            while ((count = instream.read(buffer)) >= 0) {
                out.write(buffer, 0, count);
            }
            bytes = out.toByteArray();
        } finally {
            instream.close();
        }
        return bytes;
    }

    @Test
    public void staticFiles() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpTrace httphead = new HttpTrace("http://localhost:9090/browse/blabla.html");
        CloseableHttpResponse response = httpclient.execute(httphead);
        HttpEntity entity = response.getEntity();
        String string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h3>just a page</h3></body></html>", string);
        response.close();

        httphead = new HttpTrace("http://localhost:9090/browse/dir/blabla.html");
        response = httpclient.execute(httphead);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h3>just an other page</h3></body></html>", string);
        response.close();

        httphead = new HttpTrace("http://localhost:9090/browse/dir/nanohttpd_logo.png");
        response = httpclient.execute(httphead);
        entity = response.getEntity();
        Assert.assertEquals("image/png", entity.getContentType().getValue());
        response.close();

        httphead = new HttpTrace("http://localhost:9090/browse/dir/xxx.html");
        response = httpclient.execute(httphead);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h3>Error 404: the requested page doesn't exist.</h3></body></html>", string);
        response.close();

        httphead = new HttpTrace("http://localhost:9090/browse/dir/");
        response = httpclient.execute(httphead);
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("<html><body><h3>just an index page</h3></body></html>", string);
        response.close();

        httphead = new HttpTrace("http://localhost:9090/browse/exception.html");
        response = httpclient.execute(httphead);
        Assert.assertEquals(NanoHTTPD.Response.Status.REQUEST_TIMEOUT.getRequestStatus(), response.getStatusLine().getStatusCode());
        entity = response.getEntity();
        string = new String(readContents(entity), "UTF-8");
        Assert.assertEquals("", string);
        response.close();
    }

    @AfterClass
    public static void tearDown() throws Exception {
        stdIn.write("\n\n".getBytes());
        serverStartThread.join(2000);
        Assert.assertFalse(serverStartThread.isAlive());
    }

}