aboutsummaryrefslogtreecommitdiff
path: root/nanolets/src/test
diff options
context:
space:
mode:
authorritchie <ritchie@gmx.at>2015-09-26 09:35:51 +0200
committerritchie <ritchie@gmx.at>2015-09-26 09:35:51 +0200
commit6a95983865c086f44853880e0454eff813bb8b45 (patch)
treeffcfaeaa4264dc11b517ae1e292c2f7af8c575b5 /nanolets/src/test
parent34ea556fbb94657ff8be493b937f5e5e75ed0f28 (diff)
downloadnanohttpd-6a95983865c086f44853880e0454eff813bb8b45.tar.gz
integrates the nice features of #218 in #214
Diffstat (limited to 'nanolets/src/test')
-rw-r--r--nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java2
-rw-r--r--nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java91
-rw-r--r--nanolets/src/test/resources/blabla.html1
-rw-r--r--nanolets/src/test/resources/dir/blabla.html1
-rw-r--r--nanolets/src/test/resources/dir/index.htm1
-rw-r--r--nanolets/src/test/resources/dir/nanohttpd_logo.pngbin0 -> 19931 bytes
6 files changed, 92 insertions, 4 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 b2ae4ed..ad01303 100644
--- a/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java
+++ b/nanolets/src/test/java/fi/iki/elonen/router/AppNanolets.java
@@ -40,6 +40,7 @@ package fi.iki.elonen.router;
*/
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@@ -145,6 +146,7 @@ public class AppNanolets extends RouterNanoHTTPD {
addRoute("/toBeDeleted", String.class);
removeRoute("/toBeDeleted");
addRoute("/stream", StreamUrl.class);
+ addRoute("/browse/(.)+", StaticPageHandler.class, new File("src/test/resources").getAbsoluteFile());
}
/**
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 bd360da..62d4f1b 100644
--- a/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java
+++ b/nanolets/src/test/java/fi/iki/elonen/router/TestNanolets.java
@@ -38,12 +38,13 @@ 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.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpTrace;
@@ -77,7 +78,28 @@ public class TestNanolets {
});
serverStartThread.start();
// give the server some tine to start.
- Thread.sleep(100);
+ 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
@@ -187,6 +209,28 @@ public class TestNanolets {
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();
@@ -227,8 +271,8 @@ public class TestNanolets {
@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());
+ "UrlResource{uri='photos/:customer_id/:photo_id', urlParts=[customer_id, photo_id]}",//
+ new UriResource("/photos/:customer_id/:photo_id", 100, GeneralHandler.class).toString());
}
@Test
@@ -273,6 +317,45 @@ public class TestNanolets {
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();
+ }
+
@AfterClass
public static void tearDown() throws Exception {
stdIn.write("\n\n".getBytes());
diff --git a/nanolets/src/test/resources/blabla.html b/nanolets/src/test/resources/blabla.html
new file mode 100644
index 0000000..f346c87
--- /dev/null
+++ b/nanolets/src/test/resources/blabla.html
@@ -0,0 +1 @@
+<html><body><h3>just a page</h3></body></html> \ No newline at end of file
diff --git a/nanolets/src/test/resources/dir/blabla.html b/nanolets/src/test/resources/dir/blabla.html
new file mode 100644
index 0000000..2fd951b
--- /dev/null
+++ b/nanolets/src/test/resources/dir/blabla.html
@@ -0,0 +1 @@
+<html><body><h3>just an other page</h3></body></html> \ No newline at end of file
diff --git a/nanolets/src/test/resources/dir/index.htm b/nanolets/src/test/resources/dir/index.htm
new file mode 100644
index 0000000..f410e3a
--- /dev/null
+++ b/nanolets/src/test/resources/dir/index.htm
@@ -0,0 +1 @@
+<html><body><h3>just an index page</h3></body></html> \ No newline at end of file
diff --git a/nanolets/src/test/resources/dir/nanohttpd_logo.png b/nanolets/src/test/resources/dir/nanohttpd_logo.png
new file mode 100644
index 0000000..0eff47c
--- /dev/null
+++ b/nanolets/src/test/resources/dir/nanohttpd_logo.png
Binary files differ