Search in sources :

Example 6 with HttpHeaders

use of com.azure.core.http.HttpHeaders in project lowkey-vault by nagyesta.

the class ApacheHttpRequestTest method testConstructorShouldConvertValuesWhenCalled.

@Test
void testConstructorShouldConvertValuesWhenCalled() throws MalformedURLException, URISyntaxException {
    // given
    final HttpMethod method = HttpMethod.POST;
    final URL url = new URL("https://localhost");
    final HttpHeaders headers = new HttpHeaders(Map.of(HEADER_1, HEADER_VALUE_1, HEADER_2, HEADER_VALUE_2));
    // when
    final ApacheHttpRequest actual = new ApacheHttpRequest(method, url, headers, Function.identity());
    // then
    Assertions.assertEquals(method.toString(), actual.getMethod());
    Assertions.assertEquals(url.toURI(), actual.getURI());
    Assertions.assertEquals(HEADER_VALUE_1, actual.getHeaders(HEADER_1)[0].getValue());
    Assertions.assertEquals(HEADER_VALUE_2, actual.getHeaders(HEADER_2)[0].getValue());
}
Also used : HttpHeaders(com.azure.core.http.HttpHeaders) HttpMethod(com.azure.core.http.HttpMethod) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 7 with HttpHeaders

use of com.azure.core.http.HttpHeaders in project bulk-scan-processor by hmcts.

the class BlobManagerTest method tryMoveFileToRejectedContainer_retry_delete_when_lease_lost.

@Test
void tryMoveFileToRejectedContainer_retry_delete_when_lease_lost() {
    // given
    given(inputContainerClient.getBlobClient(INPUT_FILE_NAME)).willReturn(inputBlobClient);
    given(rejectedContainerClient.getBlobClient(INPUT_FILE_NAME)).willReturn(rejectedBlobClient);
    given(blobServiceClient.getBlobContainerClient(INPUT_CONTAINER_NAME)).willReturn(inputContainerClient);
    given(blobServiceClient.getBlobContainerClient(REJECTED_CONTAINER_NAME)).willReturn(rejectedContainerClient);
    given(rejectedBlobClient.exists()).willReturn(Boolean.FALSE);
    mockBeginCopy("http://retry", UUID.randomUUID().toString());
    HttpResponse response = mock(HttpResponse.class);
    given(response.getStatusCode()).willReturn(412);
    HttpHeaders httpHeaders = mock(HttpHeaders.class);
    given(response.getHeaders()).willReturn(httpHeaders);
    given(httpHeaders.getValue(ERROR_CODE)).willReturn(BlobErrorCode.LEASE_LOST.toString());
    willThrow(new BlobStorageException(BlobErrorCode.LEASE_LOST.toString(), response, null)).willReturn(mock(Response.class)).given(inputBlobClient).deleteWithResponse(any(), any(), any(), any());
    // when
    blobManager.tryMoveFileToRejectedContainer(INPUT_FILE_NAME, INPUT_CONTAINER_NAME);
    // then
    verify(rejectedBlobClient).beginCopy(any(), any(), any(), any(), any(), any(), any());
    verify(inputBlobClient, times(2)).deleteWithResponse(any(), any(), any(), any());
}
Also used : PollResponse(com.azure.core.util.polling.PollResponse) HttpResponse(com.azure.core.http.HttpResponse) Response(com.azure.core.http.rest.Response) HttpHeaders(com.azure.core.http.HttpHeaders) HttpResponse(com.azure.core.http.HttpResponse) BlobStorageException(com.azure.storage.blob.models.BlobStorageException) Test(org.junit.jupiter.api.Test)

Example 8 with HttpHeaders

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

the class VertxHttpClientTests method validateHeadersReturnAsIs.

