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 | |
parent | d66d5b8916ccfb23f894ff1a3936e3f7462701f1 (diff) | |
download | nanohttpd-350ed420bc91cfa8b6f57478691777fd6499363b.tar.gz |
unit tests for nanolets #214
3 files changed, 221 insertions, 25 deletions
diff --git a/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java index f347741..82dca8b 100644 --- a/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java +++ b/nanolets/src/main/java/fi/iki/elonen/router/RouterNanoHTTPD.java @@ -36,6 +36,7 @@ package fi.iki.elonen.router; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Level; @@ -223,21 +224,17 @@ public class RouterNanoHTTPD extends NanoHTTPD { } } - public static class UriUtils { - - public static String normalizeUri(String value) { - if (value == null) { - return value; - } - if (value.startsWith("/")) { - value = value.substring(1); - } - if (value.endsWith("/")) { - value = value.substring(0, value.length() - 1); - } + public static String normalizeUri(String value) { + if (value == null) { return value; - } + if (value.startsWith("/")) { + value = value.substring(1); + } + if (value.endsWith("/")) { + value = value.substring(0, value.length() - 1); + } + return value; } @@ -254,7 +251,9 @@ public class RouterNanoHTTPD extends NanoHTTPD { @Override public String toString() { - return "UriPart{name='" + name + '\'' + ", isParam=" + isParam + '}'; + return new StringBuilder("UriPart{name='").append(name)// + .append("\', isParam=").append(isParam)// + .append('}').toString(); } public boolean isParam() { @@ -284,7 +283,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { this.handler = handler; uriParamsCount = 0; if (uri != null) { - this.uri = UriUtils.normalizeUri(uri); + this.uri = normalizeUri(uri); parse(); } } @@ -334,15 +333,11 @@ public class RouterNanoHTTPD extends NanoHTTPD { .append(object)// .toString()); } - } catch (InstantiationException e) { - error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); - LOG.log(Level.SEVERE, error, e); - } catch (IllegalAccessException e) { + } catch (Exception e) { error = "Error: " + e.getClass().getName() + " : " + e.getMessage(); LOG.log(Level.SEVERE, error, e); } } - return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", error); } @@ -397,7 +392,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { * @return */ public UriResource matchUrl(String url) { - String work = UriUtils.normalizeUri(url); + String work = normalizeUri(url); String[] parts = work.split("/"); List<UriResource> resultList = new ArrayList<UriResource>(); for (UriResource u : mappings) { @@ -468,8 +463,14 @@ public class RouterNanoHTTPD extends NanoHTTPD { } public void removeRoute(String url) { - if (mappings.contains(url)) { - mappings.remove(url); + String uriToDelete = normalizeUri(url); + Iterator<UriResource> iter = mappings.iterator(); + while (iter.hasNext()) { + UriResource uriResource = iter.next(); + if (uriToDelete.equals(uriResource.getUri())) { + iter.remove(); + break; + } } } @@ -493,7 +494,7 @@ public class RouterNanoHTTPD extends NanoHTTPD { if (route.getUri() == null) { return result; } - String workUri = UriUtils.normalizeUri(uri); + String workUri = normalizeUri(uri); String[] parts = workUri.split("/"); for (int i = 0; i < parts.length; i++) { UriPart currentPart = route.getUriParts().get(i); 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 { |