Search in sources :

Example 41 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class HttpRequestUriUtilsTest method authorityForm.

@Test
void authorityForm() {
    when(ctx.sslSession()).thenReturn(mock(SSLSession.class));
    final StreamingHttpRequest req = reqRespFactory.newRequest(CONNECT, "my.site.com:9876");
    assertThat(getEffectiveRequestUri(ctx, req, null, null, false), is("https://my.site.com:9876"));
    assertThat(getEffectiveRequestUri(ctx, req, "http", null, false), is("http://my.site.com:9876"));
    assertThat(getEffectiveRequestUri(ctx, req, null, "other.site.com:1234", false), is("https://other.site.com:1234"));
    assertThat(getEffectiveRequestUri(ctx, req, "http", "other.site.com:1234", false), is("http://other.site.com:1234"));
    assertThat(getEffectiveRequestUri(ctx, req, "http", "jsmith@other.site.com:1234", true), is("http://jsmith@other.site.com:1234"));
    assertThat(getEffectiveRequestUri(ctx, req, "http", "jsmith@other.site.com:1234", false), is("http://jsmith@other.site.com:1234"));
    assertThat(getEffectiveRequestUri(ctx, req, null, "jsmith@other.site.com:1234", false), is("https://jsmith@other.site.com:1234"));
    assertThat(getEffectiveRequestUri(ctx, req, null, "jsmith@other.site.com:1234", true), is("https://jsmith@other.site.com:1234"));
    assertThat(getBaseRequestUri(ctx, req, null, null, false), is("https://my.site.com:9876/"));
    assertThat(getBaseRequestUri(ctx, req, "http", null, false), is("http://my.site.com:9876/"));
    assertThat(getBaseRequestUri(ctx, req, null, "other.site.com:1234", false), is("https://other.site.com:1234/"));
    assertThat(getBaseRequestUri(ctx, req, "http", "other.site.com:1234", false), is("http://other.site.com:1234/"));
    assertThat(getBaseRequestUri(ctx, req, "http", "jsmith@other.site.com:1234", true), is("http://jsmith@other.site.com:1234/"));
    assertThat(getBaseRequestUri(ctx, req, "http", "jsmith@other.site.com:1234", false), is("http://jsmith@other.site.com:1234/"));
    assertThat(getBaseRequestUri(ctx, req, null, "jsmith@other.site.com:1234", false), is("https://jsmith@other.site.com:1234/"));
    assertThat(getBaseRequestUri(ctx, req, null, "jsmith@other.site.com:1234", true), is("https://jsmith@other.site.com:1234/"));
}
Also used : SSLSession(javax.net.ssl.SSLSession) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Test(org.junit.jupiter.api.Test)

Example 42 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class RedirectingHttpRequesterFilterTest method verifyRedirected.

private StreamingHttpRequest verifyRedirected(StreamingHttpClient client, StreamingHttpRequest request, boolean redirectsHeadersAndMessageBody, boolean absoluteForm) throws Exception {
    StreamingHttpRequest redirectedRequest = verifyResponse(client, request, OK, -1, 2, request.method());
    assertThat("Request didn't change", request, not(sameInstance(redirectedRequest)));
    if (redirectsHeadersAndMessageBody) {
        verifyHeadersAndMessageBodyRedirected(redirectedRequest);
    } else {
        verifyHeadersAndMessageBodyAreNotRedirected(redirectedRequest);
    }
    if (absoluteForm) {
        String scheme = redirectedRequest.scheme();
        assertThat("Unexpected scheme on redirected request", scheme, notNullValue());
        assertThat("Unexpected request-target of redirected request", redirectedRequest.requestTarget(), startsWith(scheme));
    } else {
        assertThat("Unexpected scheme on redirected request", redirectedRequest.scheme(), nullValue());
        assertThat("Unexpected request-target of redirected request", redirectedRequest.requestTarget(), startsWith("/"));
    }
    verifyRedirectResponsePayloadsDrained(true);
    return redirectedRequest;
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest)

