forked from AsyncHttpClient/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpTest.java
104 lines (86 loc) · 3.75 KB
/
HttpTest.java
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
/*
* Copyright (c) 2016-2023 AsyncHttpClient Project. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asynchttpclient.testserver;
import io.github.nettyplus.leakdetector.junit.NettyLeakDetectorExtension;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;
@ExtendWith(NettyLeakDetectorExtension.class)
public abstract class HttpTest {
protected static final String COMPLETED_EVENT = "Completed";
protected static final String STATUS_RECEIVED_EVENT = "StatusReceived";
protected static final String HEADERS_RECEIVED_EVENT = "HeadersReceived";
protected static final String HEADERS_WRITTEN_EVENT = "HeadersWritten";
protected static final String CONNECTION_OPEN_EVENT = "ConnectionOpen";
protected static final String HOSTNAME_RESOLUTION_EVENT = "HostnameResolution";
protected static final String HOSTNAME_RESOLUTION_SUCCESS_EVENT = "HostnameResolutionSuccess";
protected static final String CONNECTION_SUCCESS_EVENT = "ConnectionSuccess";
protected static final String TLS_HANDSHAKE_EVENT = "TlsHandshake";
protected static final String TLS_HANDSHAKE_SUCCESS_EVENT = "TlsHandshakeSuccess";
protected static final String CONNECTION_POOL_EVENT = "ConnectionPool";
protected static final String CONNECTION_OFFER_EVENT = "ConnectionOffer";
protected static final String REQUEST_SEND_EVENT = "RequestSend";
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected ClientTestBody withClient() {
return withClient(config().setMaxRedirects(0));
}
protected ClientTestBody withClient(DefaultAsyncHttpClientConfig.Builder builder) {
return withClient(builder.build());
}
private ClientTestBody withClient(AsyncHttpClientConfig config) {
return new ClientTestBody(config);
}
protected ServerTestBody withServer(HttpServer server) {
return new ServerTestBody(server);
}
@FunctionalInterface
protected interface ClientFunction {
void apply(AsyncHttpClient client) throws Throwable;
}
@FunctionalInterface
protected interface ServerFunction {
void apply(HttpServer server) throws Throwable;
}
protected static class ClientTestBody {
private final AsyncHttpClientConfig config;
private ClientTestBody(AsyncHttpClientConfig config) {
this.config = config;
}
public void run(ClientFunction f) throws Throwable {
try (AsyncHttpClient client = asyncHttpClient(config)) {
f.apply(client);
}
}
}
protected static class ServerTestBody {
private final HttpServer server;
private ServerTestBody(HttpServer server) {
this.server = server;
}
public void run(ServerFunction f) throws Throwable {
try {
f.apply(server);
} finally {
server.reset();
}
}
}
}