Home | History | Annotate | Download | only in integration
      1 package fi.iki.elonen.integration;
      2 
      3 import static org.junit.Assert.*;
      4 import fi.iki.elonen.NanoHTTPD;
      5 
      6 import org.junit.Test;
      7 
      8 import java.io.IOException;
      9 import java.io.InputStream;
     10 import java.net.HttpURLConnection;
     11 import java.net.MalformedURLException;
     12 import java.net.URL;
     13 
     14 public class ShutdownTest {
     15 
     16     @Test
     17     public void connectionsAreClosedWhenServerStops() throws IOException {
     18         TestServer server = new TestServer();
     19         server.start();
     20         makeRequest();
     21         server.stop();
     22         try {
     23             makeRequest();
     24             fail("Connection should be closed!");
     25         } catch (IOException e) {
     26             // Expected exception
     27         }
     28     }
     29 
     30     private void makeRequest() throws MalformedURLException, IOException {
     31         HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:8092/").openConnection();
     32         // Keep-alive seems to be on by default, but just in case that changes.
     33         connection.addRequestProperty("Connection", "keep-alive");
     34         InputStream in = connection.getInputStream();
     35         while (in.available() > 0) {
     36             in.read();
     37         }
     38         in.close();
     39     }
     40 
     41     private class TestServer extends NanoHTTPD {
     42 
     43         public TestServer() {
     44             super(8092);
     45         }
     46 
     47         @Override
     48         public Response serve(IHTTPSession session) {
     49             return new Response("Whatever");
     50         }
     51     }
     52 
     53 }
     54