Search in sources :

Example 21 with HostAndPort

use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.

the class HttpClientBuilderTest method sendRequestAndValidate.

private void sendRequestAndValidate(final Publisher<ServiceDiscovererEvent<InetSocketAddress>> sdPub) throws ExecutionException, InterruptedException {
    @SuppressWarnings("unchecked") ServiceDiscoverer<HostAndPort, InetSocketAddress, ServiceDiscovererEvent<InetSocketAddress>> disco = mock(ServiceDiscoverer.class);
    when(disco.discover(any())).thenReturn(sdPub.map(Collections::singletonList));
    StreamingHttpClient requester = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).serviceDiscoverer(disco).ioExecutor(CTX.ioExecutor()).executionStrategy(offloadNone()).buildStreaming();
    makeRequestValidateResponseAndClose(requester);
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) DefaultServiceDiscovererEvent(io.servicetalk.client.api.DefaultServiceDiscovererEvent) InetSocketAddress(java.net.InetSocketAddress)

Example 22 with HostAndPort

use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.

the class MixedFiltersTest method testSingleClient.

@ParameterizedTest(name = "{displayName} [{index}] filters={0}")
@MethodSource("arguments")
void testSingleClient(List<AbstractFactoryFilter> filters) throws Exception {
    String expected = filters.stream().map(AbstractFactoryFilter::toString).reduce((first, second) -> first + ',' + second).get();
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok())) {
        SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = HttpClients.forSingleAddress(serverHostAndPort(serverContext));
        filters.forEach(builder::appendConnectionFactoryFilter);
        try (BlockingHttpClient client = builder.buildBlocking()) {
            AtomicReference<StringBuilder> ref = new AtomicReference<>(new StringBuilder());
            AsyncContext.put(KEY, ref);
            HttpResponse response = client.request(client.get("/"));
            assertThat(response.status(), is(OK));
            assertThat(ref.toString(), is(equalTo(expected)));
        }
    }
}
Also used : FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Key.newKey(io.servicetalk.context.api.ContextMap.Key.newKey) Key(io.servicetalk.context.api.ContextMap.Key) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) Arrays.asList(java.util.Arrays.asList) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DelegatingConnectionFactory(io.servicetalk.client.api.DelegatingConnectionFactory) Nullable(javax.annotation.Nullable) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ConnectionFactory(io.servicetalk.client.api.ConnectionFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Single(io.servicetalk.concurrent.api.Single) Collection(java.util.Collection) ConnectionFactoryFilter(io.servicetalk.client.api.ConnectionFactoryFilter) HttpResponse(io.servicetalk.http.api.HttpResponse) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ContextMap(io.servicetalk.context.api.ContextMap) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportObserver(io.servicetalk.transport.api.TransportObserver) Matchers.is(org.hamcrest.Matchers.is) HostAndPort(io.servicetalk.transport.api.HostAndPort) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) InetSocketAddress(java.net.InetSocketAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerContext(io.servicetalk.transport.api.ServerContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 23 with HostAndPort

use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.

the class MultiAddressUrlHttpClientTest method beforeClass.

@BeforeAll
static void beforeClass() throws Exception {
    afterClassCloseables = newCompositeCloseable();
    client = afterClassCloseables.append(HttpClients.forMultiAddressUrl().followRedirects(new RedirectConfigBuilder().allowNonRelativeRedirects(true).build()).initializer((scheme, address, builder) -> builder.serviceDiscoverer(sdThatSupportsInvalidHostname())).buildStreaming());
    httpService = (ctx, request, factory) -> {
        if (HTTP_1_1.equals(request.version()) && !request.headers().contains(HOST)) {
            return succeeded(factory.badRequest().setHeader(CONTENT_LENGTH, ZERO));
        }
        if (OPTIONS.equals(request.method()) || CONNECT.equals(request.method())) {
            return succeeded(factory.ok().setHeader(CONTENT_LENGTH, ZERO));
        }
        StreamingHttpResponse response;
        try {
            HttpResponseStatus status = HttpResponseStatus.of(parseInt(request.path().substring(1)), "");
            response = factory.newResponse(status);
            final CharSequence locationHeader = request.headers().get(X_REQUESTED_LOCATION);
            if (locationHeader != null) {
                response.headers().set(LOCATION, locationHeader);
            }
        } catch (Exception e) {
            response = factory.badRequest();
        }
        return succeeded(response.setHeader(CONTENT_LENGTH, ZERO).setHeader(X_RECEIVED_REQUEST_TARGET, request.requestTarget()));
    };
    final ServerContext serverCtx = startNewLocalServer(httpService, afterClassCloseables);
    final HostAndPort serverHostAndPort = serverHostAndPort(serverCtx);
    serverHost = serverHostAndPort.hostName();
    serverPort = serverHostAndPort.port();
    hostHeader = hostHeader(serverHostAndPort);
}
Also used : LOCATION(io.servicetalk.http.api.HttpHeaderNames.LOCATION) ServiceDiscovererEvent(io.servicetalk.client.api.ServiceDiscovererEvent) BAD_REQUEST(io.servicetalk.http.api.HttpResponseStatus.BAD_REQUEST) AfterAll(org.junit.jupiter.api.AfterAll) ZERO(io.servicetalk.http.api.HttpHeaderValues.ZERO) CREATED(io.servicetalk.http.api.HttpResponseStatus.CREATED) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) UNAUTHORIZED(io.servicetalk.http.api.HttpResponseStatus.UNAUTHORIZED) AddressUtils.hostHeader(io.servicetalk.transport.netty.internal.AddressUtils.hostHeader) BlockingTestUtils.awaitIndefinitelyNonNull(io.servicetalk.concurrent.api.BlockingTestUtils.awaitIndefinitelyNonNull) SEE_OTHER(io.servicetalk.http.api.HttpResponseStatus.SEE_OTHER) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Collection(java.util.Collection) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) ServiceDiscoverer(io.servicetalk.client.api.ServiceDiscoverer) CONNECT(io.servicetalk.http.api.HttpRequestMethod.CONNECT) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) InetSocketAddress(java.net.InetSocketAddress) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) GlobalDnsServiceDiscoverer.globalDnsServiceDiscoverer(io.servicetalk.http.netty.GlobalDnsServiceDiscoverer.globalDnsServiceDiscoverer) Matchers.is(org.hamcrest.Matchers.is) NOT_IMPLEMENTED(io.servicetalk.http.api.HttpResponseStatus.NOT_IMPLEMENTED) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Publisher(io.servicetalk.concurrent.api.Publisher) BAD_GATEWAY(io.servicetalk.http.api.HttpResponseStatus.BAD_GATEWAY) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HOST(io.servicetalk.http.api.HttpHeaderNames.HOST) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) OPTIONS(io.servicetalk.http.api.HttpRequestMethod.OPTIONS) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) INTERNAL_SERVER_ERROR(io.servicetalk.http.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) MalformedURLException(java.net.MalformedURLException) Completable(io.servicetalk.concurrent.api.Completable) ACCEPTED(io.servicetalk.http.api.HttpResponseStatus.ACCEPTED) OK(io.servicetalk.http.api.HttpResponseStatus.OK) UnknownHostException(java.net.UnknownHostException) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) Integer.parseInt(java.lang.Integer.parseInt) ExecutionException(java.util.concurrent.ExecutionException) FORBIDDEN(io.servicetalk.http.api.HttpResponseStatus.FORBIDDEN) PERMANENT_REDIRECT(io.servicetalk.http.api.HttpResponseStatus.PERMANENT_REDIRECT) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) TestSingleSubscriber(io.servicetalk.concurrent.test.internal.TestSingleSubscriber) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) MOVED_PERMANENTLY(io.servicetalk.http.api.HttpResponseStatus.MOVED_PERMANENTLY) HostAndPort(io.servicetalk.transport.api.HostAndPort) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) ServerContext(io.servicetalk.transport.api.ServerContext) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) RedirectConfigBuilder(io.servicetalk.http.api.RedirectConfigBuilder) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 24 with HostAndPort

