Search in sources :

Example 6 with LOCATION

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));
}
Also used : HttpConnection(io.servicetalk.http.api.HttpConnection) HttpResponse(io.servicetalk.http.api.HttpResponse) LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) BlockingHttpRequester(io.servicetalk.http.api.BlockingHttpRequester) OK(io.servicetalk.http.api.HttpResponseStatus.OK) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter) String.format(java.lang.String.format) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FilterFactory(io.servicetalk.http.netty.ConditionalFilterFactory.FilterFactory) HttpRequest(io.servicetalk.http.api.HttpRequest) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HOST(io.servicetalk.http.api.HttpHeaderNames.HOST) PERMANENT_REDIRECT(io.servicetalk.http.api.HttpResponseStatus.PERMANENT_REDIRECT) AddressUtils.hostHeader(io.servicetalk.transport.netty.internal.AddressUtils.hostHeader) Matchers.equalTo(org.hamcrest.Matchers.equalTo) HttpClient(io.servicetalk.http.api.HttpClient) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AbstractHttpRequesterFilterTest(io.servicetalk.http.api.AbstractHttpRequesterFilterTest) HostAndPort(io.servicetalk.transport.api.HostAndPort) MethodSource(org.junit.jupiter.params.provider.MethodSource) HTTP_1_0(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_0) HttpRequest(io.servicetalk.http.api.HttpRequest) HttpResponse(io.servicetalk.http.api.HttpResponse) BlockingHttpRequester(io.servicetalk.http.api.BlockingHttpRequester) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with LOCATION

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);
}
Also used : LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) BAD_REQUEST(io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST) AfterAll(org.junit.jupiter.api.AfterAll) ZERO(io.servicetalk.http.api.HttpHeaderValues.ZERO) CREATED(io.servicetalk.http.api.HttpResponseStatus.CREATED) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) UNAUTHORIZED(io.servicetalk.http.api.HttpResponseStatus.UNAUTHORIZED) AddressUtils.hostHeader(io.servicetalk.transport.netty.internal.AddressUtils.hostHeader) BlockingTestUtils.awaitIndefinitelyNonNull(io.servicetalk.concurrent.api.BlockingTestUtils.awaitIndefinitelyNonNull) SEE_OTHER(io.servicetalk.http.api.HttpResponseStatus.SEE_OTHER) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Collection(java.util.Collection) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) CONNECT(io.servicetalk.http.api.HttpRequestMethod.CONNECT) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) InetSocketAddress(java.net.InetSocketAddress) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) GlobalDnsServiceDiscoverer.globalDnsServiceDiscoverer(io.servicetalk.http.netty.GlobalDnsServiceDiscoverer.globalDnsServiceDiscoverer) Matchers.is(org.hamcrest.Matchers.is) NOT_IMPLEMENTED(io.servicetalk.http.api.HttpResponseStatus.NOT_IMPLEMENTED) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) BAD_GATEWAY(io.servicetalk.http.api.HttpResponseStatus.BAD_GATEWAY) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HOST(io.servicetalk.http.api.HttpHeaderNames.HOST) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) OPTIONS(io.servicetalk.http.api.HttpRequestMethod.OPTIONS) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) MalformedURLException(java.net.MalformedURLException) Completable(io.servicetalk.concurrent.api.Completable) ACCEPTED(io.servicetalk.http.api.HttpResponseStatus.ACCEPTED) OK(io.servicetalk.http.api.HttpResponseStatus.OK) UnknownHostException(java.net.UnknownHostException) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) Integer.parseInt(java.lang.Integer.parseInt) ExecutionException(java.util.concurrent.ExecutionException) FORBIDDEN(io.servicetalk.http.api.HttpResponseStatus.FORBIDDEN) PERMANENT_REDIRECT(io.servicetalk.http.api.HttpResponseStatus.PERMANENT_REDIRECT) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) TestSingleSubscriber(io.servicetalk.concurrent.test.internal.TestSingleSubscriber) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) MOVED_PERMANENTLY(io.servicetalk.http.api.HttpResponseStatus.MOVED_PERMANENTLY) HostAndPort(io.servicetalk.transport.api.HostAndPort) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerContext(io.servicetalk.transport.api.ServerContext) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

LOCATION (io.servicetalk.http.api.HttpHeaderNames.LOCATION)7 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)6 HttpClient (io.servicetalk.http.api.HttpClient)6 HostAndPort (io.servicetalk.transport.api.HostAndPort)6 HOST (io.servicetalk.http.api.HttpHeaderNames.HOST)5 HttpRequest (io.servicetalk.http.api.HttpRequest)5 OK (io.servicetalk.http.api.HttpResponseStatus.OK)5 PERMANENT_REDIRECT (io.servicetalk.http.api.HttpResponseStatus.PERMANENT_REDIRECT)5 AddressUtils.hostHeader (io.servicetalk.transport.netty.internal.AddressUtils.hostHeader)5 String.format (java.lang.String.format)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 AbstractHttpRequesterFilterTest (io.servicetalk.http.api.AbstractHttpRequesterFilterTest)4 BlockingHttpRequester (io.servicetalk.http.api.BlockingHttpRequester)4 HttpConnection (io.servicetalk.http.api.HttpConnection)4 HTTP_1_0 (io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_0)4 HttpResponse (io.servicetalk.http.api.HttpResponse)4 FilterFactory (io.servicetalk.http.netty.ConditionalFilterFactory.FilterFactory)4 RedirectingHttpRequesterFilter (io.servicetalk.http.utils.RedirectingHttpRequesterFilter)4 Matchers.equalTo (org.hamcrest.Matchers.equalTo)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4