Search in sources :

Example 1 with HttpRequest

use of com.azure.core.http.HttpRequest 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 HttpRequest

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

the class VertxHttpClientTests method testRequestBodyEndsInErrorShouldPropagateToResponse.

@Test
public void testRequestBodyEndsInErrorShouldPropagateToResponse() {
    HttpClient client = new VertxHttpClientProvider().createInstance();
    String contentChunk = "abcdefgh";
    int repetitions = 1000;
    HttpRequest request = new HttpRequest(HttpMethod.POST, url(server, "/shortPost")).setHeader("Content-Length", String.valueOf(contentChunk.length() * (repetitions + 1))).setBody(Flux.just(contentChunk).repeat(repetitions).map(s -> ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8))).concatWith(Flux.error(new RuntimeException("boo"))));
    StepVerifier.create(client.send(request)).expectErrorMessage("boo").verify(Duration.ofSeconds(10));
}
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)

Example 3 with HttpRequest

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

use of com.azure.core.http.HttpRequest 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 HttpRequest

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

the class QuickPulseNetworkHelper method buildRequest.

public HttpRequest buildRequest(Date currentDate, String address) {
    long ticks = currentDate.getTime() * 10000 + TICKS_AT_EPOCH;
    HttpRequest request = new HttpRequest(HttpMethod.POST, address);
    request.setHeader(HEADER_TRANSMISSION_TIME, String.valueOf(ticks));
    return request;
}
Also used : HttpRequest(com.azure.core.http.HttpRequest)

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