-
Notifications
You must be signed in to change notification settings - Fork 932
/
Copy pathVertxHttpClientTest.groovy
119 lines (101 loc) · 3.39 KB
/
VertxHttpClientTest.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package client
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.instrumentation.testing.junit.http.SingleConnection
import io.vertx.core.Future
import io.vertx.core.Vertx
import io.vertx.core.VertxOptions
import io.vertx.core.http.HttpClientOptions
import io.vertx.core.http.HttpClientRequest
import io.vertx.core.http.HttpMethod
import io.vertx.core.http.RequestOptions
import spock.lang.Shared
import java.util.concurrent.CompletableFuture
class VertxHttpClientTest extends HttpClientTest<Future<HttpClientRequest>> implements AgentTestTrait {
@Shared
def vertx = Vertx.vertx(new VertxOptions())
@Shared
def clientOptions = new HttpClientOptions().setConnectTimeout(CONNECT_TIMEOUT_MS)
@Shared
def httpClient = vertx.createHttpClient(clientOptions)
@Override
Future<HttpClientRequest> buildRequest(String method, URI uri, Map<String, String> headers) {
RequestOptions requestOptions = new RequestOptions()
.setMethod(HttpMethod.valueOf(method))
.setAbsoluteURI(uri.toString())
headers.each { requestOptions.putHeader(it.key, it.value) }
return httpClient.request(requestOptions)
}
CompletableFuture<Integer> sendRequest(Future<HttpClientRequest> request) {
CompletableFuture<Integer> future = new CompletableFuture<>()
request.compose { req ->
req.send().onComplete { asyncResult ->
if (asyncResult.succeeded()) {
future.complete(asyncResult.result().statusCode())
} else {
future.completeExceptionally(asyncResult.cause())
}
}
}.onFailure { throwable ->
future.completeExceptionally(throwable)
}
return future
}
@Override
int sendRequest(Future<HttpClientRequest> request, String method, URI uri, Map<String, String> headers) {
// Vertx doesn't seem to provide any synchronous API so bridge through a callback
return sendRequest(request).get()
}
@Override
void sendRequestWithCallback(Future<HttpClientRequest> request, String method, URI uri, Map<String, String> headers, HttpClientResult requestResult) {
sendRequest(request).whenComplete { status, throwable ->
requestResult.complete({ status }, throwable)
}
}
@Override
boolean testRedirects() {
false
}
@Override
boolean testReusedRequest() {
// vertx requests can't be reused
false
}
@Override
boolean testHttps() {
false
}
@Override
boolean testReadTimeout() {
false
}
@Override
String expectedClientSpanName(URI uri, String method) {
switch (uri.toString()) {
case "http://localhost:61/": // unopened port
case "http://192.0.2.1/": // non routable address
return "CONNECT"
default:
return super.expectedClientSpanName(uri, method)
}
}
@Override
Set<AttributeKey<?>> httpAttributes(URI uri) {
switch (uri.toString()) {
case "http://localhost:61/": // unopened port
case "http://192.0.2.1/": // non routable address
return []
}
return super.httpAttributes(uri)
}
@Override
SingleConnection createSingleConnection(String host, int port) {
new VertxSingleConnection(host, port)
}
}