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/"));
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations