Search in sources :

Example 16 with HttpRequest

use of com.azure.core.http.HttpRequest in project terra-cloud-resource-lib by DataBiosphere.

the class AzureResponseLogger method logResponse.

@Override
public Mono<HttpResponse> logResponse(ClientLogger clientLogger, HttpResponseLoggingContext loggingOptions) {
    final HttpResponse response = loggingOptions.getHttpResponse();
    final HttpRequest request = response.getRequest();
    // Always add request method and request URL
    JsonObject requestDataJson = new JsonObject();
    requestDataJson.addProperty("requestMethod", request.getHttpMethod().toString());
    requestDataJson.addProperty("requestUrl", request.getUrl().toString());
    // Optionally add rich request data if provided in the logging context
    Optional<ResourceManagerRequestData> requestData = Optional.ofNullable(loggingOptions.getContext()).flatMap(c -> c.getData(CLOUD_RESOURCE_REQUEST_DATA_KEY)).map(o -> (ResourceManagerRequestData) o);
    requestData.ifPresent(d -> requestDataJson.add("requestBody", d.serialize()));
    // verbose.
    if (logger.isDebugEnabled()) {
        logBody(request.getHeaders(), request.getBody(), s -> requestDataJson.addProperty("rawRequestBody", s));
        logBody(response.getHeaders(), response.buffer().getBody(), s -> requestDataJson.addProperty("rawResponseBody", s));
    }
    // Build OperationData object.
    OperationData operationData = OperationData.builder().setDuration(Optional.ofNullable(loggingOptions.getResponseDuration()).orElse(Duration.ZERO)).setTryCount(OptionalInt.of(loggingOptions.getTryCount())).setExecutionException(Optional.empty()).setHttpStatusCode(OptionalInt.of(response.getStatusCode())).setCloudOperation(requestData.map(ResourceManagerRequestData::cloudOperation).orElse(ResourceManagerOperation.AZURE_RESOURCE_MANAGER_UNKNOWN_OPERATION)).setRequestData(requestDataJson).build();
    // Invoke OperationAnnotator to record the operation.
    operationAnnotator.recordOperation(operationData);
    return Mono.justOrEmpty(response);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) JsonObject(com.google.gson.JsonObject) HttpResponse(com.azure.core.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) CoreUtils(com.azure.core.util.CoreUtils) OptionalInt(java.util.OptionalInt) OperationData(bio.terra.cloudres.common.OperationData) ByteBuffer(java.nio.ByteBuffer) Duration(java.time.Duration) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) HttpResponseLogger(com.azure.core.http.policy.HttpResponseLogger) ClientLogger(com.azure.core.util.logging.ClientLogger) Channels(java.nio.channels.Channels) ClientConfig(bio.terra.cloudres.common.ClientConfig) OperationAnnotator(bio.terra.cloudres.common.OperationAnnotator) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) CLOUD_RESOURCE_REQUEST_DATA_KEY(bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) HttpRequest(com.azure.core.http.HttpRequest) HttpResponseLoggingContext(com.azure.core.http.policy.HttpResponseLoggingContext) ContentType(com.azure.core.http.ContentType) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OperationData(bio.terra.cloudres.common.OperationData) HttpResponse(com.azure.core.http.HttpResponse) JsonObject(com.google.gson.JsonObject)

Example 17 with HttpRequest

use of com.azure.core.http.HttpRequest in project camel-quarkus by apache.

the class DeadlockTests method attemptToDeadlock.

@Test
public void attemptToDeadlock() {
    HttpClient httpClient = new VertxHttpClientProvider().createInstance();
    String endpoint = server.baseUrl() + GET_ENDPOINT;
    for (int i = 0; i < 100; i++) {
        StepVerifier.create(httpClient.send(new HttpRequest(HttpMethod.GET, endpoint)).flatMap(response -> FluxUtil.collectBytesInByteBufferStream(response.getBody()).zipWith(Mono.just(response.getStatusCode())))).assertNext(responseTuple -> {
            Assertions.assertEquals(200, responseTuple.getT2());
            Assertions.assertArrayEquals(expectedGetBytes, responseTuple.getT1());
        }).verifyComplete();
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) BeforeEach(org.junit.jupiter.api.BeforeEach) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) StepVerifier(reactor.test.StepVerifier) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) Mono(reactor.core.publisher.Mono) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) WireMock(com.github.tomakehurst.wiremock.client.WireMock) SecureRandom(java.security.SecureRandom) FluxUtil(com.azure.core.util.FluxUtil) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) HttpRequest(com.azure.core.http.HttpRequest) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) HttpClient(com.azure.core.http.HttpClient) HttpMethod(com.azure.core.http.HttpMethod) Assertions(org.junit.jupiter.api.Assertions) HttpClient(com.azure.core.http.HttpClient) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 18 with HttpRequest

use of com.azure.core.http.HttpRequest in project camel-quarkus by apache.

the class VertxHttpClientBuilderTests method buildWithConnectionOptions.

