Search in sources :

Example 36 with HttpRequest

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

the class RedirectingClientAndConnectionFilterTest method redirectFilterWithHostHeaderAbsoluteLocation.

@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterWithHostHeaderAbsoluteLocation(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, "http://servicetalk.io:80/next"));
        }
        return succeeded(responseFactory.ok());
    }, newFilterFactory()));
    HttpRequest request = client.get("/").addHeader(HOST, "servicetalk.io:80");
    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) HostHeaderHttpRequesterFilter(io.servicetalk.http.utils.HostHeaderHttpRequesterFilter) 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 37 with HttpRequest

use of io.servicetalk.http.api.HttpRequest 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) HostHeaderHttpRequesterFilter(io.servicetalk.http.utils.HostHeaderHttpRequesterFilter) 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 38 with HttpRequest

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

the class RequestResponseContextTest method testBlockingAggregated.

@Test
void testBlockingAggregated() throws Exception {
    setUp(Api.BlockingAggregated);
    String requestContextValue = randomString(10);
    BlockingHttpClient client = streamingHttpClient().asBlockingClient();
    HttpRequest request = client.get(SVC_ECHO);
    request.context().put(CLIENT_REQUEST_KEY, singletonList(requestContextValue));
    HttpResponse response = client.request(request);
    assertResponse(response.toStreamingResponse(), HTTP_1_1, OK, 0);
    assertContext(requestContextValue, request, response);
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) BlockingStreamingHttpRequest(io.servicetalk.http.api.BlockingStreamingHttpRequest) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) BlockingStreamingHttpResponse(io.servicetalk.http.api.BlockingStreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 39 with HttpRequest

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

the class IoUringTest method ioUringIsAvailableOnLinux.

@Test
@EnabledOnOs(value = { LINUX })
void ioUringIsAvailableOnLinux() throws Exception {
    EventLoopAwareNettyIoExecutor ioUringExecutor = null;
    try {
        IoUringUtils.tryIoUring(true);
        assertTrue(IoUringUtils.isAvailable());
        IOUring.ensureAvailability();
        ioUringExecutor = NettyIoExecutors.createIoExecutor(2, "io-uring");
        assertThat(ioUringExecutor.eventLoopGroup(), is(instanceOf(IOUringEventLoopGroup.class)));
        try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).ioExecutor(ioUringExecutor).listenStreamingAndAwait(new TestServiceStreaming());
            BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(ioUringExecutor).buildBlocking()) {
            HttpRequest request = client.post(SVC_ECHO).payloadBody("bonjour!", textSerializerUtf8());
            HttpResponse response = client.request(request);
            assertThat(response.status(), is(OK));
            assertThat(response.payloadBody(textSerializerUtf8()), is("bonjour!"));
        }
    } finally {
        IoUringUtils.tryIoUring(false);
        if (ioUringExecutor != null) {
            ioUringExecutor.closeAsync().toFuture().get();
            assertTrue(ioUringExecutor.eventLoopGroup().isShutdown());
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) EventLoopAwareNettyIoExecutor(io.servicetalk.transport.netty.internal.EventLoopAwareNettyIoExecutor) HttpResponse(io.servicetalk.http.api.HttpResponse) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 40 with HttpRequest

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

the class ManualRedirectClient method main.

public static void main(String... args) throws Exception {
    try (HttpClient secureClient = HttpClients.forSingleAddress("localhost", SECURE_SERVER_PORT).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).build()).build()) {
        try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).build()) {
            System.out.println("- Redirect of a GET request with a custom header:");
            HttpRequest originalGet = client.get("/non-relative").addHeader(CUSTOM_HEADER, "value");
            client.request(originalGet).flatMap(response -> {
                if (response.status().statusClass() == REDIRECTION_3XX) {
                    CharSequence location = response.headers().get(LOCATION);
                    HttpClient redirectClient = lookupClient(location, client, secureClient);
                    return redirectClient.request(redirectClient.newRequest(originalGet.method(), location.toString()).addHeader(CUSTOM_HEADER, originalGet.headers().get(CUSTOM_HEADER)));
                }
                // Decided not to follow redirect, return the original response or an error:
                return succeeded(response);
            }).whenOnSuccess(resp -> {
                System.out.println(resp.toString((name, value) -> value));
                System.out.println(resp.payloadBody(textSerializerAscii()));
                System.out.println();
            }).toFuture().get();
            System.out.println("- Redirect of a POST request with a payload body:");
            HttpRequest originalPost = client.post("/non-relative").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"));
            client.request(originalPost).flatMap(response -> {
                if (response.status().statusClass() == REDIRECTION_3XX) {
                    CharSequence location = response.headers().get(LOCATION);
                    HttpClient redirectClient = lookupClient(location, client, secureClient);
                    return redirectClient.request(redirectClient.newRequest(originalPost.method(), location.toString()).payloadBody(originalPost.payloadBody()));
                }
                // Decided not to follow redirect, return the original response or an error:
                return succeeded(response);
            }).whenOnSuccess(resp -> {
                System.out.println(resp.toString((name, value) -> value));
                System.out.println(resp.payloadBody(textSerializerAscii()));
            }).toFuture().get();
        }
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) HttpSerializers.textSerializerAscii(io.servicetalk.http.api.HttpSerializers.textSerializerAscii) REDIRECTION_3XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.REDIRECTION_3XX) CUSTOM_HEADER(io.servicetalk.examples.http.redirects.RedirectingServer.CUSTOM_HEADER) LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.SECURE_SERVER_PORT) NON_SECURE_SERVER_PORT(io.servicetalk.examples.http.redirects.RedirectingServer.NON_SECURE_SERVER_PORT) HttpRequest(io.servicetalk.http.api.HttpRequest) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpClient(io.servicetalk.http.api.HttpClient) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) HttpClients(io.servicetalk.http.netty.HttpClients) Nullable(javax.annotation.Nullable) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpClient(io.servicetalk.http.api.HttpClient) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder)

Aggregations

HttpRequest (io.servicetalk.http.api.HttpRequest)42 HttpResponse (io.servicetalk.http.api.HttpResponse)28 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)26 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 Test (org.junit.jupiter.api.Test)19 HttpClient (io.servicetalk.http.api.HttpClient)17 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 ServerContext (io.servicetalk.transport.api.ServerContext)15 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)14 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)11 HostAndPort (io.servicetalk.transport.api.HostAndPort)10 InetSocketAddress (java.net.InetSocketAddress)10 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)10 OK (io.servicetalk.http.api.HttpResponseStatus.OK)9 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)9 Buffer (io.servicetalk.buffer.api.Buffer)8 Nullable (javax.annotation.Nullable)8 Matchers.emptyString (org.hamcrest.Matchers.emptyString)8 ReservedBlockingHttpConnection (io.servicetalk.http.api.ReservedBlockingHttpConnection)5