Search in sources :

Example 16 with HttpResponse

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

the class HttpSerializerErrorTest method serializationMapThrowsPropagatesToClient.

@ParameterizedTest
@MethodSource("executors")
void serializationMapThrowsPropagatesToClient(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
    };
    HttpStreamingSerializerDeserializer<Map<String, Object>> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(mapType));
    HttpSerializerDeserializer<Map<String, Object>> serializer = HttpSerializers.jsonSerializer(JACKSON.serializerDeserializer(mapType));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenAndAwait((ctx, request, responseFactory) -> responseFactory.ok().toStreamingResponse().payloadBody(request.toStreamingRequest().payloadBody(streamingSerializer).map(result -> {
        throw DELIBERATE_EXCEPTION;
    }), streamingSerializer).toResponse());
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(emptyMap(), serializer));
        assertEquals(INTERNAL_SERVER_ERROR, resp.status());
    }
}
Also used : HttpSerializers.jsonStreamingSerializer(io.servicetalk.http.api.HttpSerializers.jsonStreamingSerializer) SerializationException(io.servicetalk.serializer.api.SerializationException) BAD_REQUEST(io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST) HttpPayloadWriter(io.servicetalk.http.api.HttpPayloadWriter) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Collections.emptyMap(java.util.Collections.emptyMap) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) JACKSON(io.servicetalk.data.jackson.JacksonSerializerFactory.JACKSON) DEFAULT(io.servicetalk.http.netty.HttpTestExecutionStrategy.DEFAULT) Collection(java.util.Collection) HttpResponse(io.servicetalk.http.api.HttpResponse) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpStreamingSerializerDeserializer(io.servicetalk.http.api.HttpStreamingSerializerDeserializer) NO_OFFLOAD(io.servicetalk.http.netty.HttpTestExecutionStrategy.NO_OFFLOAD) HttpSerializers(io.servicetalk.http.api.HttpSerializers) HttpSerializerDeserializer(io.servicetalk.http.api.HttpSerializerDeserializer) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 17 with HttpResponse

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

the class HttpSerializerErrorTest method streamingDeserializationHeaderMismatch.

@ParameterizedTest
@MethodSource("executors")
void streamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenStreamingAndAwait((ctx, request, responseFactory) -> {
        try {
            return succeeded(responseFactory.ok().payloadBody(request.payloadBody(streamingSerializer), streamingSerializer));
        } catch (SerializationException e) {
            return succeeded(responseFactory.badRequest());
        }
    });
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
        assertEquals(BAD_REQUEST, resp.status());
    }
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with HttpResponse

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

the class HttpsProxyTest method testRequest.

@Test
void testRequest() throws Exception {
    assert client != null;
    final HttpResponse httpResponse = client.request(client.get("/path"));
    assertThat(httpResponse.status(), is(OK));
    assertThat(proxyTunnel.connectCount(), is(1));
    assertThat(httpResponse.payloadBody().toString(US_ASCII), is("host: " + serverAddress));
}
Also used : HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 19 with HttpResponse

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

the class HttpSerializationErrorTest method serializationMapThrowsPropagatesToClient.

@ParameterizedTest
@MethodSource("executors")
void serializationMapThrowsPropagatesToClient(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
    };
    HttpSerializerDeserializer<Map<String, Object>> httpSerializer = jsonSerializer(JACKSON.serializerDeserializer(mapType));
    HttpStreamingSerializerDeserializer<Map<String, Object>> httpStreamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(mapType));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenAndAwait((ctx, request, responseFactory) -> responseFactory.ok().toStreamingResponse().payloadBody(request.toStreamingRequest().payloadBody(httpStreamingSerializer).map(result -> {
        throw DELIBERATE_EXCEPTION;
    }), httpStreamingSerializer).toResponse());
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(emptyMap(), httpSerializer));
        assertEquals(INTERNAL_SERVER_ERROR, resp.status());
    }
}
Also used : BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Collections.emptyMap(java.util.Collections.emptyMap) HttpSerializers.jsonStreamingSerializer(io.servicetalk.http.api.HttpSerializers.jsonStreamingSerializer) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) JACKSON(io.servicetalk.data.jackson.JacksonSerializerFactory.JACKSON) DEFAULT(io.servicetalk.http.netty.HttpTestExecutionStrategy.DEFAULT) Collection(java.util.Collection) HttpResponse(io.servicetalk.http.api.HttpResponse) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) HttpStreamingSerializerDeserializer(io.servicetalk.http.api.HttpStreamingSerializerDeserializer) NO_OFFLOAD(io.servicetalk.http.netty.HttpTestExecutionStrategy.NO_OFFLOAD) HttpSerializers.jsonSerializer(io.servicetalk.http.api.HttpSerializers.jsonSerializer) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HttpSerializerDeserializer(io.servicetalk.http.api.HttpSerializerDeserializer) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) MethodSource(org.junit.jupiter.params.provider.MethodSource) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collections.emptyMap(java.util.Collections.emptyMap) Map(java.util.Map) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with HttpResponse

use of io.servicetalk.http.api.HttpResponse 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) 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)

Aggregations

HttpResponse (io.servicetalk.http.api.HttpResponse)95 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)53 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)42 ServerContext (io.servicetalk.transport.api.ServerContext)34 Test (org.junit.jupiter.api.Test)34 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)31 MethodSource (org.junit.jupiter.params.provider.MethodSource)26 HttpRequest (io.servicetalk.http.api.HttpRequest)25 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)25 HttpClient (io.servicetalk.http.api.HttpClient)23 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)18 Single (io.servicetalk.concurrent.api.Single)17 OK (io.servicetalk.http.api.HttpResponseStatus.OK)17 InetSocketAddress (java.net.InetSocketAddress)16 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)15 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)15 HttpClients (io.servicetalk.http.netty.HttpClients)14 Matchers.is (org.hamcrest.Matchers.is)14 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)13