Search in sources :

Example 1 with HttpResponse

use of com.azure.core.http.HttpResponse in project vividus by vividus-framework.

the class ResponseCapturingHttpPipelinePolicyTests method shouldCaptureResponseData.

@SuppressWarnings("unchecked")
@Test
void shouldCaptureResponseData() throws MalformedURLException {
    HttpResponse response = mock(HttpResponse.class);
    Mono<HttpResponse> mono = mock(Mono.class);
    Mono<String> body = Mono.just(BODY);
    when(response.getBodyAsString()).thenReturn(body);
    HttpHeaders headers = mock(HttpHeaders.class);
    when(response.getHeaders()).thenReturn(headers);
    Map<String, String> headersMap = Map.of("header", "value");
    when(headers.toMap()).thenReturn(headersMap);
    HttpRequest request = mock(HttpRequest.class);
    URL url = URI.create(URL).toURL();
    when(request.getUrl()).thenReturn(url).thenReturn(URI.create("https://google.com").toURL());
    when(response.getRequest()).thenReturn(request);
    when(response.getStatusCode()).thenReturn(SC_OK);
    when(mono.doOnSuccess(argThat(c -> {
        c.accept(response);
        return true;
    }))).thenReturn(mono);
    when(next.process()).thenReturn(mono);
    underTest.process(context, next);
    underTest.process(context, next);
    Map<String, Object> captured = underTest.getResponses();
    Assertions.assertAll(() -> assertEquals(headersMap, captured.get("headers")), () -> assertEquals(body, captured.get(BODY)), () -> assertEquals(SC_OK, captured.get("status-code")), () -> assertEquals(URL, captured.get("url")));
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Mock(org.mockito.Mock) HttpPipelineNextPolicy(com.azure.core.http.HttpPipelineNextPolicy) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) HttpHeaders(com.azure.core.http.HttpHeaders) Test(org.junit.jupiter.api.Test) HttpRequest(com.azure.core.http.HttpRequest) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) HttpPipelineCallContext(com.azure.core.http.HttpPipelineCallContext) Assertions(org.junit.jupiter.api.Assertions) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(com.azure.core.http.HttpHeaders) HttpResponse(com.azure.core.http.HttpResponse) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 2 with HttpResponse

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

the class VertxHttpClientTests method testFlowableBackpressure.

@Test
public void testFlowableBackpressure() {
    HttpResponse response = getResponse("/long");
    StepVerifierOptions stepVerifierOptions = StepVerifierOptions.create();
    stepVerifierOptions.initialRequest(0);
    StepVerifier.create(response.getBody(), stepVerifierOptions).expectNextCount(0).thenRequest(1).expectNextCount(1).thenRequest(3).expectNextCount(3).thenRequest(Long.MAX_VALUE).thenConsumeWhile(ByteBuffer::hasRemaining).verifyComplete();
}
Also used : StepVerifierOptions(reactor.test.StepVerifierOptions) HttpResponse(com.azure.core.http.HttpResponse) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with HttpResponse

use of com.azure.core.http.HttpResponse 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 4 with HttpResponse

use of com.azure.core.http.HttpResponse in project ApplicationInsights-Java by microsoft.

the class QuickPulseDataSender method run.

@Override
public void run() {
    while (true) {
        HttpRequest post;
        try {
            post = sendQueue.take();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
        if (quickPulseHeaderInfo.getQuickPulseStatus() != QuickPulseStatus.QP_IS_ON) {
            continue;
        }
        long sendTime = System.nanoTime();
        try (HttpResponse response = httpPipeline.send(post).block()) {
            if (response == null) {
                // this shouldn't happen, the mono should complete with a response or a failure
                throw new AssertionError("http response mono returned empty");
            }
            // response body is not consumed below
            LazyHttpClient.consumeResponseBody(response);
            if (networkHelper.isSuccess(response)) {
                QuickPulseHeaderInfo quickPulseHeaderInfo = networkHelper.getQuickPulseHeaderInfo(response);
                switch(quickPulseHeaderInfo.getQuickPulseStatus()) {
                    case QP_IS_OFF:
                    case QP_IS_ON:
                        lastValidTransmission = sendTime;
                        this.quickPulseHeaderInfo = quickPulseHeaderInfo;
                        break;
                    case ERROR:
                        onPostError(sendTime);
                        break;
                }
            }
        }
    }
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpResponse(com.azure.core.http.HttpResponse)

Example 5 with HttpResponse

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

the class ResponseEntityTest method testIsSuccessfulShouldReturnTrueWhenResponseCodeIs2xx.

@ParameterizedTest
@MethodSource("responseCodeProvider")
void testIsSuccessfulShouldReturnTrueWhenResponseCodeIs2xx(final int code, final boolean expected) {
    // given
    final HttpResponse response = mock(HttpResponse.class);
    when(response.getStatusCode()).thenReturn(code);
    when(response.getBodyAsString(eq(StandardCharsets.UTF_8))).thenReturn(Mono.empty());
    final ResponseEntity underTest = new ResponseEntity(response, mock(ObjectReader.class));
    // when
    final boolean actual = underTest.isSuccessful();
    // then
    Assertions.assertEquals(expected, actual);
    verify(response).getStatusCode();
    verify(response).getBodyAsString(eq(StandardCharsets.UTF_8));
}
Also used : HttpResponse(com.azure.core.http.HttpResponse) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

HttpResponse (com.azure.core.http.HttpResponse)18 Test (org.junit.jupiter.api.Test)10 HttpRequest (com.azure.core.http.HttpRequest)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)6 HttpHeaders (com.azure.core.http.HttpHeaders)4 VaultModel (com.github.nagyesta.lowkeyvault.http.management.VaultModel)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Mono (reactor.core.publisher.Mono)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 OperationData (bio.terra.cloudres.common.OperationData)1 ContentType (com.azure.core.http.ContentType)1 HttpMethod (com.azure.core.http.HttpMethod)1 HttpPipelineCallContext (com.azure.core.http.HttpPipelineCallContext)1