Search in sources :

Example 1 with Response

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

the class JaxRsClientDialogueEndpointTest method stubNoContentResponseChannel.

static Channel stubNoContentResponseChannel() {
    Channel channel = mock(Channel.class);
    Response response = mock(Response.class);
    when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0]));
    when(response.code()).thenReturn(204);
    when(response.headers()).thenReturn(ImmutableListMultimap.of());
    when(channel.execute(any(Endpoint.class), any(Request.class))).thenReturn(Futures.immediateFuture(response));
    return channel;
}
Also used : Response(com.palantir.dialogue.Response) Endpoint(com.palantir.dialogue.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request)

Example 2 with Response

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

the class MyServiceIntegrationTest method testCustomResponse.

private void testCustomResponse(int code) {
    undertowHandler = exchange -> {
        exchange.assertMethod(HttpMethod.PUT);
        exchange.assertPath("/custom/request1");
        exchange.assertAccept().isEqualTo("*/*");
        exchange.assertContentType().isNull();
        exchange.assertNoBody();
        exchange.exchange.setStatusCode(code);
        exchange.exchange.getResponseHeaders().add(HttpString.tryFromString("My-Custom-Header"), "my-custom-header-value");
        exchange.setContentType("text/csv");
        exchange.writeStringBody("Custom Body");
    };
    try (Response response = myServiceDialogue.customResponse()) {
        assertThat(response.code()).isEqualTo(code);
        assertThat(CharStreams.toString(new InputStreamReader(response.body(), StandardCharsets.UTF_8))).isEqualTo("Custom Body");
        assertThat(response.headers().get("My-Custom-Header")).containsExactly("my-custom-header-value");
    } catch (IOException e) {
        throw new SafeRuntimeException(e);
    }
}
Also used : Response(com.palantir.dialogue.Response) InputStreamReader(java.io.InputStreamReader) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) IOException(java.io.IOException)

Example 3 with Response

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

the class BlockingChannelAdapterTest method testFailure.

@Test
public void testFailure() {
    Channel channel = BlockingChannelAdapter.of((_endpoint, _request) -> {
        throw new SafeRuntimeException("expected");
    }, executor);
    ListenableFuture<Response> result = channel.execute(TestEndpoint.POST, Request.builder().build());
    Awaitility.waitAtMost(Duration.ofSeconds(3)).until(result::isDone);
    assertThatThrownBy(result::get).isInstanceOf(ExecutionException.class).hasCauseExactlyInstanceOf(SafeRuntimeException.class).hasRootCauseMessage("expected");
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Channel(com.palantir.dialogue.Channel) Test(org.junit.jupiter.api.Test)

Example 4 with Response

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

the class SocksProxyManualTest method testTls.

// @org.junit.jupiter.api.Test
void testTls() throws Exception {
    // ssh -D 8081 -v -N localhost
    SSLContext context = SslSocketFactories.createSslContext(TestConfigurations.SSL_CONFIG);
    Undertow undertow = Undertow.builder().addHttpsListener(8080, null, context, new ResponseCodeHandler(204)).build();
    undertow.start();
    try {
        ClientConfiguration configuration = withSocks("https://localhost:" + 8080, "127.0.0.1:8081");
        Channel channel = ApacheHttpClientChannels.create(configuration, "test");
        ListenableFuture<Response> future = channel.execute(TestEndpoint.GET, Request.builder().build());
        try (Response response = future.get()) {
            assertThat(response.code()).isEqualTo(204);
        }
    } finally {
        undertow.stop();
    }
}
Also used : ResponseCodeHandler(io.undertow.server.handlers.ResponseCodeHandler) Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) SSLContext(javax.net.ssl.SSLContext) Undertow(io.undertow.Undertow) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration)

Example 5 with Response

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

the class SocksProxyManualTest method testPlain.

// @org.junit.jupiter.api.Test
void testPlain() throws Exception {
    // ssh -D 8081 -v -N localhost
    Undertow undertow = Undertow.builder().addHttpListener(8080, null, new ResponseCodeHandler(204)).build();
    undertow.start();
    try {
        ClientConfiguration configuration = withSocks("http://localhost:" + 8080, "127.0.0.1:8081");
        Channel channel = ApacheHttpClientChannels.create(configuration, "test");
        ListenableFuture<Response> future = channel.execute(TestEndpoint.GET, Request.builder().build());
        try (Response response = future.get()) {
            assertThat(response.code()).isEqualTo(204);
        }
    } finally {
        undertow.stop();
    }
}
Also used : ResponseCodeHandler(io.undertow.server.handlers.ResponseCodeHandler) Response(com.palantir.dialogue.Response) Channel(com.palantir.dialogue.Channel) Undertow(io.undertow.Undertow) ClientConfiguration(com.palantir.conjure.java.client.config.ClientConfiguration)

Aggregations

Response (com.palantir.dialogue.Response)93 Test (org.junit.jupiter.api.Test)72 TestResponse (com.palantir.dialogue.TestResponse)56 EndpointChannel (com.palantir.dialogue.EndpointChannel)27 Channel (com.palantir.dialogue.Channel)24 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)22 Endpoint (com.palantir.dialogue.Endpoint)16 Request (com.palantir.dialogue.Request)15 ClientConfiguration (com.palantir.conjure.java.client.config.ClientConfiguration)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)9 Duration (java.time.Duration)9 Meter (com.codahale.metrics.Meter)8 IOException (java.io.IOException)8 Optional (java.util.Optional)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 QosException (com.palantir.conjure.java.api.errors.QosException)5 Arrays (java.util.Arrays)5 Stream (java.util.stream.Stream)5