Search in sources :

Example 1 with NettyConnectionFactory

use of com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory in project styx by ExpediaGroup.

the class StyxHttpClientTest method sendsToDefaultHttpPort.

/*
     * StyxHttpClient.sendRequestInternal
     * - Uses default HTTP port 8080 when not specified in Host header
     */
@Test
public void sendsToDefaultHttpPort() {
    NettyConnectionFactory factory = mockConnectionFactory();
    ArgumentCaptor<Origin> originCaptor = ArgumentCaptor.forClass(Origin.class);
    StyxHttpClient.sendRequestInternal(factory, get("/").header(HOST, "localhost").build().stream(), new StyxHttpClient.Builder());
    verify(factory).createConnection(originCaptor.capture(), any(ConnectionSettings.class), nullable(SslContext.class));
    assertThat(originCaptor.getValue().port(), is(80));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Example 2 with NettyConnectionFactory

use of com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory in project styx by ExpediaGroup.

the class StyxHttpClientTest method mockConnectionFactory.

private static NettyConnectionFactory mockConnectionFactory() {
    NettyConnectionFactory factory = mock(NettyConnectionFactory.class);
    when(factory.createConnection(any(Origin.class), any(ConnectionSettings.class), nullable(SslContext.class))).thenReturn(Mono.just(mock(Connection.class)));
    return factory;
}
Also used : Origin(com.hotels.styx.api.extension.Origin) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with NettyConnectionFactory

use of com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory in project styx by ExpediaGroup.

the class StyxHttpClientTest method sendsToDefaultHttpsPort.

/*
     * StyxHttpClient.sendRequestInternal
     * - Uses default HTTPS port 443 when not specified in Host header
     */
@Test
public void sendsToDefaultHttpsPort() {
    NettyConnectionFactory factory = mockConnectionFactory();
    ArgumentCaptor<Origin> originCaptor = ArgumentCaptor.forClass(Origin.class);
    StyxHttpClient.sendRequestInternal(factory, get("/").header(HOST, "localhost").build().stream(), new StyxHttpClient.Builder().secure(true));
    verify(factory).createConnection(originCaptor.capture(), any(ConnectionSettings.class), any(SslContext.class));
    assertThat(originCaptor.getValue().port(), is(443));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.jupiter.api.Test)

Example 4 with NettyConnectionFactory

use of com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory in project styx by ExpediaGroup.

the class StyxHttpClient method sendRequestInternal.

// Visible for testing
static Mono<LiveHttpResponse> sendRequestInternal(NettyConnectionFactory connectionFactory, LiveHttpRequest request, Builder params) {
    LiveHttpRequest networkRequest = addUserAgent(params.userAgent(), request);
    Origin origin = originFromRequest(networkRequest, params.https());
    SslContext sslContext = getSslContext(params.https(), params.tlsSettings());
    return connectionFactory.createConnection(origin, new ConnectionSettings(params.connectTimeoutMillis()), sslContext).flatMap(connection -> Mono.from(connection.write(networkRequest).doOnComplete(connection::close).doOnError(e -> connection.close()).map(response -> response.newBuilder().body(it -> it.doOnEnd(x -> connection.close()).doOnCancel(() -> connection.close())).build())));
}
Also used : Origin(com.hotels.styx.api.extension.Origin) HttpResponse(com.hotels.styx.api.HttpResponse) SslContext(io.netty.handler.ssl.SslContext) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) Preconditions.checkArgument(com.hotels.styx.common.Preconditions.checkArgument) NettyExecutor(com.hotels.styx.NettyExecutor) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) HostAndPort(com.google.common.net.HostAndPort) HttpRequest(com.hotels.styx.api.HttpRequest) Url(com.hotels.styx.api.Url) TlsSettings(com.hotels.styx.api.extension.service.TlsSettings) TimeUnit(java.util.concurrent.TimeUnit) HttpConfig.newHttpConfigBuilder(com.hotels.styx.client.HttpConfig.newHttpConfigBuilder) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) SslContextFactory(com.hotels.styx.client.ssl.SslContextFactory) Origin(com.hotels.styx.api.extension.Origin) Objects.requireNonNull(java.util.Objects.requireNonNull) USER_AGENT(com.hotels.styx.api.HttpHeaderNames.USER_AGENT) Optional(java.util.Optional) HOST(com.hotels.styx.api.HttpHeaderNames.HOST) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) NettyConnectionFactory(com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory) Builder.httpRequestOperationFactoryBuilder(com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

Origin (com.hotels.styx.api.extension.Origin)4 NettyConnectionFactory (com.hotels.styx.client.netty.connectionpool.NettyConnectionFactory)4 SslContext (io.netty.handler.ssl.SslContext)4 Test (org.junit.jupiter.api.Test)2 HostAndPort (com.google.common.net.HostAndPort)1 NettyExecutor (com.hotels.styx.NettyExecutor)1 HOST (com.hotels.styx.api.HttpHeaderNames.HOST)1 USER_AGENT (com.hotels.styx.api.HttpHeaderNames.USER_AGENT)1 HttpRequest (com.hotels.styx.api.HttpRequest)1 HttpResponse (com.hotels.styx.api.HttpResponse)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)1 Url (com.hotels.styx.api.Url)1 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)1 TlsSettings (com.hotels.styx.api.extension.service.TlsSettings)1 HttpConfig.newHttpConfigBuilder (com.hotels.styx.client.HttpConfig.newHttpConfigBuilder)1 Builder.httpRequestOperationFactoryBuilder (com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder)1 SslContextFactory (com.hotels.styx.client.ssl.SslContextFactory)1 Preconditions.checkArgument (com.hotels.styx.common.Preconditions.checkArgument)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1