use of com.palantir.dialogue.TestResponse 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();
}
use of com.palantir.dialogue.TestResponse 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();
}
use of com.palantir.dialogue.TestResponse in project dialogue by palantir.
the class ConjureBodySerDeTest method testEmptyResponse_failure.
@Test
public void testEmptyResponse_failure() {
TestResponse response = new TestResponse().code(400);
ServiceException serviceException = new ServiceException(ErrorType.INVALID_ARGUMENT);
SerializableError serialized = SerializableError.forException(serviceException);
errorDecoder = mock(ErrorDecoder.class);
when(errorDecoder.isError(response)).thenReturn(true);
when(errorDecoder.decode(response)).thenReturn(new RemoteException(serialized, 400));
BodySerDe serializers = conjureBodySerDe("application/json");
assertThatExceptionOfType(RemoteException.class).isThrownBy(() -> serializers.emptyBodyDeserializer().deserialize(response));
}
use of com.palantir.dialogue.TestResponse in project dialogue by palantir.
the class ConjureBodySerDeTest method testRequestOptionalEmpty.
@Test
public void testRequestOptionalEmpty() {
TestResponse response = new TestResponse().code(204);
BodySerDe serializers = conjureBodySerDe("application/json");
Optional<String> value = serializers.deserializer(OPTIONAL_TYPE).deserialize(response);
assertThat(value).isEmpty();
}
use of com.palantir.dialogue.TestResponse in project dialogue by palantir.
the class ConjureBodySerDeTest method testErrorsDecoded.
@Test
public void testErrorsDecoded() {
TestResponse response = new TestResponse().code(400);
ServiceException serviceException = new ServiceException(ErrorType.INVALID_ARGUMENT);
SerializableError serialized = SerializableError.forException(serviceException);
errorDecoder = mock(ErrorDecoder.class);
when(errorDecoder.isError(response)).thenReturn(true);
when(errorDecoder.decode(response)).thenReturn(new RemoteException(serialized, 400));
BodySerDe serializers = conjureBodySerDe("text/plain");
assertThatExceptionOfType(RemoteException.class).isThrownBy(() -> serializers.deserializer(TYPE).deserialize(response));
assertThat(response.isClosed()).describedAs("response should be closed").isTrue();
assertThat(response.body().isClosed()).describedAs("inputstream should be closed").isTrue();
}
Aggregations