Search in sources :

Example 31 with Request

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

the class DialogueChannelTest method bad_channel_throwing_an_error_still_returns_a_future.

@Test
public void bad_channel_throwing_an_error_still_returns_a_future() {
    Channel badUserImplementation = new Channel() {

        @Override
        public ListenableFuture<Response> execute(Endpoint _endpoint, Request _request) {
            throw new NoClassDefFoundError("something is broken");
        }
    };
    channel = DialogueChannel.builder().channelName("my-channel").clientConfiguration(stubConfig).factory(_args -> badUserImplementation).build();
    // this should never throw
    ListenableFuture<Response> future = channel.execute(endpoint, request);
    // only when we access things do we allow exceptions
    assertThatThrownBy(() -> Futures.getUnchecked(future)).hasCauseInstanceOf(NoClassDefFoundError.class);
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with Request

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

the class StickyAttachmentsTest method validate_is_passthrough_if_no_request_sticky_token_attachment.

@Test
public void validate_is_passthrough_if_no_request_sticky_token_attachment() {
    Request request = Request.builder().build();
    when(delegate.maybeExecute(TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED)).thenReturn(Optional.of(responseSettableFuture));
    assertThat(StickyAttachments.maybeExecuteAndValidateRequestStickyToken(stickyTokenHandler, TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED)).hasValue(responseSettableFuture);
}
Also used : Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 33 with Request

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

the class StickyAttachmentsTest method validate_throws_and_closes_response_if_attachments_are_not_present.

@Test
public void validate_throws_and_closes_response_if_attachments_are_not_present() {
    Request request = Request.builder().build();
    StickyAttachments.requestStickyToken(request);
    when(delegate.maybeExecute(TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED)).thenReturn(Optional.of(responseSettableFuture));
    ListenableFuture<Response> responseListenableFuture = StickyAttachments.maybeExecuteAndValidateRequestStickyToken(delegate, TestEndpoint.GET, request, LimitEnforcement.DEFAULT_ENABLED).get();
    TestResponse response = TestResponse.withBody(null);
    responseSettableFuture.set(response);
    assertThatThrownBy(() -> Futures.getDone(responseListenableFuture)).isInstanceOf(ExecutionException.class).hasRootCauseExactlyInstanceOf(SafeRuntimeException.class).hasRootCauseMessage("Requested sticky token on request but token not present on response");
    assertThat(response.isClosed()).isTrue();
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) TestResponse(com.palantir.dialogue.TestResponse) Request(com.palantir.dialogue.Request) Test(org.junit.jupiter.api.Test)

Example 34 with Request

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

the class UrlBuilderTest method testQueryParamOrder_requestRendering.

@Test
public void testQueryParamOrder_requestRendering() throws Exception {
    String key1 = UUID.randomUUID().toString();
    String value1 = UUID.randomUUID().toString();
    String key2 = UUID.randomUUID().toString();
    String value2 = UUID.randomUUID().toString();
    String key3 = UUID.randomUUID().toString();
    String value3 = UUID.randomUUID().toString();
    String key4 = UUID.randomUUID().toString();
    String value4 = UUID.randomUUID().toString();
    String key5 = UUID.randomUUID().toString();
    String value5 = UUID.randomUUID().toString();
    Request request = Request.builder().putQueryParams(key1, value1).putQueryParams(key2, value2).putQueryParams(key3, value3).putQueryParams(key4, value4).putQueryParams(key5, value5).build();
    BaseUrl baseUrl = BaseUrl.of(new URL("http://foo:42/bar"));
    String target = baseUrl.render(TestEndpoint.GET, request).toString();
    assertThat(target).isEqualTo(String.format("http://foo:42/bar?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s", key1, value1, key2, value2, key3, value3, key4, value4, key5, value5));
}
Also used : Request(com.palantir.dialogue.Request) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 35 with Request

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

the class DefaultClientsTest method testAddsAcceptHeader.

@ParameterizedTest
@EnumSource(CallType.class)
public void testAddsAcceptHeader(CallType callType) {
    String expectedAccept = "application/json";
    Request request = Request.builder().build();
    when(stringDeserializer.deserialize(eq(response))).thenReturn("value");
    when(stringDeserializer.accepts()).thenReturn(Optional.of(expectedAccept));
    when(channel.execute(eq(endpoint), any())).thenReturn(Futures.immediateFuture(response));
    ListenableFuture<String> result = call(callType, request);
    assertStringResult(callType, result);
    verify(channel).execute(eq(endpoint), requestCaptor.capture());
    assertThat(requestCaptor.getValue().headerParams().get(HttpHeaders.ACCEPT)).containsExactly(expectedAccept);
}
Also used : Request(com.palantir.dialogue.Request) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Request (com.palantir.dialogue.Request)40 Test (org.junit.jupiter.api.Test)21 Channel (com.palantir.dialogue.Channel)14 Endpoint (com.palantir.dialogue.Endpoint)14 Response (com.palantir.dialogue.Response)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 TestResponse (com.palantir.dialogue.TestResponse)8 Test (org.junit.Test)8 TestEndpoint (com.palantir.dialogue.TestEndpoint)6 Optional (java.util.Optional)6 Futures (com.google.common.util.concurrent.Futures)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 RequestBody (com.palantir.dialogue.RequestBody)5 UrlBuilder (com.palantir.dialogue.UrlBuilder)5 OutputStream (java.io.OutputStream)5 Duration (java.time.Duration)5 ExecutionException (java.util.concurrent.ExecutionException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 EnumSource (org.junit.jupiter.params.provider.EnumSource)5 ImmutableList (com.google.common.collect.ImmutableList)4