Skip to content

Commit

Permalink
Adjust doRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahchen6 committed Nov 22, 2024
1 parent 75e570c commit d7f1afa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,11 @@ abstract class ApacheHttpAsyncClient5Test<T extends HttpRequest> extends HttpCli
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers, String body, Closure callback) {
def request = SimpleHttpRequests.create(method, uri)
request.setConfig(RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS).build())
headers.each { request.addHeader(it.key, it.value) }

def future = client.execute(request, null)
def response = future.get(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
callback?.call()
return response.code
}

@Override
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
int doRequest(String method, URI uri, List<List<String>> headers, String body, Closure callback) {
def request = SimpleHttpRequests.create(method, uri)
request.setConfig(RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS).build())
for (String header : headers) {
String[] keyVal = header.split(":")
request.addHeader(keyVal[0], keyVal[1])
request.addHeader(header[0], header[1])
}

def future = client.execute(request, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,10 @@ class ApacheHttpClientResponseHandlerTest extends HttpClientTest implements Test
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers, String body, Closure callback) {
int doRequest(String method, URI uri, List<List<String>> headers, String body, Closure callback) {
def request = new BasicClassicHttpRequest(method, uri)
headers.entrySet().each {
request.addHeader(new BasicHeader(it.key, it.value))
}

CloseableHttpResponse response = null
def status = client.execute(request, handler)

// handler execution is included within the client span, so we can't call the callback there.
callback?.call()

return status
}

@Override
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
def request = new BasicClassicHttpRequest(method, uri)
for (String header : headers) {
String[] keyVal = header.split(":")
request.addHeader(new BasicHeader(keyVal[0], keyVal[1]))
for (List<String> header : headers) {
request.addHeader(new BasicHeader(header[0], header[1]))
}

CloseableHttpResponse response = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,10 @@ abstract class ApacheHttpClientTest<T extends HttpRequest> extends HttpClientTes
}

@Override
int doRequest(String method, URI uri, Map<String, String> headers, String body, Closure callback) {
int doRequest(String method, URI uri, List<List<String>> headers, String body, Closure callback) {
def request = createRequest(method, uri)
headers.entrySet().each {
request.addHeader(new BasicHeader(it.key, it.value))
}

CloseableHttpResponse response = null
try {
response = executeRequest(request, uri)
callback?.call()
return response.code
}
finally {
response?.close()
}
}

@Override
int doRequest(String method, URI uri, String[] headers, String body, Closure callback) {
def request = createRequest(method, uri)
for (String header : headers) {
String[] keyVal = header.split(":")
request.addHeader(new BasicHeader(keyVal[0], keyVal[1]))
for (List<String> header : headers) {
request.addHeader(new BasicHeader(header[0], header[1]))
}

CloseableHttpResponse response = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ abstract class OkHttp3Test extends HttpClientTest {
.build()

@Override
int doRequest(String method, URI uri, Map<String, String> headers, String body, Closure callback) {
int doRequest(String method, URI uri, List<List<String>> headers, String body, Closure callback) {
def reqBody = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("text/plain"), body) : null
Map<String, String> headersMap = new HashMap<>()
for (List<String> header : headers) {
String key = header.get(0)
String val = header.get(1)
if (headersMap.containsKey(key)) {
String originalVal = headersMap.get(key)
headersMap.put(key, originalVal + "," + val)
} else {
headersMap.put(key, val)
}
}
def request = new Request.Builder()
.url(uri.toURL())
.method(method, reqBody)
.headers(Headers.of(headers)).build()
.headers(Headers.of(headersMap)).build()
def response = client.newCall(request).execute()
callback?.call()
return response.code()
Expand All @@ -58,7 +69,7 @@ abstract class OkHttp3Test extends HttpClientTest {

def "request to agent not traced"() {
when:
def status = doRequest(method, url, ["Datadog-Meta-Lang": "java"])
def status = doRequest(method, url, [["Datadog-Meta-Lang", "java"]])

then:
status == 200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
* @param method
* @return
*/
abstract int doRequest(String method, URI uri, Map<String, String> headers = [:], String body = "", Closure callback = null)

abstract int doRequest(String method, URI uri, String[] headers, String body = "", Closure callback = null)
abstract int doRequest(String method, URI uri, List<List<String>> headers = [], String body = "", Closure callback = null)

String keyStorePath() {
server.keystorePath
Expand Down Expand Up @@ -245,7 +243,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {

when:
def status = runUnderTrace("parent") {
doRequest(method, url, [:], body)
doRequest(method, url, [], body)
}
println("RESPONSE: $status")
if (isDataStreamsEnabled()) {
Expand Down Expand Up @@ -285,7 +283,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
def "basic #method request with parent"() {
when:
def status = runUnderTrace("parent") {
doRequest(method, server.address.resolve("/success"), [:], body)
doRequest(method, server.address.resolve("/success"), [], body)
}
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
Expand Down Expand Up @@ -428,7 +426,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
when:
injectSysConfig(HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "$renameService")
def status = runUnderTrace("parent") {
doRequest(method, server.address.resolve("/success"), ["is-dd-server": "false"])
doRequest(method, server.address.resolve("/success"), [["is-dd-server", "false"]])
}
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
Expand Down Expand Up @@ -466,7 +464,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {

when:
def status = runUnderTrace("parent") {
doRequest(method, server.address.resolve("/success"), ["is-dd-server": "false"], "") {
doRequest(method, server.address.resolve("/success"), [["is-dd-server", "false"]], "") {
runUnderTrace("child") {
blockUntilChildSpansFinished(1)
}
Expand Down Expand Up @@ -500,7 +498,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {

def "trace request with callback and no parent"() {
when:
def status = doRequest(method, server.address.resolve("/success"), ["is-dd-server": "false"], "") {
def status = doRequest(method, server.address.resolve("/success"), [["is-dd-server", "false"]], "") {
runUnderTrace("callback") {
// FIXME: since in async we may not have the other trace report until the callback is done
// we should add a test method to detect that the other trace is finished but waiting for
Expand Down Expand Up @@ -652,7 +650,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
def uri = server.address.resolve("/to-secured")

when:
def status = doRequest(method, uri, [(BASIC_AUTH_KEY): BASIC_AUTH_VAL])
def status = doRequest(method, uri, [[BASIC_AUTH_KEY, BASIC_AUTH_VAL]])
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
}
Expand Down Expand Up @@ -769,7 +767,7 @@ abstract class HttpClientTest extends VersionedNamingTestBase {
def url = server.address.resolve("/success")
System.out.println("testing header, value, value2: " + header + ", " + value + ", " + value2)
String[] headerVals = [header + ":" + value , header + ":" + value2]
def status = (value2 == null) ? doRequest(method, url, [(header): value]) : doRequest(method, url, headerVals)
def status = (value2 == null) ? doRequest(method, url, [[header, value]]) : doRequest(method, url, [[header, value], [header, value2]])
if (isDataStreamsEnabled()) {
TEST_DATA_STREAMS_WRITER.waitForGroups(1)
}
Expand Down

0 comments on commit d7f1afa

Please sign in to comment.