diff options
Diffstat (limited to 'core/src/test/java/fi/iki/elonen/integration')
5 files changed, 177 insertions, 178 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 f765a38..f085f3e 100644 --- a/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/CookieIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * 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. @@ -33,7 +33,13 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; +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; @@ -41,81 +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 */ 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); - - assertEquals(1, testServer.cookiesReceived.size()); - assertTrue(testServer.cookiesReceived.get(0).getHTTPHeader().contains("name=value")); - } + this.httpclient.execute(httpget, responseHandler); - @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/GetAndPostIntegrationTest.java b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java index ea4041d..127aac5 100644 --- a/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/GetAndPostIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * 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. @@ -33,9 +33,14 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + 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; @@ -44,61 +49,61 @@ 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.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; /** * @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)); + } + } - assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody); + return newFixedLengthResponse(sb.toString()); + } + } + + @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>(); @@ -107,14 +112,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); @@ -123,39 +128,30 @@ 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(); - } - - public static class TestServer extends NanoHTTPD { + @Test + public void testPostWithNoParameters() throws Exception { + this.testServer.response = "testPostWithNoParameters"; - public String response; + HttpPost httppost = new HttpPost("http://localhost:8192/"); + ResponseHandler<String> responseHandler = new BasicResponseHandler(); + String responseBody = this.httpclient.execute(httppost, responseHandler); - public TestServer() { - super(8192); - } + assertEquals("POST:testPostWithNoParameters", responseBody); + } - @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); + @Test + public void testSimpleGetRequest() throws Exception { + this.testServer.response = "testSimpleGetRequest"; - 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)); - } - } + HttpGet httpget = new HttpGet("http://localhost:8192/"); + ResponseHandler<String> responseHandler = new BasicResponseHandler(); + String responseBody = this.httpclient.execute(httpget, responseHandler); - return new Response(sb.toString()); - } + assertEquals("GET:testSimpleGetRequest", 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 9ad6b73..36de796 100644 --- a/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java +++ b/core/src/test/java/fi/iki/elonen/integration/IntegrationTestBase.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * 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. @@ -33,13 +33,13 @@ package fi.iki.elonen.integration; * #L% */ -import fi.iki.elonen.NanoHTTPD; -import org.apache.http.client.HttpClient; +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 @@ -50,12 +50,14 @@ public abstract class IntegrationTestBase<T extends NanoHTTPD> { 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(); } @@ -63,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 6bdde4d..0232dc0 100644 --- a/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/PutStreamIntegrationTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * 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. @@ -49,23 +49,6 @@ 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() { @@ -73,11 +56,6 @@ public class PutStreamIntegrationTest extends IntegrationTestBase<PutStreamInteg } @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(); @@ -89,11 +67,33 @@ public class PutStreamIntegrationTest extends IntegrationTestBase<PutStreamInteg body = new byte[contentLength]; dataInputStream.readFully(body, 0, contentLength); } catch (IOException e) { - return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, e.getMessage()); + 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 fc3b57b..c650f52 100644 --- a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java +++ b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.integration; * %% * 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. @@ -33,10 +33,7 @@ package fi.iki.elonen.integration; * #L% */ -import static org.junit.Assert.*; -import fi.iki.elonen.NanoHTTPD; - -import org.junit.Test; +import static org.junit.Assert.fail; import java.io.IOException; import java.io.InputStream; @@ -44,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(); @@ -71,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"); - } - } - } |