-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathApacheAsyncHttpClientTest.java
More file actions
300 lines (245 loc) · 10.2 KB
/
ApacheAsyncHttpClientTest.java
File metadata and controls
300 lines (245 loc) · 10.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.atlassian.httpclient.apache.httpcomponents;
import com.atlassian.httpclient.api.Response;
import com.atlassian.httpclient.api.factory.HttpClientOptions;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.UrlMode;
import com.atlassian.sal.api.executor.ThreadLocalContextManager;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.ProxyConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.Callback;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
public class ApacheAsyncHttpClientTest {
@Rule
public JenkinsRule j = new JenkinsRule();
private final ConnectionFactory connectionFactory = new HttpConnectionFactory();
private Server server;
private ServerConnector connector;
static final String CONTENT_RESPONSE = "Sounds Good";
public void prepare(Handler handler) throws Exception {
server = new Server();
connector = new ServerConnector(server, connectionFactory);
server.addConnector(connector);
server.setHandler(handler);
server.start();
}
@After
public void dispose() throws Exception {
if (server != null) {
server.stop();
}
}
@Test
public void simple_get() throws Exception {
TestHandler testHandler = new TestHandler();
prepare(testHandler);
ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
Response response = httpClient
.newRequest("http://localhost:" + connector.getLocalPort() + "/foo")
.get()
.get(10, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
}
@Test
public void simple_post() throws Exception {
TestHandler testHandler = new TestHandler();
prepare(testHandler);
ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
Response response = httpClient
.newRequest("http://localhost:" + connector.getLocalPort() + "/foo")
.setEntity("FOO")
.setContentType("text")
.post()
.get(10, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
Assert.assertEquals("FOO", testHandler.postReceived);
}
@Test
public void simple_get_with_non_proxy_host() throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);
Jenkins.get().proxy =
new ProxyConfiguration("localhost", connector.getLocalPort(), "foo", "bar", "www.apache.org");
ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
Response response = httpClient.newRequest("http://www.apache.org").get().get(30, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
// Assert.assertEquals( CONTENT_RESPONSE, IOUtils.toString( response.getEntityStream() ) );
}
@Test
public void simple_get_with_proxy() throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);
Jenkins.get().proxy = new ProxyConfiguration("localhost", connector.getLocalPort(), "foo", "bar");
ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
Response response = httpClient.newRequest("http://jenkins.io").get().get(30, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
}
@Test
public void simple_post_with_proxy() throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);
Jenkins.get().proxy = new ProxyConfiguration("localhost", connector.getLocalPort(), "foo", "bar");
ApacheAsyncHttpClient httpClient = new ApacheAsyncHttpClient(
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());
Response response = httpClient
.newRequest("http://jenkins.io")
.setEntity("FOO")
.setContentType("text")
.post()
.get(30, TimeUnit.SECONDS);
// we are sure to hit the proxy first :-)
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
Assert.assertEquals("FOO", testHandler.postReceived);
}
public static class ProxyTestHandler extends Handler.Abstract {
String postReceived;
final String user = "foo";
final String password = "bar";
final String serverHost = "server";
final String realm = "test_realm";
@Override
public boolean handle(
org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response, Callback callback)
throws IOException {
final String credentials = Base64.getEncoder().encodeToString((user + ":" + password).getBytes("UTF-8"));
String authorization = request.getHeaders().get(HttpHeader.PROXY_AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
response.getHeaders().add(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + realm + "\"");
callback.succeeded();
return true;
} else {
String prefix = "Basic ";
if (authorization.startsWith(prefix)) {
String attempt = authorization.substring(prefix.length());
if (!credentials.equals(attempt)) {
callback.succeeded();
return true;
}
}
}
if (StringUtils.equalsIgnoreCase("post", request.getMethod())) {
postReceived = Content.Source.asString(request, StandardCharsets.UTF_8);
}
Content.Sink.write(response, true, CONTENT_RESPONSE, callback);
return true;
}
}
public static class TestHandler extends Handler.Abstract {
String postReceived;
@Override
public boolean handle(
org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response, Callback callback)
throws IOException {
if (StringUtils.equalsIgnoreCase("post", request.getMethod())) {
postReceived = Content.Source.asString(request, StandardCharsets.UTF_8);
}
Content.Sink.write(response, true, CONTENT_RESPONSE, callback);
return true;
}
}
private ApplicationProperties buildApplicationProperties() {
ApplicationProperties applicationProperties = new ApplicationProperties() {
@Override
public String getBaseUrl() {
return null;
}
@NonNull
@Override
public String getBaseUrl(UrlMode urlMode) {
return null;
}
@NonNull
@Override
public String getDisplayName() {
return "Foo";
}
@NonNull
@Override
public String getPlatformId() {
return null;
}
@NonNull
@Override
public String getVersion() {
return "1";
}
@NonNull
@Override
public Date getBuildDate() {
return null;
}
@NonNull
@Override
public String getBuildNumber() {
return "1";
}
@Nullable
@Override
public File getHomeDirectory() {
return null;
}
@Override
public String getPropertyValue(String s) {
return null;
}
@NonNull
@Override
public String getApplicationFileEncoding() {
return System.getProperty("file.encoding");
}
@NonNull
@Override
public Optional<Path> getLocalHomeDirectory() {
return Optional.empty();
}
@NonNull
@Override
public Optional<Path> getSharedHomeDirectory() {
return Optional.empty();
}
};
return applicationProperties;
}
private static final class NoOpThreadLocalContextManager<C> implements ThreadLocalContextManager<C> {
@Override
public C getThreadLocalContext() {
return null;
}
@Override
public void setThreadLocalContext(C context) {}
@Override
public void clearThreadLocalContext() {}
}
}