aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/fi/iki/elonen/HttpKeepAliveTest.java
blob: f349ee5224f8d34aa18838b8ec3dd648d718bbda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package fi.iki.elonen;

import static junit.framework.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.junit.Test;

public class HttpKeepAliveTest extends HttpServerTest {

    @Test
    public void testManyGetRequests() throws Exception {
        String request = "GET " + URI + " HTTP/1.1\r\n\r\n";
        String[] expected = {
                "HTTP/1.1 200 OK",
                "Content-Type: text/html",
                "Date: .*",
                "Connection: keep-alive",
                "Content-Length: 0",
                ""
        };
        testManyRequests(request, expected);
    }
    
    @Test
    public void testManyPutRequests() throws Exception {
        String data = "BodyData 1\nLine 2";
        String request = "PUT " + URI + " HTTP/1.1\r\nContent-Length: " + data.length() + "\r\n\r\n" + data;
        String[] expected = {
                "HTTP/1.1 200 OK",
                "Content-Type: text/html",
                "Date: .*",
                "Connection: keep-alive",
                "Content-Length: 0",
                ""
        };
        testManyRequests(request, expected);
    }

    private Throwable error = null;
    
    /**
     * Issue the given request many times to check whether an error occurs.
     * For this test, a small stack size is used, since a stack overflow is among the possible errors.
     * @param request The request to issue
     * @param expected The expected response
     */
    public void testManyRequests(final String request, final String[] expected) throws Exception {
        Runnable r = new Runnable() {
            public void run() {
                try {
                    PipedOutputStream requestStream = new PipedOutputStream();
                    PipedInputStream inputStream = new PipedInputStream(requestStream);
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    NanoHTTPD.HTTPSession session = testServer.createSession(new TestTempFileManager(), inputStream, outputStream);
                    for (int i = 0; i < 2048; i++) {
                        requestStream.write(request.getBytes());
                        requestStream.flush();
                        session.execute();
                        assertResponse(outputStream, expected);
                    }
                } catch (Throwable t) {
                    error = t;
                }
            }
        };
        Thread t = new Thread(null, r, "Request Thread", 1 << 17);
        t.start();
        t.join();
        if (error != null) {
            fail(""+error);
            error.printStackTrace();
        }
    }
}