66 lines
2.0 KiB
Groovy
66 lines
2.0 KiB
Groovy
/*
|
|
* Copyright The OpenTelemetry Authors
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import io.opentelemetry.instrumentation.test.AgentTestTrait
|
|
import io.opentelemetry.instrumentation.test.base.HttpClientTest
|
|
import java.util.function.Consumer
|
|
import org.springframework.http.HttpEntity
|
|
import org.springframework.http.HttpHeaders
|
|
import org.springframework.http.HttpMethod
|
|
import org.springframework.http.client.ClientHttpRequestFactory
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory
|
|
import org.springframework.web.client.ResourceAccessException
|
|
import org.springframework.web.client.RestTemplate
|
|
import spock.lang.Shared
|
|
|
|
class SpringRestTemplateTest extends HttpClientTest<HttpEntity<String>> implements AgentTestTrait {
|
|
|
|
@Shared
|
|
ClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory()
|
|
@Shared
|
|
RestTemplate restTemplate = new RestTemplate(factory)
|
|
|
|
def setupSpec() {
|
|
factory.connectTimeout = CONNECT_TIMEOUT_MS
|
|
}
|
|
|
|
@Override
|
|
HttpEntity<String> buildRequest(String method, URI uri, Map<String, String> headers) {
|
|
def httpHeaders = new HttpHeaders()
|
|
headers.each { httpHeaders.put(it.key, [it.value]) }
|
|
return new HttpEntity<String>(httpHeaders)
|
|
}
|
|
|
|
@Override
|
|
int sendRequest(HttpEntity<String> request, String method, URI uri, Map<String, String> headers) {
|
|
try {
|
|
return restTemplate.exchange(uri, HttpMethod.valueOf(method), request, String)
|
|
.statusCode
|
|
.value()
|
|
} catch (ResourceAccessException exception) {
|
|
throw exception.getCause()
|
|
}
|
|
}
|
|
|
|
@Override
|
|
void sendRequestWithCallback(HttpEntity<String> request, String method, URI uri, Map<String, String> headers = [:], Consumer<Integer> callback) {
|
|
restTemplate.execute(uri, HttpMethod.valueOf(method), { req ->
|
|
req.getHeaders().putAll(request.getHeaders())
|
|
}, { response ->
|
|
callback.accept(response.statusCode.value())
|
|
})
|
|
}
|
|
|
|
@Override
|
|
int maxRedirects() {
|
|
20
|
|
}
|
|
|
|
@Override
|
|
Integer responseCodeOnRedirectError() {
|
|
return 302
|
|
}
|
|
}
|