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
|
// Copyright 2017 Yahoo Holdings. 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.eclipse.jetty.server.HttpConnection;
import org.testng.annotations.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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.fail;
/**
* @author Steinar Knutsen
* @author bjorncs
*/
public class HttpRequestFactoryTest {
private static final int LOCAL_PORT = 8080;
@Test
public final 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_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("Query 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("http", "example.com", "/search", "query=value"));
assertEquals(LOCAL_PORT, request.getUri().getPort());
}
private static HttpServletRequest createMockRequest(String scheme, String serverName, String path, String queryString) {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpConnection connection = mock(HttpConnection.class);
when(connection.getCreatedTimeStamp()).thenReturn(System.currentTimeMillis());
when(request.getAttribute("org.eclipse.jetty.server.HttpConnection")).thenReturn(connection);
when(request.getProtocol()).thenReturn("HTTP/1.1");
when(request.getScheme()).thenReturn(scheme);
when(request.getServerName()).thenReturn(serverName);
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
when(request.getRemotePort()).thenReturn(1234);
when(request.getLocalPort()).thenReturn(LOCAL_PORT);
when(request.getMethod()).thenReturn("GET");
when(request.getQueryString()).thenReturn(queryString);
when(request.getRequestURI()).thenReturn(path);
return request;
}
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;
}
};
}
}
}
|