aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/fi/iki/elonen/integration
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/test/java/fi/iki/elonen/integration')
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java130
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/GZipIntegrationTest.java168
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java177
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java58
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java85
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java64
6 files changed, 525 insertions, 157 deletions
diff --git a/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java
index 0d54b37..5cf5719 100644
--- a/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java
+++ b/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java
@@ -1,6 +1,45 @@
package fi.iki.elonen.integration;
-import fi.iki.elonen.NanoHTTPD;
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
@@ -8,78 +47,77 @@ import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.junit.Test;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.List;
-
-import static org.junit.Assert.*;
+import fi.iki.elonen.NanoHTTPD;
/**
- * @author Paul S. Hawke (paul.hawke@gmail.com)
- * On: 9/2/13 at 10:10 PM
+ * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:10 PM
*/
public class CookieIntegrationTest extends IntegrationTestBase<CookieIntegrationTest.CookieTestServer> {
- @Test
- public void testNoCookies() throws Exception {
- HttpGet httpget = new HttpGet("http://localhost:8192/");
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- httpclient.execute(httpget, responseHandler);
+ public static class CookieTestServer extends NanoHTTPD {
- CookieStore cookies = httpclient.getCookieStore();
- assertEquals(0, cookies.getCookies().size());
+ List<Cookie> cookiesReceived = new ArrayList<Cookie>();
+
+ List<Cookie> cookiesToSend = new ArrayList<Cookie>();
+
+ public CookieTestServer() {
+ super(8192);
+ }
+
+ @Override
+ public Response serve(IHTTPSession session) {
+ CookieHandler cookies = session.getCookies();
+ for (String cookieName : cookies) {
+ this.cookiesReceived.add(new Cookie(cookieName, cookies.read(cookieName)));
+ }
+ for (Cookie c : this.cookiesToSend) {
+ cookies.set(c);
+ }
+ return newFixedLengthResponse("Cookies!");
+ }
+ }
+
+ @Override
+ public CookieTestServer createTestServer() {
+ return new CookieTestServer();
}
@Test
public void testCookieSentBackToClient() throws Exception {
- testServer.cookiesToSend.add(new NanoHTTPD.Cookie("name", "value", 30));
+ this.testServer.cookiesToSend.add(new NanoHTTPD.Cookie("name", "value", 30));
HttpGet httpget = new HttpGet("http://localhost:8192/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
- httpclient.execute(httpget, responseHandler);
+ this.httpclient.execute(httpget, responseHandler);
- CookieStore cookies = httpclient.getCookieStore();
+ CookieStore cookies = this.httpclient.getCookieStore();
assertEquals(1, cookies.getCookies().size());
assertEquals("name", cookies.getCookies().get(0).getName());
assertEquals("value", cookies.getCookies().get(0).getValue());
}
@Test
+ public void testNoCookies() throws Exception {
+ HttpGet httpget = new HttpGet("http://localhost:8192/");
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ this.httpclient.execute(httpget, responseHandler);
+
+ CookieStore cookies = this.httpclient.getCookieStore();
+ assertEquals(0, cookies.getCookies().size());
+ }
+
+ @Test
public void testServerReceivesCookiesSentFromClient() throws Exception {
BasicClientCookie clientCookie = new BasicClientCookie("name", "value");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 100);
clientCookie.setExpiryDate(calendar.getTime());
clientCookie.setDomain("localhost");
- httpclient.getCookieStore().addCookie(clientCookie);
+ this.httpclient.getCookieStore().addCookie(clientCookie);
HttpGet httpget = new HttpGet("http://localhost:8192/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
- httpclient.execute(httpget, responseHandler);
+ this.httpclient.execute(httpget, responseHandler);
- assertEquals(1, testServer.cookiesReceived.size());
- assertTrue(testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value"));
- }
-
- @Override public CookieTestServer createTestServer() {
- return new CookieTestServer();
- }
-
- public static class CookieTestServer extends NanoHTTPD {
- List<Cookie> cookiesReceived = new ArrayList<Cookie>();
- List<Cookie> cookiesToSend = new ArrayList<Cookie>();
-
- public CookieTestServer() {
- super(8192);
- }
-
- @Override public Response serve(IHTTPSession session) {
- CookieHandler cookies = session.getCookies();
- for (String cookieName : cookies) {
- cookiesReceived.add(new Cookie(cookieName, cookies.read(cookieName)));
- }
- for (Cookie c : cookiesToSend) {
- cookies.set(c);
- }
- return new Response("Cookies!");
- }
+ assertEquals(1, this.testServer.cookiesReceived.size());
+ assertTrue(this.testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value"));
}
}
diff --git a/core/src/test/java/fi/iki/elonen/integration/GZipIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/GZipIntegrationTest.java
new file mode 100644
index 0000000..a278406
--- /dev/null
+++ b/core/src/test/java/fi/iki/elonen/integration/GZipIntegrationTest.java
@@ -0,0 +1,168 @@
+package fi.iki.elonen.integration;
+
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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 static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DecompressingHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.junit.Test;
+
+import fi.iki.elonen.NanoHTTPD;
+
+public class GZipIntegrationTest extends IntegrationTestBase<GZipIntegrationTest.TestServer> {
+
+ public static class TestServer extends NanoHTTPD {
+
+ public Response response;
+
+ public TestServer() {
+ super(8192);
+ }
+
+ @Override
+ public Response serve(IHTTPSession session) {
+ return response;
+ }
+
+ @Override
+ protected boolean useGzipWhenAccepted(Response r) {
+ return true;
+ }
+ }
+
+ @Override
+ public TestServer createTestServer() {
+ return new TestServer();
+ }
+
+ @Test
+ public void contentEncodingShouldBeAddedToFixedLengthResponses() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = httpclient.execute(request);
+ Header contentEncoding = response.getFirstHeader("content-encoding");
+ assertNotNull("Content-Encoding should be set", contentEncoding);
+ assertEquals("gzip", contentEncoding.getValue());
+ }
+
+ @Test
+ public void contentEncodingShouldBeAddedToChunkedResponses() throws IOException {
+ InputStream data = new ByteArrayInputStream("This is a test".getBytes("UTF-8"));
+ testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "text/plain", data);
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = httpclient.execute(request);
+ Header contentEncoding = response.getFirstHeader("content-encoding");
+ assertNotNull("Content-Encoding should be set", contentEncoding);
+ assertEquals("gzip", contentEncoding.getValue());
+ }
+
+ @Test
+ public void shouldFindCorrectAcceptEncodingAmongMany() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "deflate,gzip");
+ HttpResponse response = httpclient.execute(request);
+ Header contentEncoding = response.getFirstHeader("content-encoding");
+ assertNotNull("Content-Encoding should be set", contentEncoding);
+ assertEquals("gzip", contentEncoding.getValue());
+ }
+
+ @Test
+ public void contentLengthShouldBeRemovedFromZippedResponses() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = httpclient.execute(request);
+ Header contentLength = response.getFirstHeader("content-length");
+ assertNull("Content-Length should not be set when gzipping response", contentLength);
+ }
+
+ @Test
+ public void fixedLengthContentIsEncodedProperly() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = new DecompressingHttpClient(httpclient).execute(request);
+ assertEquals("This is a test", EntityUtils.toString(response.getEntity()));
+ }
+
+ @Test
+ public void chunkedContentIsEncodedProperly() throws IOException {
+ InputStream data = new ByteArrayInputStream("This is a test".getBytes("UTF-8"));
+ testServer.response = NanoHTTPD.newChunkedResponse(NanoHTTPD.Response.Status.OK, "text/plain", data);
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = new DecompressingHttpClient(httpclient).execute(request);
+ assertEquals("This is a test", EntityUtils.toString(response.getEntity()));
+ }
+
+ @Test
+ public void noGzipWithoutAcceptEncoding() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ HttpResponse response = httpclient.execute(request);
+ Header contentEncoding = response.getFirstHeader("content-encoding");
+ assertThat(contentEncoding, is(nullValue()));
+ assertEquals("This is a test", EntityUtils.toString(response.getEntity()));
+ }
+
+ @Test
+ public void contentShouldNotBeGzippedIfContentLengthIsAddedManually() throws IOException {
+ testServer.response = NanoHTTPD.newFixedLengthResponse("This is a test");
+ testServer.response.addHeader("Content-Length", "" + ("This is a test".getBytes("UTF-8").length));
+ HttpGet request = new HttpGet("http://localhost:8192/");
+ request.addHeader("Accept-encoding", "gzip");
+ HttpResponse response = httpclient.execute(request);
+ Header contentEncoding = response.getFirstHeader("content-encoding");
+ assertNull("Content-Encoding should not be set when manually setting content-length", contentEncoding);
+ assertEquals("This is a test", EntityUtils.toString(response.getEntity()));
+
+ }
+
+}
diff --git a/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java
index bc0a9d9..eef2f22 100644
--- a/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java
+++ b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java
@@ -1,8 +1,49 @@
package fi.iki.elonen.integration;
-import fi.iki.elonen.NanoHTTPD;
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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 static org.junit.Assert.assertEquals;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
-import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
@@ -11,62 +52,66 @@ import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
-import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
-import org.junit.After;
-import org.junit.Before;
+import org.apache.http.util.EntityUtils;
import org.junit.Test;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
-import static org.junit.Assert.assertEquals;
+import fi.iki.elonen.NanoHTTPD;
+import fi.iki.elonen.NanoHTTPD.Response.Status;
/**
- * @author Paul S. Hawke (paul.hawke@gmail.com)
- * On: 5/19/13 at 5:36 PM
+ * @author Paul S. Hawke (paul.hawke@gmail.com) On: 5/19/13 at 5:36 PM
*/
public class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostIntegrationTest.TestServer> {
- @Test
- public void testSimpleGetRequest() throws Exception {
- testServer.response = "testSimpleGetRequest";
+ public static class TestServer extends NanoHTTPD {
- HttpGet httpget = new HttpGet("http://localhost:8192/");
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httpget, responseHandler);
+ public String response;
- assertEquals("GET:testSimpleGetRequest", responseBody);
- }
+ public TestServer() {
+ super(8192);
+ }
- @Test
- public void testGetRequestWithParameters() throws Exception {
- testServer.response = "testGetRequestWithParameters";
+ @Override
+ public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
+ StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + this.response);
- HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male");
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httpget, responseHandler);
+ if (parms.size() > 1) {
+ parms.remove("NanoHttpd.QUERY_STRING");
+ sb.append("-params=").append(parms.size());
+ List<String> p = new ArrayList<String>(parms.keySet());
+ Collections.sort(p);
+ for (String k : p) {
+ sb.append(';').append(k).append('=').append(parms.get(k));
+ }
+ }
+ if ("/chin".equals(uri)) {
+ return newFixedLengthResponse(Status.OK, "application/octet-stream", sb.toString());
+ } else {
+ return newFixedLengthResponse(sb.toString());
+ }
+ }
+ }
- assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody);
+ @Override
+ public TestServer createTestServer() {
+ return new TestServer();
}
@Test
- public void testPostWithNoParameters() throws Exception {
- testServer.response = "testPostWithNoParameters";
+ public void testGetRequestWithParameters() throws Exception {
+ this.testServer.response = "testGetRequestWithParameters";
- HttpPost httppost = new HttpPost("http://localhost:8192/");
+ HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httppost, responseHandler);
+ String responseBody = this.httpclient.execute(httpget, responseHandler);
- assertEquals("POST:testPostWithNoParameters", responseBody);
+ assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody);
}
@Test
public void testPostRequestWithFormEncodedParameters() throws Exception {
- testServer.response = "testPostRequestWithFormEncodedParameters";
+ this.testServer.response = "testPostRequestWithFormEncodedParameters";
HttpPost httppost = new HttpPost("http://localhost:8192/");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
@@ -75,14 +120,14 @@ public class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostInt
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httppost, responseHandler);
+ String responseBody = this.httpclient.execute(httppost, responseHandler);
assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
@Test
public void testPostRequestWithMultipartEncodedParameters() throws Exception {
- testServer.response = "testPostRequestWithMultipartEncodedParameters";
+ this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
HttpPost httppost = new HttpPost("http://localhost:8192/");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
@@ -91,37 +136,53 @@ public class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostInt
httppost.setEntity(reqEntity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httppost, responseHandler);
+ String responseBody = this.httpclient.execute(httppost, responseHandler);
assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody);
}
- @Override public TestServer createTestServer() {
- return new TestServer();
+ @Test
+ public void testPostWithNoParameters() throws Exception {
+ this.testServer.response = "testPostWithNoParameters";
+
+ HttpPost httppost = new HttpPost("http://localhost:8192/");
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ String responseBody = this.httpclient.execute(httppost, responseHandler);
+
+ assertEquals("POST:testPostWithNoParameters", responseBody);
}
- public static class TestServer extends NanoHTTPD {
- public String response;
+ @Test
+ public void testSimpleGetRequest() throws Exception {
+ this.testServer.response = "testSimpleGetRequest";
- public TestServer() {
- super(8192);
- }
+ HttpGet httpget = new HttpGet("http://localhost:8192/");
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ String responseBody = this.httpclient.execute(httpget, responseHandler);
- @Override
- public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
- StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + response);
+ assertEquals("GET:testSimpleGetRequest", responseBody);
+ }
- if (parms.size() > 1) {
- parms.remove("NanoHttpd.QUERY_STRING");
- sb.append("-params=").append(parms.size());
- List<String> p = new ArrayList<String>(parms.keySet());
- Collections.sort(p);
- for (String k : p) {
- sb.append(';').append(k).append('=').append(parms.get(k));
- }
+ @Test
+ public void testPostRequestWithMultipartExtremEncodedParameters() throws Exception {
+ this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
+
+ HttpPost httppost = new HttpPost("http://localhost:8192/chin");
+ MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "sfsadfasdf", Charset.forName("UTF-8"));
+ reqEntity.addPart("specialString", new StringBody("拖拉图片到浏览器,可以实现预览功能", "text/plain", Charset.forName("UTF-8")));
+ reqEntity.addPart("gender", new StringBody("图片名称", Charset.forName("UTF-8")) {
+
+ @Override
+ public String getFilename() {
+ return "图片名称";
}
+ });
+ httppost.setEntity(reqEntity);
+ HttpResponse response = this.httpclient.execute(httppost);
- return new Response(sb.toString());
- }
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity, "UTF-8");
+
+ assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;gender=图片名称;specialString=拖拉图片到浏览器,可以实现预览功能", responseBody);
}
}
diff --git a/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java b/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java
index eb34033..fd3d6d3 100644
--- a/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java
+++ b/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java
@@ -1,27 +1,63 @@
package fi.iki.elonen.integration;
-import fi.iki.elonen.NanoHTTPD;
-import org.apache.http.client.HttpClient;
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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.IOException;
+
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.After;
import org.junit.Before;
-import java.io.IOException;
+import fi.iki.elonen.NanoHTTPD;
/**
- * @author Paul S. Hawke (paul.hawke@gmail.com)
- * On: 9/2/13 at 10:02 PM
+ * @author Paul S. Hawke (paul.hawke@gmail.com) On: 9/2/13 at 10:02 PM
*/
public abstract class IntegrationTestBase<T extends NanoHTTPD> {
+
protected DefaultHttpClient httpclient;
+
protected T testServer;
+ public abstract T createTestServer();
+
@Before
public void setUp() {
- testServer = createTestServer();
- httpclient = new DefaultHttpClient();
+ this.testServer = createTestServer();
+ this.httpclient = new DefaultHttpClient();
try {
- testServer.start();
+ this.testServer.start();
} catch (IOException e) {
e.printStackTrace();
}
@@ -29,9 +65,7 @@ public abstract class IntegrationTestBase<T extends NanoHTTPD> {
@After
public void tearDown() {
- httpclient.getConnectionManager().shutdown();
- testServer.stop();
+ this.httpclient.getConnectionManager().shutdown();
+ this.testServer.stop();
}
-
- public abstract T createTestServer();
}
diff --git a/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java
index 1e260b2..41b84fd 100644
--- a/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java
+++ b/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java
@@ -1,5 +1,38 @@
package fi.iki.elonen.integration;
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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 static org.junit.Assert.assertEquals;
import java.io.DataInputStream;
@@ -16,34 +49,13 @@ import fi.iki.elonen.NanoHTTPD;
public class PutStreamIntegrationTest extends IntegrationTestBase<PutStreamIntegrationTest.TestServer> {
- @Test
- public void testSimplePutRequest() throws Exception {
- String expected = "This HttpPut request has a content-length of 48.";
-
- HttpPut httpput = new HttpPut("http://localhost:8192/");
- httpput.setEntity(new ByteArrayEntity(expected.getBytes()));
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- String responseBody = httpclient.execute(httpput, responseHandler);
-
- assertEquals("PUT:" + expected, responseBody);
- }
-
- @Override public TestServer createTestServer() {
- return new TestServer();
- }
-
public static class TestServer extends NanoHTTPD {
+
public TestServer() {
super(8192);
}
@Override
- public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files)
- {
- throw new UnsupportedOperationException();
- }
-
- @Override
public Response serve(IHTTPSession session) {
Method method = session.getMethod();
Map<String, String> headers = session.getHeaders();
@@ -54,13 +66,34 @@ public class PutStreamIntegrationTest extends IntegrationTestBase<PutStreamInteg
DataInputStream dataInputStream = new DataInputStream(session.getInputStream());
body = new byte[contentLength];
dataInputStream.readFully(body, 0, contentLength);
- }
- catch(IOException e) {
- return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, e.getMessage());
+ } catch (IOException e) {
+ return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, e.getMessage());
}
String response = String.valueOf(method) + ':' + new String(body);
- return new Response(response);
+ return newFixedLengthResponse(response);
+ }
+
+ @Override
+ public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> parms, Map<String, String> files) {
+ throw new UnsupportedOperationException();
}
}
+
+ @Override
+ public TestServer createTestServer() {
+ return new TestServer();
+ }
+
+ @Test
+ public void testSimplePutRequest() throws Exception {
+ String expected = "This HttpPut request has a content-length of 48.";
+
+ HttpPut httpput = new HttpPut("http://localhost:8192/");
+ httpput.setEntity(new ByteArrayEntity(expected.getBytes()));
+ ResponseHandler<String> responseHandler = new BasicResponseHandler();
+ String responseBody = this.httpclient.execute(httpput, responseHandler);
+
+ assertEquals("PUT:" + expected, responseBody);
+ }
}
diff --git a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java
index 0fcb275..cd305ea 100644
--- a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java
+++ b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java
@@ -1,9 +1,39 @@
package fi.iki.elonen.integration;
-import static org.junit.Assert.*;
-import fi.iki.elonen.NanoHTTPD;
+/*
+ * #%L
+ * NanoHttpd-Core
+ * %%
+ * 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 org.junit.Test;
+import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
@@ -11,8 +41,24 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import org.junit.Test;
+
+import fi.iki.elonen.NanoHTTPD;
+
public class ShutdownTest {
+ private class TestServer extends NanoHTTPD {
+
+ public TestServer() {
+ super(8092);
+ }
+
+ @Override
+ public Response serve(IHTTPSession session) {
+ return newFixedLengthResponse("Whatever");
+ }
+ }
+
@Test
public void connectionsAreClosedWhenServerStops() throws IOException {
TestServer server = new TestServer();
@@ -38,16 +84,4 @@ public class ShutdownTest {
in.close();
}
- private class TestServer extends NanoHTTPD {
-
- public TestServer() {
- super(8092);
- }
-
- @Override
- public Response serve(IHTTPSession session) {
- return new Response("Whatever");
- }
- }
-
}