Skip to content

Commit 03363d0

Browse files
committed
Add tests
1 parent 58de7ff commit 03363d0

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

dd-java-agent/instrumentation/apache-httpclient-5/src/test/groovy/ApacheHttpAsyncClient5Test.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ abstract class ApacheHttpAsyncClient5Test<T extends HttpRequest> extends HttpCli
5555
callback?.call()
5656
return response.code
5757
}
58+
59+
@Override
60+
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
61+
def request = SimpleHttpRequests.create(method, uri)
62+
request.setConfig(RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS).build())
63+
for (String header : headers) {
64+
String[] keyVal = header.split(":")
65+
request.addHeader(keyVal[0], keyVal[1])
66+
}
67+
68+
def future = client.execute(request, null)
69+
def response = future.get(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
70+
callback?.call()
71+
return response.code
72+
}
5873
}
5974

6075
class ApacheHttpAsyncClient5NamingV0ForkedTest extends ApacheHttpAsyncClient5Test implements TestingGenericHttpNamingConventions.ClientV0 {

dd-java-agent/instrumentation/apache-httpclient-5/src/test/groovy/ApacheHttpClientResponseHandlerTest.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ class ApacheHttpClientResponseHandlerTest extends HttpClientTest implements Test
5353
return status
5454
}
5555

56+
@Override
57+
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
58+
def request = new BasicClassicHttpRequest(method, uri)
59+
for (String header : headers) {
60+
String[] keyVal = header.split(":")
61+
request.addHeader(new BasicHeader(keyVal[0], keyVal[1]))
62+
}
63+
64+
CloseableHttpResponse response = null
65+
def status = client.execute(request, handler)
66+
67+
// handler execution is included within the client span, so we can't call the callback there.
68+
callback?.call()
69+
70+
return status
71+
}
72+
5673
@Override
5774
CharSequence component() {
5875
return ApacheHttpClientDecorator.DECORATE.component()

dd-java-agent/instrumentation/apache-httpclient-5/src/test/groovy/ApacheHttpClientTest.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ abstract class ApacheHttpClientTest<T extends HttpRequest> extends HttpClientTes
4848
}
4949
}
5050

51+
@Override
52+
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
53+
def request = createRequest(method, uri)
54+
for (String header : headers) {
55+
String[] keyVal = header.split(":")
56+
request.addHeader(new BasicHeader(keyVal[0], keyVal[1]))
57+
}
58+
59+
CloseableHttpResponse response = null
60+
try {
61+
response = executeRequest(request, uri)
62+
callback?.call()
63+
return response.code
64+
}
65+
finally {
66+
response?.close()
67+
}
68+
}
69+
5170
abstract T createRequest(String method, URI uri)
5271

5372
abstract CloseableHttpResponse executeRequest(T request, URI uri)

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/base/HttpClientTest.groovy

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
144144
*/
145145
abstract int doRequest(String method, URI uri, Map<String, String> headers = [:], String body = "", Closure callback = null)
146146

147+
abstract int doRequest(String method, URI uri, String[] headers, String body = "", Closure callback = null)
148+
147149
String keyStorePath() {
148150
server.keystorePath
149151
}
@@ -765,7 +767,8 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
765767
def "test request header #header tag mapping"() {
766768
when:
767769
def url = server.address.resolve("/success")
768-
def status = doRequest(method, url, [(header): value])
770+
String[] headerVals = [header + ":" + value , header + ":" + value2]
771+
def status = (value2 == null) ? doRequest(method, url, [(header): value]) : doRequest(method, url, headerVals)
769772
if (isDataStreamsEnabled()) {
770773
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
771774
}
@@ -788,11 +791,13 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
788791
}
789792

790793
where:
791-
method | header | value | tags
792-
'GET' | 'X-Datadog-Test-Both-Header' | 'foo' | [ 'both_header_tag': 'foo' ]
793-
'GET' | 'X-Datadog-Test-Request-Header' | 'bar' | [ 'request_header_tag': 'bar' ]
794-
'GET' | 'X-Datadog-Test-Both-Header' | 'bar,baz' | [ 'both_header_tag': 'bar,baz' ]
795-
'GET' | 'X-Datadog-Test-Request-Header' | 'foo,bar' | [ 'request_header_tag': 'foo,bar' ]
794+
method | header | value | value2 | tags
795+
'GET' | 'X-Datadog-Test-Both-Header' | 'foo' | null | [ 'both_header_tag': 'foo' ]
796+
'GET' | 'X-Datadog-Test-Request-Header' | 'bar' | null | [ 'request_header_tag': 'bar' ]
797+
'GET' | 'X-Datadog-Test-Both-Header' | 'bar,baz' | null | [ 'both_header_tag': 'bar,baz' ]
798+
'GET' | 'X-Datadog-Test-Request-Header' | 'foo,bar' | null | [ 'request_header_tag': 'foo,bar' ]
799+
'GET' | 'X-Datadog-Test-Both-Header' | 'bar,baz' | 'foo' | [ 'both_header_tag': 'bar,baz,foo' ]
800+
'GET' | 'X-Datadog-Test-Request-Header' | 'foo,bar' | 'baz' | [ 'request_header_tag': 'foo,bar,baz' ]
796801
}
797802

798803
def "test response header #header tag mapping"() {

0 commit comments

Comments
 (0)