use of io.servicetalk.http.api.HttpHeaderNames.LOCATION in project servicetalk by apple.
the class RedirectingClientAndConnectionFilterTest method redirectFilterWithHostHeaderRelativeLocation.
@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterWithHostHeaderRelativeLocation(final RequesterType type, final SecurityType security) throws Exception {
setUp(security);
BlockingHttpRequester client = asBlockingRequester(createFilter(type, (responseFactory, request) -> {
if (request.requestTarget().equals("/")) {
return succeeded(responseFactory.permanentRedirect().addHeader(LOCATION, "/next"));
}
return succeeded(responseFactory.ok());
}, newFilterFactory()));
HttpRequest request = client.get("/").addHeader(HOST, "servicetalk.io");
HttpResponse response = client.request(request);
assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(OK));
}
use of io.servicetalk.http.api.HttpHeaderNames.LOCATION in project servicetalk by apple.
the class MultiAddressUrlHttpClientTest method beforeClass.
@BeforeAll
static void beforeClass() throws Exception {
afterClassCloseables = newCompositeCloseable();
client = afterClassCloseables.append(HttpClients.forMultiAddressUrl().followRedirects(new RedirectConfigBuilder().allowNonRelativeRedirects(true).build()).initializer((scheme, address, builder) -> builder.serviceDiscoverer(sdThatSupportsInvalidHostname())).buildStreaming());
httpService = (ctx, request, factory) -> {
if (HTTP_1_1.equals(request.version()) && !request.headers().contains(HOST)) {
return succeeded(factory.badRequest().setHeader(CONTENT_LENGTH, ZERO));
}
if (OPTIONS.equals(request.method()) || CONNECT.equals(request.method())) {
return succeeded(factory.ok().setHeader(CONTENT_LENGTH, ZERO));
}
StreamingHttpResponse response;
try {
HttpResponseStatus status = HttpResponseStatus.of(parseInt(request.path().substring(1)), "");
response = factory.newResponse(status);
final CharSequence locationHeader = request.headers().get(X_REQUESTED_LOCATION);
if (locationHeader != null) {
response.headers().set(LOCATION, locationHeader);
}
} catch (Exception e) {
response = factory.badRequest();
}
return succeeded(response.setHeader(CONTENT_LENGTH, ZERO).setHeader(X_RECEIVED_REQUEST_TARGET, request.requestTarget()));
};
final ServerContext serverCtx = startNewLocalServer(httpService, afterClassCloseables);
final HostAndPort serverHostAndPort = serverHostAndPort(serverCtx);
serverHost = serverHostAndPort.hostName();
serverPort = serverHostAndPort.port();
hostHeader = hostHeader(serverHostAndPort);
}
Aggregations