-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathApacheHttpClientTest.groovy
253 lines (215 loc) · 8.28 KB
/
ApacheHttpClientTest.groovy
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.instrumentation.test.AgentTestTrait
import io.opentelemetry.instrumentation.test.base.HttpClientTest
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.apache.http.HttpHost
import org.apache.http.HttpRequest
import org.apache.http.HttpResponse
import org.apache.http.client.params.ClientPNames
import org.apache.http.conn.ClientConnectionManager
import org.apache.http.conn.ClientConnectionManagerFactory
import org.apache.http.conn.scheme.SchemeRegistry
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
import org.apache.http.message.BasicHeader
import org.apache.http.message.BasicHttpRequest
import org.apache.http.params.BasicHttpParams
import org.apache.http.params.HttpConnectionParams
import org.apache.http.params.HttpParams
import org.apache.http.protocol.BasicHttpContext
import spock.lang.Shared
import java.util.function.Consumer
abstract class ApacheHttpClientTest<T extends HttpRequest> extends HttpClientTest<T> implements AgentTestTrait {
@Shared
DefaultHttpClient client = buildClient(false)
@Shared
DefaultHttpClient clientWithReadTimeout = buildClient(true)
DefaultHttpClient buildClient(boolean readTimeout) {
HttpParams httpParams = new BasicHttpParams()
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT_MS)
if (readTimeout) {
HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT_MS)
}
httpParams.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, ThreadSafeClientConnManagerFactory.getName())
return new DefaultHttpClient(httpParams)
}
DefaultHttpClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return clientWithReadTimeout
}
return client
}
static class ThreadSafeClientConnManagerFactory implements ClientConnectionManagerFactory {
@Override
ClientConnectionManager newInstance(HttpParams httpParams, SchemeRegistry schemeRegistry) {
return new ThreadSafeClientConnManager(httpParams, schemeRegistry)
}
}
def cleanupSpec() {
client.getConnectionManager().shutdown()
clientWithReadTimeout.getConnectionManager().shutdown()
}
@Override
String userAgent() {
return "apachehttpclient"
}
@Override
boolean testReadTimeout() {
true
}
@Override
T buildRequest(String method, URI uri, Map<String, String> headers) {
def request = createRequest(method, uri)
request.addHeader("user-agent", userAgent())
headers.entrySet().each {
request.setHeader(new BasicHeader(it.key, it.value))
}
return request
}
@Override
Set<AttributeKey<?>> httpAttributes(URI uri) {
Set<AttributeKey<?>> extra = [
SemanticAttributes.HTTP_SCHEME,
SemanticAttributes.HTTP_TARGET
]
super.httpAttributes(uri) + extra
}
// compilation fails with @Override annotation on this method (groovy quirk?)
int sendRequest(T request, String method, URI uri, Map<String, String> headers) {
def response = executeRequest(request, uri)
response.entity?.content?.close() // Make sure the connection is closed.
return response.statusLine.statusCode
}
// compilation fails with @Override annotation on this method (groovy quirk?)
void sendRequestWithCallback(T request, String method, URI uri, Map<String, String> headers, HttpClientResult requestResult) {
try {
executeRequestWithCallback(request, uri) {
it.entity?.content?.close() // Make sure the connection is closed.
requestResult.complete(it.statusLine.statusCode)
}
} catch (Throwable throwable) {
requestResult.complete(throwable)
}
}
abstract T createRequest(String method, URI uri)
abstract HttpResponse executeRequest(T request, URI uri)
abstract void executeRequestWithCallback(T request, URI uri, Consumer<HttpResponse> callback)
static String fullPathFromURI(URI uri) {
StringBuilder builder = new StringBuilder()
if (uri.getPath() != null) {
builder.append(uri.getPath())
}
if (uri.getQuery() != null) {
builder.append('?')
builder.append(uri.getQuery())
}
if (uri.getFragment() != null) {
builder.append('#')
builder.append(uri.getFragment())
}
return builder.toString()
}
}
class ApacheClientHostRequest extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicHttpRequest(method, fullPathFromURI(uri))
}
@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request)
}
@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request) {
callback.accept(it)
}
}
}
class ApacheClientHostAbsoluteUriRequest extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
return new BasicHttpRequest(method, uri.toString())
}
@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request)
}
@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request) {
callback.accept(it)
}
}
}
class ApacheClientHostRequestContext extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
// also testing with an absolute path below
return new BasicHttpRequest(method, fullPathFromURI(uri))
}
@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, new BasicHttpContext())
}
@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, {
callback.accept(it)
}, new BasicHttpContext())
}
}
class ApacheClientHostAbsoluteUriRequestContext extends ApacheHttpClientTest<BasicHttpRequest> {
@Override
BasicHttpRequest createRequest(String method, URI uri) {
return new BasicHttpRequest(method, uri.toString())
}
@Override
HttpResponse executeRequest(BasicHttpRequest request, URI uri) {
return getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, new BasicHttpContext())
}
@Override
void executeRequestWithCallback(BasicHttpRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), request, {
callback.accept(it)
}, new BasicHttpContext())
}
}
class ApacheClientUriRequest extends ApacheHttpClientTest<HttpUriRequest> {
@Override
HttpUriRequest createRequest(String method, URI uri) {
return new HttpUriRequest(method, uri)
}
@Override
HttpResponse executeRequest(HttpUriRequest request, URI uri) {
return getClient(uri).execute(request)
}
@Override
void executeRequestWithCallback(HttpUriRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(request) {
callback.accept(it)
}
}
}
class ApacheClientUriRequestContext extends ApacheHttpClientTest<HttpUriRequest> {
@Override
HttpUriRequest createRequest(String method, URI uri) {
return new HttpUriRequest(method, uri)
}
@Override
HttpResponse executeRequest(HttpUriRequest request, URI uri) {
return getClient(uri).execute(request, new BasicHttpContext())
}
@Override
void executeRequestWithCallback(HttpUriRequest request, URI uri, Consumer<HttpResponse> callback) {
getClient(uri).execute(request, {
callback.accept(it)
}, new BasicHttpContext())
}
}