aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/fi/iki/elonen/HttpGetRequestTest.java
blob: 598e50bcaf6d5536b4086c4a71ea1744415f5e27 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package fi.iki.elonen;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.util.List;

import static junit.framework.Assert.*;

public class HttpGetRequestTest extends HttpServerTest {

    @Test
    public void testFullyQualifiedWorkingGetRequest() throws Exception {
        ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1");

        String[] expected = {
                "HTTP/1.1 200 OK",
                "Content-Type: text/html",
                "Date: .*",
                "Connection: keep-alive",
                "Content-Length: 0",
                ""
        };

        assertResponse(outputStream, expected);
    }

    @Test
    public void testOutputOfServeSentBackToClient() throws Exception {
        String responseBody = "Success!";
        testServer.response = new NanoHTTPD.Response(responseBody);
        ByteArrayOutputStream outputStream = invokeServer("GET " + URI + " HTTP/1.1");

        String[] expected = {
                "HTTP/1.1 200 OK",
                "Content-Type: text/html",
                "Date: .*",
                "Connection: keep-alive",
                "Content-Length: 8",
                "",
                responseBody
        };

        assertResponse(outputStream, expected);
    }

    @Test
    public void testEmptyHeadersSuppliedToServeMethodFromSimpleWorkingGetRequest() {
        invokeServer("GET " + URI + " HTTP/1.1");
        assertNotNull(testServer.parms);
        assertNotNull(testServer.header);
        assertNotNull(testServer.files);
        assertNotNull(testServer.uri);
    }

    @Test
    public void testSingleUserAgentHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() {
        String userAgent = "jUnit 4.8.2 Unit Test";
        invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\n");
        assertEquals(userAgent, testServer.header.get("user-agent"));
        assertEquals(NanoHTTPD.Method.GET, testServer.method);
        assertEquals(URI, testServer.uri);
    }

    @Test
    public void testMultipleHeaderSuppliedToServeMethodFromSimpleWorkingGetRequest() {
        String userAgent = "jUnit 4.8.2 Unit Test";
        String accept = "text/html";
        invokeServer("GET " + URI + " HTTP/1.1\nUser-Agent: " + userAgent + "\nAccept: " + accept);
        assertEquals(userAgent, testServer.header.get("user-agent"));
        assertEquals(accept, testServer.header.get("accept"));
    }

    @Test
    public void testSingleGetParameter() {
        invokeServer("GET " + URI + "?foo=bar HTTP/1.1");
        assertEquals("bar", testServer.parms.get("foo"));
    }

    @Test
    public void testSingleGetParameterWithNoValue() {
        invokeServer("GET " + URI + "?foo HTTP/1.1");
        assertEquals("", testServer.parms.get("foo"));
    }

    @Test
    public void testMultipleGetParameters() {
        invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1");
        assertEquals("bar", testServer.parms.get("foo"));
        assertEquals("zot", testServer.parms.get("baz"));
    }

    @Test
    public void testMultipleGetParametersWithMissingValue() {
        invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1");
        assertEquals("", testServer.parms.get("foo"));
        assertEquals("zot", testServer.parms.get("baz"));
    }

    @Test
    public void testMultipleGetParametersWithMissingValueAndRequestHeaders() {
        invokeServer("GET " + URI + "?foo=&baz=zot HTTP/1.1\nAccept: text/html");
        assertEquals("", testServer.parms.get("foo"));
        assertEquals("zot", testServer.parms.get("baz"));
        assertEquals("text/html", testServer.header.get("accept"));
    }

    @Test
    public void testDecodingParametersWithSingleValue() {
        invokeServer("GET " + URI + "?foo=bar&baz=zot HTTP/1.1");
        assertEquals("foo=bar&baz=zot", testServer.queryParameterString);
        assertTrue(testServer.decodedParamters.get("foo") instanceof List);
        assertEquals(1, testServer.decodedParamters.get("foo").size());
        assertEquals("bar", testServer.decodedParamters.get("foo").get(0));
        assertTrue(testServer.decodedParamters.get("baz") instanceof List);
        assertEquals(1, testServer.decodedParamters.get("baz").size());
        assertEquals("zot", testServer.decodedParamters.get("baz").get(0));
    }

    @Test
    public void testDecodingParametersWithSingleValueAndMissingValue() {
        invokeServer("GET " + URI + "?foo&baz=zot HTTP/1.1");
        assertEquals("foo&baz=zot", testServer.queryParameterString);
        assertTrue(testServer.decodedParamters.get("foo") instanceof List);
        assertEquals(0, testServer.decodedParamters.get("foo").size());
        assertTrue(testServer.decodedParamters.get("baz") instanceof List);
        assertEquals(1, testServer.decodedParamters.get("baz").size());
        assertEquals("zot", testServer.decodedParamters.get("baz").get(0));
    }

    @Test
    public void testDecodingFieldWithEmptyValueAndFieldWithMissingValueGiveDifferentResults() {
        invokeServer("GET " + URI + "?foo&bar= HTTP/1.1");
        assertTrue(testServer.decodedParamters.get("foo") instanceof List);
        assertEquals(0, testServer.decodedParamters.get("foo").size());
        assertTrue(testServer.decodedParamters.get("bar") instanceof List);
        assertEquals(1, testServer.decodedParamters.get("bar").size());
        assertEquals("", testServer.decodedParamters.get("bar").get(0));
    }

    @Test
    public void testDecodingSingleFieldRepeated() {
        invokeServer("GET " + URI + "?foo=bar&foo=baz HTTP/1.1");
        assertTrue(testServer.decodedParamters.get("foo") instanceof List);
        assertEquals(2, testServer.decodedParamters.get("foo").size());
        assertEquals("bar", testServer.decodedParamters.get("foo").get(0));
        assertEquals("baz", testServer.decodedParamters.get("foo").get(1));
    }

    @Test
    public void testDecodingMixtureOfParameters() {
        invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1");
        assertTrue(testServer.decodedParamters.get("foo") instanceof List);
        assertEquals(2, testServer.decodedParamters.get("foo").size());
        assertEquals("bar", testServer.decodedParamters.get("foo").get(0));
        assertEquals("baz", testServer.decodedParamters.get("foo").get(1));
        assertTrue(testServer.decodedParamters.get("zot") instanceof List);
        assertEquals(0, testServer.decodedParamters.get("zot").size());
        assertTrue(testServer.decodedParamters.get("zim") instanceof List);
        assertEquals(1, testServer.decodedParamters.get("zim").size());
        assertEquals("", testServer.decodedParamters.get("zim").get(0));
    }

    @Test
    public void testDecodingParametersFromParameterMap() {
        invokeServer("GET " + URI + "?foo=bar&foo=baz&zot&zim= HTTP/1.1");
        assertEquals(testServer.decodedParamters, testServer.decodedParamtersFromParameter);
    }
    // -------------------------------------------------------------------------------------------------------- //

}