Example 43 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class RedirectingHttpRequesterFilterTest method redirectFromAbsoluteFormToAbsoluteFormNonRelativeLocation.

@ParameterizedTest(name = "{displayName} [{index}] allowNonRelativeRedirects={0}")
@ValueSource(booleans = { true, false })
void redirectFromAbsoluteFormToAbsoluteFormNonRelativeLocation(boolean allowNonRelativeRedirects) throws Exception {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY, "http://non-relative.servicetalk.io"), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().allowNonRelativeRedirects(allowNonRelativeRedirects).build());
    StreamingHttpRequest request = newRequest(client, GET).requestTarget("http://servicetalk.io/path");
    if (allowNonRelativeRedirects) {
        verifyRedirected(client, request, false, true);
    } else {
        verifyDoesNotRedirect(client, request, MOVED_PERMANENTLY);
        verifyRedirectResponsePayloadsDrained(false);
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class RedirectingHttpRequesterFilterTest method verifyResponse.

private StreamingHttpRequest verifyResponse(StreamingHttpClient client, StreamingHttpRequest request, HttpResponseStatus expectedStatus, int expectedLocationN, int numberOfInvocations, @Nullable HttpRequestMethod expectedMethod) throws Exception {
    ArgumentCaptor<StreamingHttpRequest> requestCaptor = forClass(StreamingHttpRequest.class);
    StreamingHttpResponse response = client.request(request).toFuture().get();
    assertThat(response, is(notNullValue()));
    assertThat("Unexpected response status", response.status(), is(expectedStatus));
    if (expectedLocationN > 0) {
        CharSequence location = response.headers().get(LOCATION);
        assertThat("Unexpected location", location, notNullValue());
        assertThat("Unexpected location", location.toString(), endsWith("/location-" + expectedLocationN));
    } else {
        assertThat("Unexpected location", response.headers().get(LOCATION), nullValue());
    }
    verify(httpClient, times(numberOfInvocations)).request(requestCaptor.capture());
    StreamingHttpRequest redirectedRequest = requestCaptor.getValue();
    if (expectedMethod != null) {
        assertThat("Unexpected request method", redirectedRequest.method(), is(expectedMethod));
    }
    return redirectedRequest;
}
Also used : StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 45 with StreamingHttpRequest

use of io.servicetalk.http.api.StreamingHttpRequest in project servicetalk by apple.

the class RedirectingHttpRequesterFilterTest method crossSchemeRedirects.

private void crossSchemeRedirects(String fromScheme, String toScheme, boolean allowNonRelativeRedirects) throws Exception {
    when(httpClient.request(any())).thenReturn(redirectResponse(MOVED_PERMANENTLY, toScheme + "://servicetalk.io"), okResponse());
    StreamingHttpClient client = newClient(new RedirectConfigBuilder().allowNonRelativeRedirects(allowNonRelativeRedirects).build());
    StreamingHttpRequest request = newRequest(client, GET).requestTarget(fromScheme + "://servicetalk.io/path");
    if (allowNonRelativeRedirects) {
        verifyRedirected(client, request, false, true);
    } else {
        verifyDoesNotRedirect(client, request, MOVED_PERMANENTLY);
        verifyRedirectResponsePayloadsDrained(false);
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest)

Aggregations

StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)169 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)84 Test (org.junit.jupiter.api.Test)82 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)66 MethodSource (org.junit.jupiter.params.provider.MethodSource)47 Single (io.servicetalk.concurrent.api.Single)35 Buffer (io.servicetalk.buffer.api.Buffer)33 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)32 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)23 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)23 Nullable (javax.annotation.Nullable)22 InetSocketAddress (java.net.InetSocketAddress)21 OK (io.servicetalk.http.api.HttpResponseStatus.OK)20 StreamingHttpServiceFilter (io.servicetalk.http.api.StreamingHttpServiceFilter)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)18 HttpHeaders (io.servicetalk.http.api.HttpHeaders)17 ServerContext (io.servicetalk.transport.api.ServerContext)17 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)17