aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java
diff options
context:
space:
mode:
authorAlbin Theander <Albin.Theander@jayway.com>2013-10-30 14:14:36 +0100
committerAlbin Theander <Albin.Theander@jayway.com>2013-10-30 14:14:36 +0100
commit0a35219489d8f19d13197bc60eec168dd6b3089d (patch)
tree4872c0bf958024b1c6700ed01fc1ad0a5d6f078e /core/src/test/java
parent12e45c1e4150d8a4405e3e68b2b8d9993b7e7add (diff)
downloadnanohttpd-0a35219489d8f19d13197bc60eec168dd6b3089d.tar.gz
Closes all existing connections at shotdown.
If clients uses keep-alive connections, the client connections will remain even if the server is closed down. This commit forcibly closes all client connections when the server is stopped.
Diffstat (limited to 'core/src/test/java')
-rw-r--r--core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java
new file mode 100644
index 0000000..0fcb275
--- /dev/null
+++ b/core/src/test/java/fi/iki/elonen/integration/ShutdownTest.java
@@ -0,0 +1,53 @@
+package fi.iki.elonen.integration;
+
+import static org.junit.Assert.*;
+import fi.iki.elonen.NanoHTTPD;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class ShutdownTest {
+
+ @Test
+ public void connectionsAreClosedWhenServerStops() throws IOException {
+ TestServer server = new TestServer();
+ server.start();
+ makeRequest();
+ server.stop();
+ try {
+ makeRequest();
+ fail("Connection should be closed!");
+ } catch (IOException e) {
+ // Expected exception
+ }
+ }
+
+ private void makeRequest() throws MalformedURLException, IOException {
+ HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8092/").openConnection();
+ // Keep-alive seems to be on by default, but just in case that changes.
+ connection.addRequestProperty("Connection", "keep-alive");
+ InputStream in = connection.getInputStream();
+ while (in.available() > 0) {
+ in.read();
+ }
+ in.close();
+ }
+
+ private class TestServer extends NanoHTTPD {
+
+ public TestServer() {
+ super(8092);
+ }
+
+ @Override
+ public Response serve(IHTTPSession session) {
+ return new Response("Whatever");
+ }
+ }
+
+}