Search in sources :

Example 6 with TestResponse

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

the class ConjureBodySerDeTest method testEmptyResponse_list_raw.

@Test
@SuppressWarnings("rawtypes")
public void testEmptyResponse_list_raw() {
    BodySerDe serde = DefaultConjureRuntime.builder().build().bodySerDe();
    List result = serde.deserializer(new TypeMarker<List>() {
    }).deserialize(new TestResponse().code(204));
    assertThat(result).isEmpty();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) TypeMarker(com.palantir.dialogue.TypeMarker) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Example 7 with TestResponse

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

the class DefaultClientsTest method testOptionalBinaryResponse_inputStreamRemainsUnclosed.

@ParameterizedTest
@EnumSource(CallType.class)
public void testOptionalBinaryResponse_inputStreamRemainsUnclosed(CallType callType) throws IOException {
    when(channel.execute(eq(endpoint), any())).thenReturn(responseFuture);
    ListenableFuture<Optional<InputStream>> future = call(callType, Request.builder().build(), bodySerde.optionalInputStreamDeserializer());
    TestResponse testResponse = new TestResponse().contentType("application/octet-stream");
    responseFuture.set(testResponse);
    Optional<InputStream> maybeInputStream = Futures.getUnchecked(future);
    try (CloseRecordingInputStream inputStream = (CloseRecordingInputStream) maybeInputStream.get()) {
        assertThat(inputStream.available()).describedAs("Content should be empty").isEqualTo(0);
        inputStream.assertNotClosed();
        assertThat(testResponse.isClosed()).describedAs("Response").isFalse();
    }
    assertThat(testResponse.body().isClosed()).describedAs("User has closed it now").isTrue();
    assertThat(testResponse.isClosed()).describedAs("Response#close was never called, but no big deal because the body is the only resource worth" + " closing").isFalse();
}
Also used : Optional(java.util.Optional) TestResponse(com.palantir.dialogue.TestResponse) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) InputStream(java.io.InputStream) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with TestResponse

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

the class ContentDecodingChannelTest method testDecoding.

@Test
public void testDecoding() throws Exception {
    byte[] expected = new byte[] { 1, 2, 3, 4 };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (GZIPOutputStream compressor = new GZIPOutputStream(out)) {
        compressor.write(expected);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    Response response = new ContentDecodingChannel(_request -> Futures.immediateFuture(new TestResponse(out.toByteArray()).withHeader("content-encoding", "gzip").withHeader("content-length", Integer.toString(out.size())))).execute(Request.builder().build()).get();
    assertThat(response.headers().get("content-encoding")).isEmpty();
    assertThat(ByteStreams.toByteArray(response.body())).containsExactly(expected);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) GZIPOutputStream(java.util.zip.GZIPOutputStream) TestResponse(com.palantir.dialogue.TestResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 9 with TestResponse

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

the class ContentDecodingChannelTest method testDecoding_delayedFailure.

@Test
public void testDecoding_delayedFailure() throws Exception {
    Response response = new ContentDecodingChannel(_request -> Futures.immediateFuture(// Will fail because it's not valid gzip content
    new TestResponse(new byte[] { 1, 2, 3, 4 }).withHeader("content-encoding", "gzip"))).execute(Request.builder().build()).get();
    assertThat(response.headers().get("content-encoding")).isEmpty();
    assertThatThrownBy(response.body()::read).isInstanceOf(IOException.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) Test(org.junit.jupiter.api.Test)

Example 10 with TestResponse

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

the class RetryingChannelTest method retries_500s_for_put.

@Test
public void retries_500s_for_put() throws Exception {
    when(channel.execute(any())).thenReturn(Futures.immediateFuture(new TestResponse().code(500))).thenReturn(Futures.immediateFuture(new TestResponse().code(200)));
    EndpointChannel retryer = new RetryingChannel(channel, TestEndpoint.PUT, "my-channel", 3, Duration.ZERO, ClientConfiguration.ServerQoS.AUTOMATIC_RETRY, ClientConfiguration.RetryOnTimeout.DISABLED);
    ListenableFuture<Response> response = retryer.execute(REQUEST);
    assertThat(response).isDone();
    assertThat(response.get().code()).isEqualTo(200);
    verify(channel, times(2)).execute(REQUEST);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestResponse(com.palantir.dialogue.TestResponse) EndpointChannel(com.palantir.dialogue.EndpointChannel) Test(org.junit.jupiter.api.Test)

Aggregations

TestResponse (com.palantir.dialogue.TestResponse)31 Test (org.junit.jupiter.api.Test)26 BodySerDe (com.palantir.dialogue.BodySerDe)13 Response (com.palantir.dialogue.Response)13 EndpointChannel (com.palantir.dialogue.EndpointChannel)7 CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)4 InputStream (java.io.InputStream)4 Request (com.palantir.dialogue.Request)3 TypeMarker (com.palantir.dialogue.TypeMarker)3 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)3 IOException (java.io.IOException)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 RemoteException (com.palantir.conjure.java.api.errors.RemoteException)2 SerializableError (com.palantir.conjure.java.api.errors.SerializableError)2 ServiceException (com.palantir.conjure.java.api.errors.ServiceException)2 Channel (com.palantir.dialogue.Channel)2 Endpoint (com.palantir.dialogue.Endpoint)2 Random (java.util.Random)2 Meter (com.codahale.metrics.Meter)1