use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.

the class SecurityHandshakeObserverTest method withProxyTunnel.

@Test
void withProxyTunnel() throws Exception {
    try (ProxyTunnel proxyTunnel = new ProxyTunnel()) {
        HostAndPort proxyAddress = proxyTunnel.startProxy();
        verifyHandshakeObserved(HttpServers::forAddress, address -> forSingleAddress(address).proxyAddress(proxyAddress));
    }
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) Test(org.junit.jupiter.api.Test)

Example 25 with HostAndPort

use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.

the class HttpProvidersTest method testMultiAddressHttpClientBuilderProvider.

@Test
void testMultiAddressHttpClientBuilderProvider() throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).listenStreamingAndAwait(new TestServiceStreaming())) {
        HostAndPort serverAddress = serverHostAndPort(serverContext);
        TestSingleAddressHttpClientBuilderProvider.MODIFY_FOR_ADDRESS.set(serverAddress);
        try (BlockingHttpClient client = HttpClients.forMultiAddressUrl().buildBlocking()) {
            assertThat(TestMultiAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            HttpResponse response = client.request(client.get("http://" + serverAddress + SVC_ECHO));
            assertThat(response.status(), is(OK));
            assertThat(TestSingleAddressHttpClientBuilderProvider.BUILD_COUNTER.get(), is(1));
            assertThat(TestSingleAddressHttpClientBuilderProvider.CONNECTION_COUNTER.get(), is(1));
        }
    }
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpServerContext(io.servicetalk.http.api.HttpServerContext) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HostAndPort (io.servicetalk.transport.api.HostAndPort)31 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)25 InetSocketAddress (java.net.InetSocketAddress)22 ServerContext (io.servicetalk.transport.api.ServerContext)13 Test (org.junit.jupiter.api.Test)12 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)9 Single (io.servicetalk.concurrent.api.Single)8 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)8 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)8 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)8 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)7 Publisher (io.servicetalk.concurrent.api.Publisher)7 StreamingHttpClientFilterFactory (io.servicetalk.http.api.StreamingHttpClientFilterFactory)7 ExecutionException (java.util.concurrent.ExecutionException)7 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)6 BlockingQueue (java.util.concurrent.BlockingQueue)6 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)6 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)6 Greeter (io.grpc.examples.helloworld.Greeter)5 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)5