Search in sources :

Example 1 with HttpMethod

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

the class ApacheHttpClientTest method testConstructorShouldConvertValuesWhenCalled.

@ParameterizedTest
@MethodSource("validProvider")
void testConstructorShouldConvertValuesWhenCalled(final String body, final Function<URI, URI> hostOverrideFunction, final String expectedHost) throws IOException {
    // 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));
    final HttpClient client = mock(HttpClient.class);
    final HttpRequest azureRequest;
    if (body != null) {
        azureRequest = new HttpRequest(method, url, headers, Flux.defer(() -> Flux.just(ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8)))));
    } else {
        azureRequest = new HttpRequest(method, url, headers, null);
    }
    final org.apache.http.HttpResponse response = ApacheHttpResponseTest.responseMock();
    final ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    when(client.execute(captor.capture())).thenReturn(response);
    final ApacheHttpClient underTest = new ApacheHttpClient(client, hostOverrideFunction);
    // when
    final HttpResponse actual = underTest.send(azureRequest).block();
    // then
    Assertions.assertNotNull(actual);
    ApacheHttpResponseTest.verifyResponse((ApacheHttpResponse) actual);
    Assertions.assertEquals(expectedHost, captor.getValue().getURI().getHost());
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpHeaders(com.azure.core.http.HttpHeaders) HttpClient(org.apache.http.client.HttpClient) HttpResponse(com.azure.core.http.HttpResponse) HttpMethod(com.azure.core.http.HttpMethod) URL(java.net.URL) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with HttpMethod

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

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

the class VertxHttpClient method toVertxHttpRequest.

private Mono<VertxHttpRequest> toVertxHttpRequest(HttpRequest request) {
    return Mono.from(convertBodyToBuffer(request)).map(buffer -> {
        HttpMethod httpMethod = request.getHttpMethod();
        io.vertx.core.http.HttpMethod requestMethod = io.vertx.core.http.HttpMethod.valueOf(httpMethod.name());
        URL url = request.getUrl();
        if (url.getPath().isEmpty()) {
            try {
                // Azure API documentation states:
                // 
                // The URI must always include the forward slash (/) to separate the host name
                // from the path and query portions of the URI.
                // 
                url = new URL(url.getProtocol(), url.getHost(), url.getPort(), "/" + url.getFile());
            } catch (MalformedURLException e) {
                throw new IllegalStateException(e);
            }
        }
        io.vertx.ext.web.client.HttpRequest<Buffer> delegate = client.requestAbs(requestMethod, url.toString());
        if (request.getHeaders() != null) {
            request.getHeaders().stream().forEach(httpHeader -> delegate.putHeader(httpHeader.getName(), httpHeader.getValuesList()));
        }
        return new VertxHttpRequest(delegate, buffer);
    });
}
Also used : Context(com.azure.core.util.Context) ByteBuffer(java.nio.ByteBuffer) Buffer(io.vertx.core.buffer.Buffer) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) HttpMethod(com.azure.core.http.HttpMethod)

Aggregations

HttpMethod (com.azure.core.http.HttpMethod)3 URL (java.net.URL)3 HttpHeaders (com.azure.core.http.HttpHeaders)2 HttpRequest (com.azure.core.http.HttpRequest)1 HttpResponse (com.azure.core.http.HttpResponse)1 Context (com.azure.core.util.Context)1 Buffer (io.vertx.core.buffer.Buffer)1 MalformedURLException (java.net.MalformedURLException)1 ByteBuffer (java.nio.ByteBuffer)1 HttpClient (org.apache.http.client.HttpClient)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1