@Test
public void validateHeadersReturnAsIs() {
    HttpClient client = new VertxHttpClientProvider().createInstance();
    final String singleValueHeaderName = "singleValue";
    final String singleValueHeaderValue = "value";
    final String multiValueHeaderName = "Multi-value";
    final List<String> multiValueHeaderValue = Arrays.asList("value1", "value2");
    HttpHeaders headers = new HttpHeaders().set(singleValueHeaderName, singleValueHeaderValue).set(multiValueHeaderName, multiValueHeaderValue);
    StepVerifier.create(client.send(new HttpRequest(HttpMethod.GET, url(server, RETURN_HEADERS_AS_IS_PATH), headers, Flux.empty()))).assertNext(response -> {
        Assertions.assertEquals(200, response.getStatusCode());
        HttpHeaders responseHeaders = response.getHeaders();
        HttpHeader singleValueHeader = responseHeaders.get(singleValueHeaderName);
        assertEquals(singleValueHeaderName, singleValueHeader.getName());
        assertEquals(singleValueHeaderValue, singleValueHeader.getValue());
        HttpHeader multiValueHeader = responseHeaders.get("Multi-value");
        assertEquals(multiValueHeaderName, multiValueHeader.getName());
    }).expectComplete().verify(Duration.ofSeconds(10));
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpHeaders(com.azure.core.http.HttpHeaders) HttpHeader(com.azure.core.http.HttpHeader) HttpClient(com.azure.core.http.HttpClient) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 9 with HttpHeaders

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

the class VertxHttpResponse method fromVertxHttpHeaders.

private HttpHeaders fromVertxHttpHeaders(MultiMap headers) {
    HttpHeaders azureHeaders = new HttpHeaders();
    headers.names().forEach(name -> azureHeaders.set(name, headers.getAll(name)));
    return azureHeaders;
}
Also used : HttpHeaders(com.azure.core.http.HttpHeaders)

Example 10 with HttpHeaders

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

the class AzureResponseLogger method logBody.

private static void logBody(HttpHeaders headers, Flux<ByteBuffer> body, Consumer<String> consumer) {
    // Ensure we have a valid content length
    String contentLengthString = headers.getValue("Content-Length");
    if (CoreUtils.isNullOrEmpty(contentLengthString)) {
        return;
    }
    final long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException | NullPointerException e) {
        return;
    }
    // The body is logged if the Content-Type is not "application/octet-stream" and the
    // body isn't empty and is less than 16KB in size.
    String contentTypeHeader = headers.getValue("Content-Type");
    if (!ContentType.APPLICATION_OCTET_STREAM.equalsIgnoreCase(contentTypeHeader) && contentLength != 0 && contentLength < MAX_BODY_LOG_SIZE) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream((int) contentLength);
        WritableByteChannel bodyContentChannel = Channels.newChannel(outputStream);
        body.flatMap(byteBuffer -> {
            try {
                bodyContentChannel.write(byteBuffer.duplicate());
                return Mono.just(byteBuffer);
            } catch (IOException e) {
                return Mono.error(e);
            }
        }).doFinally(ignored -> consumer.accept(outputStream.toString(Charsets.UTF_8)));
    }
}
Also used : 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) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

HttpHeaders (com.azure.core.http.HttpHeaders)12 Test (org.junit.jupiter.api.Test)6 HttpRequest (com.azure.core.http.HttpRequest)4 HttpResponse (com.azure.core.http.HttpResponse)4 Mono (reactor.core.publisher.Mono)4 HttpPipeline (com.azure.core.http.HttpPipeline)3 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)3 URI (java.net.URI)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HttpHeader (com.azure.core.http.HttpHeader)2 HttpMethod (com.azure.core.http.HttpMethod)2 MockHttpResponse (com.microsoft.applicationinsights.agent.internal.MockHttpResponse)2 TelemetryClient (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)2 URISyntaxException (java.net.URISyntaxException)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 CLOUD_RESOURCE_REQUEST_DATA_KEY (bio.terra.cloudres.azure.resourcemanager.common.Defaults.CLOUD_RESOURCE_REQUEST_DATA_KEY)1 ClientConfig (bio.terra.cloudres.common.ClientConfig)1 OperationAnnotator (bio.terra.cloudres.common.OperationAnnotator)1