use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.
the class ConjureBodySerDeTest method testResponseNoContentType.
@Test
public void testResponseNoContentType() throws IOException {
BodySerDe serializers = conjureBodySerDe("application/json", "text/plain");
RequestBody body = serializers.serializer(TYPE).serialize("test");
assertThat(body.contentType()).isEqualTo("application/json");
}
use of com.palantir.dialogue.BodySerDe 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();
}
use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.
the class ConjureBodySerDeTest method testEmptyResponse_list.
@Test
public void testEmptyResponse_list() {
BodySerDe serde = DefaultConjureRuntime.builder().build().bodySerDe();
List<String> result = serde.deserializer(new TypeMarker<List<String>>() {
}).deserialize(new TestResponse().code(204));
assertThat(result).isEmpty();
}
use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.
the class ConjureBodySerDeTest method testEmptyResponse_success.
@Test
public void testEmptyResponse_success() {
TestResponse response = new TestResponse().code(204);
BodySerDe serializers = conjureBodySerDe("application/json");
serializers.emptyBodyDeserializer().deserialize(response);
}
use of com.palantir.dialogue.BodySerDe in project dialogue by palantir.
the class ConjureBodySerDeTest method testAcceptBasedOnWeight.
@Test
public void testAcceptBasedOnWeight() throws IOException {
Encoding json = new StubEncoding("application/json");
Encoding plain = new StubEncoding("text/plain");
BodySerDe serializers = new ConjureBodySerDe(ImmutableList.of(WeightedEncoding.of(plain, .5), WeightedEncoding.of(json, 1)), ErrorDecoder.INSTANCE, Encodings.emptyContainerDeserializer(), DefaultConjureRuntime.DEFAULT_SERDE_CACHE_SPEC);
// first encoding is default
RequestBody body = serializers.serializer(TYPE).serialize("test");
assertThat(body.contentType()).isEqualTo(plain.getContentType());
assertThat(serializers.deserializer(TYPE).accepts()).hasValue("application/json, text/plain");
}
Aggregations