Search in sources :

Example 11 with HttpRequestMetaData

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

the class PartitionedHttpClientTest method testPartitionByHeader.

@Test
void testPartitionByHeader() throws Exception {
    final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, requireNonNull(req.headers().get(X_SERVER)).toString());
    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("/").addHeader(X_SERVER, SRV_2));
        final HttpResponse httpResponse2 = clt.request(clt.get("/").addHeader(X_SERVER, 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 12 with HttpRequestMetaData

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

the class PartitionedHttpClientTest method testPartitionByLeader.

@Test
void testPartitionByLeader() throws Exception {
    final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_LEADER, true);
    try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).buildBlocking()) {
        sdPublisher.onSubscribe(new TestSubscription());
        sdPublisher.onNext(new TestPSDE(SRV_1, false, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, true, (InetSocketAddress) srv2.listenAddress()));
        final HttpResponse httpResponse1 = clt.request(clt.get("/foo"));
        final HttpResponse httpResponse2 = clt.request(clt.get("/bar"));
        MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
        MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_2));
    }
}
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 13 with HttpRequestMetaData

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

the class HttpRequestDecoderTest method assertRequestLine.

private static HttpRequestMetaData assertRequestLine(HttpRequestMethod expectedMethod, String expectedRequestTarget, HttpProtocolVersion expectedVersion, EmbeddedChannel channel) {
    HttpRequestMetaData request = channel.readInbound();
    assertThat(request.method(), equalTo(expectedMethod));
    assertThat(request.requestTarget(), equalTo(expectedRequestTarget));
    assertThat(request.version(), equalTo(expectedVersion));
    return request;
}
Also used : HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData)

Example 14 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)

Example 15 with HttpRequestMetaData

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

the class HttpRequestEncoderTest method withContentLengthAndChunked.

@Test
void withContentLengthAndChunked() {
    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").add(CONTENT_LENGTH, valueOf(content.length)).add(TRANSFER_ENCODING, CHUNKED);
    channel.writeOutbound(request);
    channel.writeOutbound(buffer.duplicate());
    channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
    String metaData = verifyHttpRequest(channel, buffer, TransferEncoding.Chunked, false);
    assertFalse(metaData.contains(CONTENT_LENGTH), "Unexpected content-length header in meta-data while chunked encoding is used: " + metaData);
    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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpRequestMetaData (io.servicetalk.http.api.HttpRequestMetaData)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 Test (org.junit.jupiter.api.Test)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Buffer (io.servicetalk.buffer.api.Buffer)10 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