Search in sources :

Example 1 with CloseRecordingInputStream

use of com.palantir.dialogue.CloseRecordingInputStream 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 2 with CloseRecordingInputStream

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

the class BinaryEncodingTest method testBinary.

@Test
public void testBinary() throws IOException {
    TestResponse response = new TestResponse().code(200).contentType("application/octet-stream");
    BodySerDe serializers = new ConjureBodySerDe(ImmutableList.of(WeightedEncoding.of(new ConjureBodySerDeTest.StubEncoding("application/json"))), ErrorDecoder.INSTANCE, Encodings.emptyContainerDeserializer(), DefaultConjureRuntime.DEFAULT_SERDE_CACHE_SPEC);
    InputStream deserialized = serializers.inputStreamDeserializer().deserialize(response);
    assertThat(deserialized.available()).isEqualTo(0);
    CloseRecordingInputStream rawInputStream = response.body();
    rawInputStream.assertNotClosed();
    assertThat(response.isClosed()).describedAs("response is unclosed initially").isFalse();
    deserialized.close();
    assertThat(response.isClosed()).describedAs("Response#close was never called, but no big deal because the body is the only resource worth" + " closing").isFalse();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) InputStream(java.io.InputStream) BodySerDe(com.palantir.dialogue.BodySerDe) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) Test(org.junit.jupiter.api.Test)

Example 3 with CloseRecordingInputStream

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

the class BinaryEncodingTest method testBinary_optional_present.

@Test
public void testBinary_optional_present() throws IOException {
    TestResponse response = new TestResponse().code(200).contentType("application/octet-stream");
    BodySerDe serializers = new ConjureBodySerDe(ImmutableList.of(WeightedEncoding.of(new ConjureBodySerDeTest.StubEncoding("application/json"))), ErrorDecoder.INSTANCE, Encodings.emptyContainerDeserializer(), DefaultConjureRuntime.DEFAULT_SERDE_CACHE_SPEC);
    Optional<InputStream> maybe = serializers.optionalInputStreamDeserializer().deserialize(response);
    assertThat(maybe).isPresent();
    InputStream deserialized = maybe.get();
    assertThat(deserialized.available()).isEqualTo(0);
    CloseRecordingInputStream rawInputStream = response.body();
    rawInputStream.assertNotClosed();
    assertThat(response.isClosed()).describedAs("response is unclosed initially").isFalse();
    deserialized.close();
    assertThat(response.isClosed()).describedAs("Response#close was never called, but no big deal because the body is the only resource worth" + " closing").isFalse();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) InputStream(java.io.InputStream) BodySerDe(com.palantir.dialogue.BodySerDe) CloseRecordingInputStream(com.palantir.dialogue.CloseRecordingInputStream) Test(org.junit.jupiter.api.Test)

Example 4 with CloseRecordingInputStream

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

the class DefaultClientsTest method testBinaryResponse_inputStreamRemainsUnclosed.

@ParameterizedTest
@EnumSource(CallType.class)
public void testBinaryResponse_inputStreamRemainsUnclosed(CallType callType) throws IOException {
    when(channel.execute(eq(endpoint), any())).thenReturn(responseFuture);
    ListenableFuture<InputStream> future = call(callType, Request.builder().build(), bodySerde.inputStreamDeserializer());
    TestResponse testResponse = new TestResponse().contentType("application/octet-stream");
    responseFuture.set(testResponse);
    try (CloseRecordingInputStream inputStream = (CloseRecordingInputStream) Futures.getUnchecked(future)) {
        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 : 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)

Aggregations

CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)4 TestResponse (com.palantir.dialogue.TestResponse)4 InputStream (java.io.InputStream)4 BodySerDe (com.palantir.dialogue.BodySerDe)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 Optional (java.util.Optional)1