Search in sources :

Example 16 with BodySerDe

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");
}
Also used : BodySerDe(com.palantir.dialogue.BodySerDe) BinaryRequestBody(com.palantir.dialogue.BinaryRequestBody) RequestBody(com.palantir.dialogue.RequestBody) Test(org.junit.jupiter.api.Test)

Example 17 with BodySerDe

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();
}
Also used : ServiceException(com.palantir.conjure.java.api.errors.ServiceException) TestResponse(com.palantir.dialogue.TestResponse) SerializableError(com.palantir.conjure.java.api.errors.SerializableError) RemoteException(com.palantir.conjure.java.api.errors.RemoteException) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Example 18 with BodySerDe

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();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) TypeMarker(com.palantir.dialogue.TypeMarker) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Example 19 with BodySerDe

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);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) BodySerDe(com.palantir.dialogue.BodySerDe) Test(org.junit.jupiter.api.Test)

Example 20 with BodySerDe

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");
}
Also used : BodySerDe(com.palantir.dialogue.BodySerDe) BinaryRequestBody(com.palantir.dialogue.BinaryRequestBody) RequestBody(com.palantir.dialogue.RequestBody) Test(org.junit.jupiter.api.Test)

Aggregations

BodySerDe (com.palantir.dialogue.BodySerDe)20 Test (org.junit.jupiter.api.Test)20 TestResponse (com.palantir.dialogue.TestResponse)13 BinaryRequestBody (com.palantir.dialogue.BinaryRequestBody)7 RequestBody (com.palantir.dialogue.RequestBody)7 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 CloseRecordingInputStream (com.palantir.dialogue.CloseRecordingInputStream)2 TypeMarker (com.palantir.dialogue.TypeMarker)2 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ImmutableList (com.google.common.collect.ImmutableList)1 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)1 List (java.util.List)1