Search in sources :

Example 21 with Request

use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testPostWithBody_defaultContentType.

@Test
public void testPostWithBody_defaultContentType() {
    Channel channel = stubNoContentResponseChannel();
    StubServiceWithoutContentType service = JaxRsClient.create(StubServiceWithoutContentType.class, channel, runtime);
    service.post("Hello, World!");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    Endpoint endpoint = endpointCaptor.getValue();
    assertThat(endpoint.serviceName()).isEqualTo("StubServiceWithoutContentType");
    assertThat(endpoint.endpointName()).isEqualTo("post");
    assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.POST);
    Request request = requestCaptor.getValue();
    assertThat(request.body()).isPresent();
    assertThat(request.body().get().contentType()).isEqualTo("application/json");
    assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Content-Length", ImmutableList.of("15")));
}
Also used : AbstractMap(java.util.AbstractMap) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.Test)

Example 22 with Request

use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testTrailingWildcardParameter_emptyString.

@Test
public void testTrailingWildcardParameter_emptyString() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.complexPath("dynamic0", "");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    verify(urlBuilder).pathSegment("static0");
    verify(urlBuilder).pathSegment("dynamic0");
    verify(urlBuilder).pathSegment("static1");
    // Empty string must be included
    verify(urlBuilder).pathSegment("");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Example 23 with Request

use of com.palantir.dialogue.Request in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testEmptyStringPathParameter.

@Test
public void testEmptyStringPathParameter() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.innerPath("");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    verify(urlBuilder).pathSegment("begin");
    verify(urlBuilder).pathSegment("");
    verify(urlBuilder).pathSegment("end");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Example 24 with Request

use of com.palantir.dialogue.Request in project dialogue by palantir.

the class LargeResponseTest method large_response.

@ParameterizedTest
@MethodSource("responseTestArguments")
public void large_response(CloseType closeType, TransferEncoding encoding) throws Exception {
    AtomicBoolean used = new AtomicBoolean();
    Undertow server = startServer(new BlockingHandler(exchange -> {
        long responseBytes = 300 * 1024 * 1024;
        encoding.apply(exchange, responseBytes);
        String sessionId = Base64.getEncoder().encodeToString(exchange.getConnection().getSslSession().getId());
        exchange.getResponseHeaders().put(HttpString.tryFromString("TlsSessionId"), sessionId);
        if (!used.getAndSet(true)) {
            OutputStream out = exchange.getOutputStream();
            for (int i = 0; i < responseBytes; i++) {
                out.write(7);
                // Flush + pause 100ms every 1M for a response time of 30 seconds
                if (i % (1024 * 1024) == 0) {
                    out.flush();
                    Thread.sleep(100);
                }
            }
        }
    }));
    try {
        String uri = "https://localhost:" + getPort(server);
        ClientConfiguration conf = TestConfigurations.create(uri);
        Meter closedConns = DialogueClientMetrics.of(conf.taggedMetricRegistry()).connectionClosedPartiallyConsumedResponse("client");
        String firstSession;
        Channel channel;
        assertThat(closedConns.getCount()).isZero();
        try (ApacheHttpClientChannels.CloseableClient client = ApacheHttpClientChannels.createCloseableHttpClient(conf, "client")) {
            channel = ApacheHttpClientChannels.createSingleUri(uri, client);
            ListenableFuture<Response> response = channel.execute(TestEndpoint.POST, Request.builder().build());
            Response resp = response.get();
            firstSession = resp.getFirstHeader("TlsSessionId").orElseThrow();
            assertThat(resp.code()).isEqualTo(200);
            try (InputStream responseStream = resp.body()) {
                assertThat(responseStream.read()).isEqualTo(7);
                long beforeClose = System.nanoTime();
                closeType.close(responseStream, resp);
                Duration closeDuration = Duration.ofNanos(System.nanoTime() - beforeClose);
                assertThat(closeDuration).isLessThan(Duration.ofSeconds(2));
                assertThat(closedConns.getCount()).isOne();
            }
        }
        // Ensure that the client isn't left in a bad state (connection has been discarded, not incorrectly added
        // back to the pool in an ongoing request state)
        ListenableFuture<Response> response = channel.execute(TestEndpoint.POST, Request.builder().build());
        try (Response resp = response.get()) {
            assertThat(resp.code()).isEqualTo(200);
            assertThat(resp.body()).isEmpty();
            assertThat(resp.getFirstHeader("TlsSessionId").orElseThrow()).as("Expected a new connection").isNotEqualTo(firstSession);
            assertThat(closedConns.getCount()).isOne();
        }
    } finally {
        server.stop();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpServerExchange(io.undertow.server.HttpServerExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) TestEndpoint(com.palantir.dialogue.TestEndpoint) Undertow(io.undertow.Undertow) HttpString(io.undertow.util.HttpString) Meter(com.codahale.metrics.Meter) Duration(java.time.Duration) Request(com.palantir.dialogue.Request) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHandler(io.undertow.server.handlers.BlockingHandler) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Channel(com.palantir.dialogue.Channel) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) TestConfigurations(com.palantir.dialogue.TestConfigurations) HttpHandler(io.undertow.server.HttpHandler) SslSocketFactories(com.palantir.conjure.java.config.ssl.SslSocketFactories) Base64(java.util.Base64) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Headers(io.undertow.util.Headers) Response(com.palantir.dialogue.Response) InputStream(java.io.InputStream) Meter(com.codahale.metrics.Meter) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Channel(com.palantir.dialogue.Channel) Duration(java.time.Duration) HttpString(io.undertow.util.HttpString) Response(com.palantir.dialogue.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockingHandler(io.undertow.server.handlers.BlockingHandler) Undertow(io.undertow.Undertow) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 25 with Request

use of com.palantir.dialogue.Request in project dialogue by palantir.

the class ContentEncodingChannelTest method testCompression.

@Test
void testCompression() {
    byte[] expected = "Hello".getBytes(StandardCharsets.UTF_8);
    Request request = Request.builder().body(new RequestBody() {

        @Override
        public void writeTo(OutputStream output) throws IOException {
            output.write(expected);
        }

        @Override
        public String contentType() {
            return "text/plain";
        }

        @Override
        public boolean repeatable() {
            return true;
        }

        @Override
        public OptionalLong contentLength() {
            return OptionalLong.of(expected.length);
        }

        @Override
        public void close() {
        }
    }).build();
    Request wrapped = ContentEncodingChannel.wrap(request);
    assertThat(wrapped.body()).hasValueSatisfying(body -> {
        assertThat(body.contentLength()).isEmpty();
        assertThat(inflate(content(body))).isEqualTo(expected);
    });
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Request(com.palantir.dialogue.Request) RequestBody(com.palantir.dialogue.RequestBody) Test(org.junit.jupiter.api.Test)

Aggregations

Request (com.palantir.dialogue.Request)40 Test (org.junit.jupiter.api.Test)21 Channel (com.palantir.dialogue.Channel)14 Endpoint (com.palantir.dialogue.Endpoint)14 Response (com.palantir.dialogue.Response)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 TestResponse (com.palantir.dialogue.TestResponse)8 Test (org.junit.Test)8 TestEndpoint (com.palantir.dialogue.TestEndpoint)6 Optional (java.util.Optional)6 Futures (com.google.common.util.concurrent.Futures)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 RequestBody (com.palantir.dialogue.RequestBody)5 UrlBuilder (com.palantir.dialogue.UrlBuilder)5 OutputStream (java.io.OutputStream)5 Duration (java.time.Duration)5 ExecutionException (java.util.concurrent.ExecutionException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ImmutableList (com.google.common.collect.ImmutableList)4