@Test
public void buildWithConnectionOptions() {
    WebClientOptions options = new WebClientOptions();
    HttpClient client = new VertxHttpClientBuilder(vertx).webClientOptions(options).connectTimeout(Duration.ofSeconds(10)).idleTimeout(Duration.ofSeconds(20)).readIdleTimeout(Duration.ofSeconds(30)).writeIdleTimeout(Duration.ofSeconds(40)).build();
    try {
        StepVerifier.create(client.send(new HttpRequest(HttpMethod.GET, defaultUrl))).assertNext(response -> assertEquals(200, response.getStatusCode())).verifyComplete();
        assertEquals(10000, options.getConnectTimeout());
        assertEquals(20, options.getIdleTimeout());
        assertEquals(30, options.getReadIdleTimeout());
        assertEquals(40, options.getWriteIdleTimeout());
    } finally {
        ((VertxHttpClient) client).close();
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) PROXY_PASSWORD(org.apache.camel.quarkus.support.azure.core.http.vertx.VertxHttpClientTestResource.PROXY_PASSWORD) StepVerifier(reactor.test.StepVerifier) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) Vertx(io.vertx.core.Vertx) EnumSource(org.junit.jupiter.params.provider.EnumSource) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) WireMock(com.github.tomakehurst.wiremock.client.WireMock) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) PROXY_USER(org.apache.camel.quarkus.support.azure.core.http.vertx.VertxHttpClientTestResource.PROXY_USER) CountDownLatch(java.util.concurrent.CountDownLatch) ProxyOptions(com.azure.core.http.ProxyOptions) List(java.util.List) Configuration(com.azure.core.util.Configuration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpRequest(com.azure.core.http.HttpRequest) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) HttpClient(com.azure.core.http.HttpClient) HttpMethod(com.azure.core.http.HttpMethod) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpClient(com.azure.core.http.HttpClient) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with HttpRequest

use of com.azure.core.http.HttpRequest in project camel-quarkus by apache.

the class VertxHttpClientBuilderTests method buildWithDefaultConnectionOptions.

@Test
public void buildWithDefaultConnectionOptions() {
    WebClientOptions options = new WebClientOptions();
    HttpClient client = new VertxHttpClientBuilder(vertx).webClientOptions(options).build();
    try {
        StepVerifier.create(client.send(new HttpRequest(HttpMethod.GET, defaultUrl))).assertNext(response -> assertEquals(200, response.getStatusCode())).verifyComplete();
        assertEquals(10000, options.getConnectTimeout());
        assertEquals(60, options.getIdleTimeout());
        assertEquals(60, options.getReadIdleTimeout());
        assertEquals(60, options.getWriteIdleTimeout());
    } finally {
        ((VertxHttpClient) client).close();
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) PROXY_PASSWORD(org.apache.camel.quarkus.support.azure.core.http.vertx.VertxHttpClientTestResource.PROXY_PASSWORD) StepVerifier(reactor.test.StepVerifier) WireMockConfiguration(com.github.tomakehurst.wiremock.core.WireMockConfiguration) Vertx(io.vertx.core.Vertx) EnumSource(org.junit.jupiter.params.provider.EnumSource) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) WireMock(com.github.tomakehurst.wiremock.client.WireMock) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) PROXY_USER(org.apache.camel.quarkus.support.azure.core.http.vertx.VertxHttpClientTestResource.PROXY_USER) CountDownLatch(java.util.concurrent.CountDownLatch) ProxyOptions(com.azure.core.http.ProxyOptions) List(java.util.List) Configuration(com.azure.core.util.Configuration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpRequest(com.azure.core.http.HttpRequest) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) HttpClient(com.azure.core.http.HttpClient) HttpMethod(com.azure.core.http.HttpMethod) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpClient(com.azure.core.http.HttpClient) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with HttpRequest

use of com.azure.core.http.HttpRequest in project camel-quarkus by apache.

the class VertxHttpClientTests method testRequestBodyIsErrorShouldPropagateToResponse.

@Test
public void testRequestBodyIsErrorShouldPropagateToResponse() {
    HttpClient client = new VertxHttpClientProvider().createInstance();
    HttpRequest request = new HttpRequest(HttpMethod.POST, url(server, "/shortPost")).setHeader("Content-Length", "123").setBody(Flux.error(new RuntimeException("boo")));
    StepVerifier.create(client.send(request)).expectErrorMessage("boo").verify();
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpClient(com.azure.core.http.HttpClient) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (com.azure.core.http.HttpRequest)31 Test (org.junit.jupiter.api.Test)13 HttpMethod (com.azure.core.http.HttpMethod)9 HttpResponse (com.azure.core.http.HttpResponse)9 HttpClient (com.azure.core.http.HttpClient)8 URI (java.net.URI)8 URL (java.net.URL)8 HttpHeaders (com.azure.core.http.HttpHeaders)7 IOException (java.io.IOException)7 Mono (reactor.core.publisher.Mono)7 Date (java.util.Date)6 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)5 StandardCharsets (java.nio.charset.StandardCharsets)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpPipeline (com.azure.core.http.HttpPipeline)3 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)3 WireMockConfiguration (com.github.tomakehurst.wiremock.core.WireMockConfiguration)3 Duration (java.time.Duration)3 HashMap (java.util.HashMap)3