1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate.test;
17
18 import java.io.IOException;
19 import java.util.Properties;
20
21 import org.apache.http.HttpHost;
22 import org.apache.http.HttpResponse;
23 import org.apache.http.HttpVersion;
24 import org.apache.http.RequestLine;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.conn.HttpClientConnectionManager;
27 import org.apache.http.message.BasicRequestLine;
28 import org.esigate.Driver;
29 import org.esigate.DriverFactory;
30 import org.esigate.HttpErrorPage;
31 import org.esigate.Renderer;
32 import org.esigate.api.ContainerRequestContext;
33 import org.esigate.http.Http;
34 import org.esigate.http.HttpClientRequestExecutor;
35 import org.esigate.http.IncomingRequest;
36 import org.esigate.impl.DriverRequest;
37 import org.esigate.test.conn.IResponseHandler;
38 import org.esigate.test.conn.MockConnectionManager;
39 import org.esigate.test.http.HttpResponseBuilder;
40 import org.esigate.util.UriUtils;
41
42
43
44
45
46
47
48
49 public final class TestUtils {
50
51 private TestUtils() {
52
53 }
54
55
56
57
58
59
60
61
62 public static IncomingRequest.Builder createIncomingRequest(String uri) {
63 HttpHost httpHost = UriUtils.extractHost(uri);
64 String scheme = httpHost.getSchemeName();
65 String host = httpHost.getHostName();
66 int port = httpHost.getPort();
67 RequestLine requestLine = new BasicRequestLine("GET", uri, HttpVersion.HTTP_1_1);
68 IncomingRequest.Builder builder = IncomingRequest.builder(requestLine);
69 builder.setContext(new ContainerRequestContext() {
70 });
71
72 if (port == -1 || (port == Http.DEFAULT_HTTP_PORT && "http".equals(scheme))
73 || (port == Http.DEFAULT_HTTPS_PORT && "https".equals(scheme))) {
74 builder.addHeader("Host", host);
75 } else {
76 builder.addHeader("Host", host + ":" + port);
77 }
78 builder.setSession(new MockSession());
79 return builder;
80 }
81
82
83
84
85
86
87 public static IncomingRequest.Builder createIncomingRequest() {
88 return createIncomingRequest("http://localhost:8080");
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public static DriverRequest createDriverRequest(String uri, Driver driver) throws HttpErrorPage {
103 IncomingRequest request = createIncomingRequest(uri).build();
104 return new DriverRequest(request, driver, "/");
105 }
106
107
108
109
110
111
112
113
114
115
116 public static DriverRequest createDriverRequest(Driver driver) throws HttpErrorPage {
117 IncomingRequest request = createIncomingRequest().build();
118 return new DriverRequest(request, driver, "/");
119 }
120
121
122
123
124
125
126
127
128
129
130 public static Driver createMockDriver(Properties properties, IResponseHandler responseHandler) {
131 MockConnectionManager connManager = new MockConnectionManager();
132 connManager.setResponseHandler(responseHandler);
133 return createMockDriver(properties, connManager);
134 }
135
136
137
138
139
140
141
142
143
144
145 public static Driver createMockDriver(Properties properties, HttpResponse response) {
146 MockConnectionManager connManager = new MockConnectionManager();
147 connManager.setResponse(response);
148 return createMockDriver(properties, connManager);
149 }
150
151
152
153
154
155
156
157
158
159
160 public static Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager) {
161 return createMockDriver(properties, connectionManager, "tested");
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175 public static Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager,
176 String name) {
177 Driver driver =
178 Driver.builder()
179 .setName(name)
180 .setProperties(properties)
181 .setRequestExecutorBuilder(
182 HttpClientRequestExecutor.builder().setConnectionManager(connectionManager)).build();
183 DriverFactory.put(name, driver);
184 return driver;
185 }
186
187
188
189
190
191
192 public static HttpResponseBuilder createHttpResponse() {
193 return new HttpResponseBuilder();
194 }
195
196
197
198
199
200
201
202
203 public static IncomingRequest.Builder createRequest(String uri) {
204 return createIncomingRequest(uri);
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public static CloseableHttpResponse driverProxy(Driver d, IncomingRequest request, Renderer... renderers)
224 throws IOException, HttpErrorPage {
225 String uri = request.getRequestLine().getUri();
226 return d.proxy(UriUtils.getPath(uri), request, renderers);
227 }
228
229 }