Home | History | Annotate | Download | only in elonen
      1 package fi.iki.elonen;
      2 
      3 /*
      4  * #%L
      5  * NanoHttpd-Core
      6  * %%
      7  * Copyright (C) 2012 - 2015 nanohttpd
      8  * %%
      9  * Redistribution and use in source and binary forms, with or without modification,
     10  * are permitted provided that the following conditions are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright notice, this
     13  *    list of conditions and the following disclaimer.
     14  *
     15  * 2. Redistributions in binary form must reproduce the above copyright notice,
     16  *    this list of conditions and the following disclaimer in the documentation
     17  *    and/or other materials provided with the distribution.
     18  *
     19  * 3. Neither the name of the nanohttpd nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     32  * OF THE POSSIBILITY OF SUCH DAMAGE.
     33  * #L%
     34  */
     35 
     36 import static junit.framework.Assert.fail;
     37 
     38 import java.io.ByteArrayOutputStream;
     39 import java.io.PipedInputStream;
     40 import java.io.PipedOutputStream;
     41 
     42 import org.junit.Test;
     43 
     44 public class HttpKeepAliveTest extends HttpServerTest {
     45 
     46     private Throwable error = null;
     47 
     48     @Test
     49     public void testManyGetRequests() throws Exception {
     50         String request = "GET " + HttpServerTest.URI + " HTTP/1.1\r\n\r\n";
     51         String[] expected = {
     52             "HTTP/1.1 200 OK",
     53             "Content-Type: text/html",
     54             "Date: .*",
     55             "Connection: keep-alive",
     56             "Content-Length: 0",
     57             ""
     58         };
     59         testManyRequests(request, expected);
     60     }
     61 
     62     @Test
     63     public void testManyPutRequests() throws Exception {
     64         String data = "BodyData 1\nLine 2";
     65         String request = "PUT " + HttpServerTest.URI + " HTTP/1.1\r\nContent-Length: " + data.length() + "\r\n\r\n" + data;
     66         String[] expected = {
     67             "HTTP/1.1 200 OK",
     68             "Content-Type: text/html",
     69             "Date: .*",
     70             "Connection: keep-alive",
     71             "Content-Length: 0",
     72             ""
     73         };
     74         testManyRequests(request, expected);
     75     }
     76 
     77     /**
     78      * Issue the given request many times to check whether an error occurs. For
     79      * this test, a small stack size is used, since a stack overflow is among
     80      * the possible errors.
     81      *
     82      * @param request
     83      *            The request to issue
     84      * @param expected
     85      *            The expected response
     86      */
     87     public void testManyRequests(final String request, final String[] expected) throws Exception {
     88         Runnable r = new Runnable() {
     89 
     90             @Override
     91             public void run() {
     92                 try {
     93                     PipedOutputStream requestStream = new PipedOutputStream();
     94                     PipedInputStream inputStream = new PipedInputStream(requestStream);
     95                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     96                     NanoHTTPD.DefaultTempFileManager tempFileManager = new NanoHTTPD.DefaultTempFileManager();
     97                     try {
     98                         NanoHTTPD.HTTPSession session = HttpKeepAliveTest.this.testServer.createSession(tempFileManager, inputStream, outputStream);
     99                         for (int i = 0; i < 2048; i++) {
    100                             requestStream.write(request.getBytes());
    101                             requestStream.flush();
    102                             outputStream.reset();
    103                             session.execute();
    104                             assertResponse(outputStream, expected);
    105                         }
    106 
    107                         // Finally, try "Connection: Close"
    108                         String closeReq = request.replaceAll("HTTP/1.1", "HTTP/1.1\r\nConnection: Close");
    109                         expected[3] = "Connection: close";
    110                         requestStream.write(closeReq.getBytes());
    111                         outputStream.reset();
    112                         requestStream.flush();
    113                         // Server should now close the socket by throwing a
    114                         // SocketException:
    115                         try {
    116                             session.execute();
    117                         } catch (java.net.SocketException se) {
    118                             junit.framework.Assert.assertEquals(se.getMessage(), "NanoHttpd Shutdown");
    119                         }
    120                         assertResponse(outputStream, expected);
    121 
    122                     } finally {
    123                         tempFileManager.clear();
    124                     }
    125                 } catch (Throwable t) {
    126                     HttpKeepAliveTest.this.error = t;
    127                 }
    128             }
    129         };
    130         Thread t = new Thread(null, r, "Request Thread", 1 << 17);
    131         t.start();
    132         t.join();
    133         if (this.error != null) {
    134             fail("" + this.error);
    135             this.error.printStackTrace();
    136         }
    137     }
    138 }
    139