diff options
author | ritchie <ritchie@gmx.at> | 2015-09-12 07:45:31 +0200 |
---|---|---|
committer | ritchie <ritchie@gmx.at> | 2015-09-12 07:45:31 +0200 |
commit | 350ed420bc91cfa8b6f57478691777fd6499363b (patch) | |
tree | 6f74b3c31412b8360dfdd3c4c1009c981c515c57 /nanolets/src/test/java/fi/iki/elonen | |
parent | d66d5b8916ccfb23f894ff1a3936e3f7462701f1 (diff) | |
download | nanohttpd-350ed420bc91cfa8b6f57478691777fd6499363b.tar.gz |
unit tests for nanolets #214
Diffstat (limited to 'nanolets/src/test/java/fi/iki/elonen')
-rw-r--r-- | nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java | 28 | ||||
-rw-r--r-- | nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java | 169 |
2 files changed, 196 insertions, 1 deletions
diff --git a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java index f69a611..b2ae4ed 100644 --- a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java +++ b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java @@ -41,9 +41,12 @@ package fi.iki.elonen.router; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Map; import fi.iki.elonen.NanoHTTPD; +import fi.iki.elonen.NanoHTTPD.Response.IStatus; +import fi.iki.elonen.NanoHTTPD.Response.Status; import fi.iki.elonen.util.ServerRunner; public class AppNanolets extends RouterNanoHTTPD { @@ -95,6 +98,25 @@ public class AppNanolets extends RouterNanoHTTPD { } + static public class StreamUrl extends DefaultStreamHandler { + + @Override + public String getMimeType() { + return "text/plain"; + } + + @Override + public IStatus getStatus() { + return Status.OK; + } + + @Override + public InputStream getData() { + return new ByteArrayInputStream("a stream of data ;-)".getBytes()); + } + + } + /** * Create the server instance */ @@ -115,8 +137,14 @@ public class AppNanolets extends RouterNanoHTTPD { addRoute("/user", UserHandler.class); addRoute("/user/:id", UserHandler.class); addRoute("/user/help", GeneralHandler.class); + addRoute("/general/:param1/:param2", GeneralHandler.class); addRoute("/photos/:customer_id/:photo_id", null); addRoute("/test", String.class); + addRoute("/interface", UriResponder.class); // this will cause an error + // when called + addRoute("/toBeDeleted", String.class); + removeRoute("/toBeDeleted"); + addRoute("/stream", StreamUrl.class); } /** diff --git a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java index 0d35868..bd360da 100644 --- a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java +++ b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java @@ -41,7 +41,12 @@ import java.io.PipedOutputStream; 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.HttpHead; +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; @@ -49,6 +54,9 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import fi.iki.elonen.router.RouterNanoHTTPD.GeneralHandler; +import fi.iki.elonen.router.RouterNanoHTTPD.UriResource; + public class TestNanolets { private static PipedOutputStream stdIn; @@ -73,8 +81,9 @@ public class TestNanolets { } @Test - public void doSomeBasicTest() throws Exception { + 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(); @@ -82,6 +91,164 @@ public class TestNanolets { Assert.assertEquals( "<html><body>User handler. Method: GET<br><h1>Uri parameters:</h1><div> Param: id 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 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 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 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 + public void doGeneralParams() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + + HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3¶m4=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{hasParameters=true, uriParamsCount=2, uri='photos/:customer_id/:photo_id', urlParts=[UriPart{name='photos', isParam=false}, UriPart{name='customer_id', isParam=true}, UriPart{name='photo_id', isParam=true}]}",// + new UriResource("/photos/:customer_id/:photo_id", 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 { |