summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/HttpRequestFactoryTest.java
blob: e2ebdf7a8fca21f52874b23458f835b107d19dfa (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import com.google.inject.Key;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.References;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.jdisc.service.CurrentContainer;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
 * @author Steinar Knutsen
 * @author bjorncs
 */
public class HttpRequestFactoryTest {

    private static final int LOCAL_PORT = 80;

    @Test
    public void testLegalURIs() {
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", null, null));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertNull(uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "", ""));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("http", "host.a1-2-3", "", ""));
            assertEquals("http", uri.getScheme());
            assertEquals("host.a1-2-3", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "/:1/../1=.", ""));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("/:1/../1=.", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "", "a=/../&?="));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("a=/../&?=", uri.getRawQuery());
        }
    }

    @Test
    public void testIllegalQuery() {
        try {
            HttpRequestFactory.newJDiscRequest(
                    new MockContainer(),
                    createMockRequest("http", "example.com", "/search", "query=\"contains_quotes\""));
            fail("Above statement should throw");
        } catch (RequestException e) {
            assertThat(e.getResponseStatus(), is(Response.Status.BAD_REQUEST));
        }
    }

    @Test
    public final void illegal_host_throws_requestexception1() {
        try {
            HttpRequestFactory.newJDiscRequest(
                    new MockContainer(),
                    createMockRequest("http", "?", "/foo", ""));
            fail("Above statement should throw");
        } catch (RequestException e) {
            assertThat(e.getResponseStatus(), is(Response.Status.BAD_REQUEST));
        }
    }

    @Test
    public final void illegal_host_throws_requestexception2() {
        try {
            HttpRequestFactory.newJDiscRequest(
                    new MockContainer(),
                    createMockRequest("http", ".", "/foo", ""));
            fail("Above statement should throw");
        } catch (RequestException e) {
            assertThat(e.getResponseStatus(), is(Response.Status.BAD_REQUEST));
        }
    }

    @Test
    public final void illegal_host_throws_requestexception3() {
        try {
            HttpRequestFactory.newJDiscRequest(
                    new MockContainer(),
                    createMockRequest("http", "*", "/foo", ""));
            fail("Above statement should throw");
        } catch (RequestException e) {
            assertThat(e.getResponseStatus(), is(Response.Status.BAD_REQUEST));
        }
    }

    @Test
    public final void illegal_unicode_in_query_throws_requestexception() {
        try {
            HttpRequestFactory.newJDiscRequest(
                    new MockContainer(),
                    createMockRequest("http", "example.com", "/search", "query=%c0%ae"));
            fail("Above statement should throw");
        } catch (RequestException e) {
            assertThat(e.getResponseStatus(), is(Response.Status.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("URL violates RFC 2396: Not valid UTF8! byte C0 in state 0"));
        }
    }

    @Test
    public void request_uri_uses_local_port() {
        HttpRequest request = HttpRequestFactory.newJDiscRequest(
                new MockContainer(),
                createMockRequest("https", "example.com", "/search", "query=value"));
        assertEquals(LOCAL_PORT, request.getUri().getPort());
    }

    private HttpServletRequest createMockRequest(String scheme, String host, String path, String query) {
        return JettyMockRequestBuilder.newBuilder()
                .uri(scheme, host, LOCAL_PORT, path, query)
                .remote("127.0.0.1", "localhost", 1234)
                .localPort(LOCAL_PORT)
                .build();
    }


    private static final class MockContainer implements CurrentContainer {

        @Override
        public Container newReference(URI uri) {
            return new Container() {

                @Override
                public RequestHandler resolveHandler(com.yahoo.jdisc.Request request) {
                    return null;
                }

                @Override
                public <T> T getInstance(Key<T> tKey) {
                    return null;
                }

                @Override
                public <T> T getInstance(Class<T> tClass) {
                    return null;
                }

                @Override
                public ResourceReference refer() {
                    return References.NOOP_REFERENCE;
                }

                @Override
                public void release() {

                }

                @Override
                public long currentTimeMillis() {
                    return 0;
                }
            };
        }
    }

}