Search in sources :

Example 6 with HttpRequestMetaData

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

the class HttpRequestEncoderTest method contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff.

@Test
void contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = allocator.wrap(content);
    HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", new DefaultHttpHeadersFactory(false, false, false).newHeaders());
    request.headers().add(" " + CONNECTION + " ", " " + KEEP_ALIVE).add("  " + USER_AGENT + "   ", "    unit-test   ").add(CONTENT_LENGTH, valueOf(content.length));
    channel.writeOutbound(request);
    channel.writeOutbound(buffer.duplicate());
    ByteBuf byteBuf = channel.readOutbound();
    String actualMetaData = byteBuf.toString(US_ASCII);
    byteBuf.release();
    assertTrue(actualMetaData.contains("GET /some/path?foo=bar&baz=yyy HTTP/1.1" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains(" " + CONNECTION + " :  " + KEEP_ALIVE + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains("  " + USER_AGENT + "   :     unit-test   " + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.contains(CONTENT_LENGTH + ": " + buffer.readableBytes() + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    assertTrue(actualMetaData.endsWith("\r\n" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
    byteBuf = channel.readOutbound();
    assertEquals(buffer.toNioBuffer(), byteBuf.nioBuffer());
    byteBuf.release();
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Integer.toHexString(java.lang.Integer.toHexString) ByteBuf(io.netty.buffer.ByteBuf) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with HttpRequestMetaData

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

the class HttpRequestEncoderTest method variableNoTrailers.

@Test
void variableNoTrailers() {
    EmbeddedChannel channel = newEmbeddedChannel();
    byte[] content = new byte[128];
    ThreadLocalRandom.current().nextBytes(content);
    Buffer buffer = allocator.wrap(content);
    HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", INSTANCE.newHeaders());
    request.headers().add(CONNECTION, KEEP_ALIVE).add(USER_AGENT, "unit-test");
    channel.writeOutbound(request);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    verifyHttpRequest(channel, buffer, TransferEncoding.Variable, false);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with HttpRequestMetaData

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

the class PartitionedHttpClientTest method testPartitionByTarget.

@Test
void testPartitionByTarget() throws Exception {
    final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, req.requestTarget().substring(1));
    try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
        sdPublisher.onSubscribe(new TestSubscription());
        sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
        final HttpResponse httpResponse1 = clt.request(clt.get("/" + SRV_2));
        final HttpResponse httpResponse2 = clt.request(clt.get("/" + SRV_1));
        MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
        MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Matchers.hasToString(org.hamcrest.Matchers.hasToString) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) HttpRequestMethod(io.servicetalk.http.api.HttpRequestMethod) Publisher(io.servicetalk.concurrent.api.Publisher) ClientGroup(io.servicetalk.client.api.ClientGroup) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) AVAILABLE(io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) Objects.requireNonNull(java.util.Objects.requireNonNull) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) InetAddress.getLoopbackAddress(java.net.InetAddress.getLoopbackAddress) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) StreamingHttpRequestFactory(io.servicetalk.http.api.StreamingHttpRequestFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) Mockito.when(org.mockito.Mockito.when) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) StandardCharsets(java.nio.charset.StandardCharsets) PartitionedServiceDiscovererEvent(io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) List(java.util.List) PartitionAttributes(io.servicetalk.client.api.partition.PartitionAttributes) MatcherAssert(org.hamcrest.MatcherAssert) ExecutionContext(io.servicetalk.transport.api.ExecutionContext) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) DefaultStreamingHttpRequestResponseFactory(io.servicetalk.http.api.DefaultStreamingHttpRequestResponseFactory) HostAndPort(io.servicetalk.transport.api.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) DefaultPartitionAttributesBuilder(io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder) PartitionAttributesBuilder(io.servicetalk.client.api.partition.PartitionAttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 9 with HttpRequestMetaData

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

the class H2ToStH1ClientDuplexHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof HttpRequestMetaData) {
        closeHandler.protocolPayloadBeginOutbound(ctx);
        HttpRequestMetaData metaData = (HttpRequestMetaData) msg;
        HttpHeaders h1Headers = metaData.headers();
        waitForContinuation = REQ_EXPECT_CONTINUE.test(metaData);
        CharSequence host = h1Headers.getAndRemove(HOST);
        Http2Headers h2Headers = h1HeadersToH2Headers(h1Headers);
        if (host == null) {
            host = metaData.host();
            if (host != null) {
                h2Headers.authority(host);
            }
        } else {
            h2Headers.authority(host);
        }
        method = metaData.method();
        h2Headers.method(method.name());
        if (!CONNECT.equals(method)) {
            // The ":scheme" and ":path" pseudo-header fields MUST be omitted for CONNECT.
            // https://tools.ietf.org/html/rfc7540#section-8.3
            h2Headers.scheme(scheme.name());
            h2Headers.path(metaData.requestTarget());
        }
        try {
            writeMetaData(ctx, metaData, h2Headers, true, promise);
        } finally {
            final Http2StreamChannel streamChannel = (Http2StreamChannel) ctx.channel();
            final int streamId = streamChannel.stream().id();
            if (streamId > 0) {
                observer.streamIdAssigned(streamId);
            }
        }
    } else if (msg instanceof Buffer) {
        writeBuffer(ctx, (Buffer) msg, promise);
    } else if (msg instanceof HttpHeaders) {
        writeTrailers(ctx, msg, promise);
    } else {
        ctx.write(msg, promise);
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Http2Headers(io.netty.handler.codec.http2.Http2Headers) Http2StreamChannel(io.netty.handler.codec.http2.Http2StreamChannel)

Example 10 with HttpRequestMetaData

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

the class HttpRequestEncoderTest method variableNoTrailersNoContent.

@Test
void variableNoTrailersNoContent() {
    EmbeddedChannel channel = newEmbeddedChannel();
    HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", INSTANCE.newHeaders());
    request.headers().add(CONNECTION, KEEP_ALIVE).add(USER_AGENT, "unit-test");
    channel.writeOutbound(request);
    channel.writeOutbound(EMPTY_BUFFER);
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    verifyHttpRequest(channel, EMPTY_BUFFER, TransferEncoding.Variable, false);
    assertFalse(channel.finishAndReleaseAll());
}
Also used : HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpRequestMetaData (io.servicetalk.http.api.HttpRequestMetaData)16 Test (org.junit.jupiter.api.Test)12 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Buffer (io.servicetalk.buffer.api.Buffer)9 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)4 HttpHeaders (io.servicetalk.http.api.HttpHeaders)4 ClientGroup (io.servicetalk.client.api.ClientGroup)3 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)3 AVAILABLE (io.servicetalk.client.api.ServiceDiscovererEvent.Status.AVAILABLE)3 DefaultPartitionAttributesBuilder (io.servicetalk.client.api.internal.partition.DefaultPartitionAttributesBuilder)3 PartitionAttributes (io.servicetalk.client.api.partition.PartitionAttributes)3 PartitionAttributesBuilder (io.servicetalk.client.api.partition.PartitionAttributesBuilder)3 PartitionedServiceDiscovererEvent (io.servicetalk.client.api.partition.PartitionedServiceDiscovererEvent)3 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)3 Publisher (io.servicetalk.concurrent.api.Publisher)3 Single (io.servicetalk.concurrent.api.Single)3 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)3 TestSubscription (io.servicetalk.concurrent.api.TestSubscription)3 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)3