Search in sources :

Example 11 with HttpRequest

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

the class PayloadBodyModificationsTest method aggregatedExpandOriginalBuffer.

@Test
void aggregatedExpandOriginalBuffer() throws Exception {
    HttpRequest request = streamingHttpClient().asClient().post(SVC_ECHO);
    Buffer payload = request.payloadBody();
    payload.writeAscii(CONTENT);
    assertResponse(makeRequest(request.toStreamingRequest()), request.version(), OK, CONTENT);
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Buffer(io.servicetalk.buffer.api.Buffer) Test(org.junit.jupiter.api.Test)

Example 12 with HttpRequest

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

the class RedirectingClientAndConnectionFilterTest method redirectFilterNoHostHeaderAbsoluteLocation.

@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterNoHostHeaderAbsoluteLocation(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, format("http://%s/next", hostHeader(HostAndPort.of(remoteAddress())))));
        }
        return succeeded(responseFactory.ok());
    }, newFilterFactory()));
    HttpRequest request = client.get("/");
    HttpResponse response = client.request(request);
    assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
    response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
    assertThat(response.status(), equalTo(OK));
    // HTTP/1.0 doesn't support HOST => we can not infer that the absolute-form location is relative, don't redirect
    response = client.request(client.get("/").version(HTTP_1_0).addHeader("X-REDIRECT", "TRUE"));
    assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
}
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 13 with HttpRequest

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

the class RedirectingClientAndConnectionFilterTest method redirectFilterNoHostHeaderRelativeLocation.

@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterNoHostHeaderRelativeLocation(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("/");
    HttpResponse response = client.request(request);
    assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
    response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
    assertThat(response.status(), equalTo(OK));
    // HTTP/1.0 doesn't support HOST, ensure that we don't get any errors and perform relative redirect
    response = client.request(client.get("/").version(HTTP_1_0).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 14 with HttpRequest

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

the class RequestResponseContextTest method testAsyncAggregated.

@Test
void testAsyncAggregated() throws Exception {
    setUp(Api.AsyncAggregated);
    String requestContextValue = randomString(10);
    HttpClient client = streamingHttpClient().asClient();
    HttpRequest request = client.get(SVC_ECHO);
    request.context().put(CLIENT_REQUEST_KEY, singletonList(requestContextValue));
    HttpResponse response = client.request(request).toFuture().get();
    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) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) 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 15 with HttpRequest

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

the class MalformedDataAfterHttpMessageTest method afterRequest.

@Test
void afterRequest() throws Exception {
    try (ServerContext server = stServer();
        BlockingHttpClient client = stClient(server.listenAddress())) {
        Buffer malformedBody = client.executionContext().bufferAllocator().fromAscii(CONTENT).writeShort(// malformed data at the end of the request msg
        0);
        HttpRequest request = client.post("/").setHeader(CONTENT_LENGTH, valueOf(CONTENT.length())).setHeader(CONTENT_TYPE, TEXT_PLAIN).payloadBody(malformedBody);
        ReservedBlockingHttpConnection connection = client.reserveConnection(request);
        CountDownLatch connectionClosedLatch = new CountDownLatch(1);
        connection.connectionContext().onClose().whenFinally(connectionClosedLatch::countDown).subscribe();
        assertThrows(IOException.class, () -> connection.request(request));
        // Server should close the connection:
        connectionClosedLatch.await();
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpRequest(io.servicetalk.http.api.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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