Search in sources :

Example 21 with HttpRequest

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

the class H2PriorKnowledgeFeatureParityTest method trailersWithContentLength.

@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void trailersWithContentLength(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
    setUp(strategy, h2PriorKnowledge);
    final String expectedPayload = "Hello World!";
    final String expectedPayloadLength = valueOf(expectedPayload.length());
    final String expectedTrailer = "foo";
    final String expectedTrailerValue = "bar";
    final AtomicReference<HttpRequest> requestReceived = new AtomicReference<>();
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        requestReceived.set(request);
        return responseFactory.ok().addTrailer(expectedTrailer, expectedTrailerValue).addHeader(CONTENT_LENGTH, expectedPayloadLength).payloadBody(expectedPayload, textSerializerUtf8());
    });
        BlockingHttpClient client = forSingleAddress(HostAndPort.of((InetSocketAddress) serverContext.listenAddress())).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
        HttpResponse response = client.request(client.post("/").addTrailer(expectedTrailer, expectedTrailerValue).addHeader(CONTENT_LENGTH, expectedPayloadLength).payloadBody(expectedPayload, textSerializerUtf8()));
        assertThat(response.status(), is(OK));
        assertThat(response.payloadBody(textSerializerUtf8()), equalTo(expectedPayload));
        assertHeaders(h2PriorKnowledge, response.headers(), expectedPayloadLength);
        assertTrailers(response.trailers(), expectedTrailer, expectedTrailerValue);
        // Verify what server received:
        HttpRequest request = requestReceived.get();
        assertThat(request.payloadBody(textSerializerUtf8()), equalTo(expectedPayload));
        assertHeaders(h2PriorKnowledge, request.headers(), expectedPayloadLength);
        assertTrailers(request.trailers(), expectedTrailer, expectedTrailerValue);
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.emptyString(org.hamcrest.Matchers.emptyString) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with HttpRequest

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

the class HostHeaderHttpRequesterFilterTest method assertResponse.

private void assertResponse(BlockingHttpRequester requester, @Nullable String hostHeader, String expectedValue) throws Exception {
    final HttpRequest request = requester.get("/").version(httpVersionConfig.version());
    if (hostHeader != null) {
        request.setHeader(HOST, hostHeader);
    }
    HttpResponse response = requester.request(request);
    assertThat(response.status(), equalTo(OK));
    assertThat(response.version(), equalTo(httpVersionConfig.version()));
    // "Host" header is not required for HTTP/1.0. Therefore, we may expect "null" here.
    assertThat(response.payloadBody(textSerializerUtf8()), equalTo(httpVersionConfig == HttpVersionConfig.HTTP_1_0 && hostHeader == null ? "null" : expectedValue));
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) HttpResponse(io.servicetalk.http.api.HttpResponse)

Example 23 with HttpRequest

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

the class HttpClientOverrideOffloadingTest method reserveRespectsDisable.

@ParameterizedTest
@EnumSource(Params.class)
void reserveRespectsDisable(final Params params) throws Exception {
    setUp(params);
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    HttpRequest request = client.get("/");
    request.context().put(HTTP_EXECUTION_STRATEGY_KEY, this.overridingStrategy);
    client.reserveConnection(request).beforeOnSuccess(__ -> {
        if (isInvalidThread()) {
            errors.add(new AssertionError("Invalid thread found providing the connection. Thread: " + currentThread()));
        }
    }).toFuture().get().closeAsync().toFuture().get();
    assertThat("Unexpected errors: " + errors, errors, hasSize(0));
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) EnumSource(org.junit.jupiter.params.provider.EnumSource) Thread.currentThread(java.lang.Thread.currentThread) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) HttpRequest(io.servicetalk.http.api.HttpRequest) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) Executor(io.servicetalk.concurrent.api.Executor) HttpClient(io.servicetalk.http.api.HttpClient) Matchers.hasSize(org.hamcrest.Matchers.hasSize) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Nullable(javax.annotation.Nullable) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Predicate(java.util.function.Predicate) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) InetSocketAddress(java.net.InetSocketAddress) HTTP_EXECUTION_STRATEGY_KEY(io.servicetalk.http.api.HttpContextKeys.HTTP_EXECUTION_STRATEGY_KEY) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) IoExecutor(io.servicetalk.transport.api.IoExecutor) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Executors.newCachedThreadExecutor(io.servicetalk.concurrent.api.Executors.newCachedThreadExecutor) HostAndPort(io.servicetalk.transport.api.HostAndPort) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with HttpRequest

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

the class HttpClientOverrideOffloadingTest method requestRespectsDisable.

@ParameterizedTest
@EnumSource(Params.class)
void requestRespectsDisable(final Params params) throws Exception {
    setUp(params);
    ConcurrentLinkedQueue<AssertionError> errors = new ConcurrentLinkedQueue<>();
    HttpRequest request = client.get("/");
    request.context().put(HTTP_EXECUTION_STRATEGY_KEY, this.overridingStrategy);
    client.request(request).beforeOnSuccess(__ -> {
        if (isInvalidThread()) {
            errors.add(new AssertionError("Invalid thread called response. " + "Thread: " + currentThread()));
        }
    }).toFuture().get();
    assertThat("Unexpected errors: " + errors, errors, hasSize(0));
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with HttpRequest

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

the class AbstractBasicAuthSecurityContextFilterTest method getSecurityContextJson.

@Nullable
private String getSecurityContextJson(final String path, final boolean authenticated, final HttpResponseStatus expectedStatus) throws Exception {
    final HttpRequest req = httpClient.get(path).appendPathSegments("security-context");
    if (authenticated) {
        req.setHeader(AUTHORIZATION, TEST_AUTHORIZATION);
    }
    final HttpResponse res = httpClient.request(req);
    assertThat(res.status(), is(expectedStatus));
    return OK.equals(res.status()) ? res.payloadBody().toString(UTF_8) : null;
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) HttpResponse(io.servicetalk.http.api.HttpResponse) Nullable(javax.annotation.Nullable)

Aggregations

HttpRequest (io.servicetalk.http.api.HttpRequest)40 HttpResponse (io.servicetalk.http.api.HttpResponse)27 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)26 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 HttpClient (io.servicetalk.http.api.HttpClient)17 MethodSource (org.junit.jupiter.params.provider.MethodSource)15 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)14 ServerContext (io.servicetalk.transport.api.ServerContext)14 Test (org.junit.jupiter.api.Test)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