Search in sources :

Example 56 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagatorTest method shouldLeaveIntactQuotedCookieValues.

@Test
public void shouldLeaveIntactQuotedCookieValues() {
    DefaultHttpResponse nettyResponse = new DefaultHttpResponse(HTTP_1_1, OK);
    nettyResponse.headers().add("Set-Cookie", "SESSID=\"sessId\"; Domain=.foo.com; Path=/; HttpOnly");
    LiveHttpResponse styxResponse = toStyxResponse(nettyResponse).build();
    assertThat(styxResponse.header("Set-Cookie"), isValue("SESSID=\"sessId\"; Domain=.foo.com; Path=/; HttpOnly"));
    assertThat(styxResponse.cookie("SESSID"), equalTo(Optional.of(responseCookie("SESSID", "\"sessId\"").domain(".foo.com").path("/").httpOnly(true).build())));
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 57 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class NettyToStyxResponsePropagatorTest method shouldConvertNettyCookieHeaderToStyxCookies.

@Test
public void shouldConvertNettyCookieHeaderToStyxCookies() {
    DefaultHttpResponse nettyResponse = new DefaultHttpResponse(HTTP_1_1, OK);
    nettyResponse.headers().add("Set-Cookie", "SESSID=sessId; Domain=.foo.com; Path=/; HttpOnly");
    LiveHttpResponse styxResponse = toStyxResponse(nettyResponse).build();
    assertThat(styxResponse.header("Set-Cookie"), isValue("SESSID=sessId; Domain=.foo.com; Path=/; HttpOnly"));
    assertThat(styxResponse.cookie("SESSID"), equalTo(Optional.of(responseCookie("SESSID", "sessId").domain(".foo.com").path("/").httpOnly(true).build())));
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 58 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse 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)

Example 59 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class EarlyReturnExamplePluginTest method returnsEarlyWhenHeaderIsPresent.

@Test
public void returnsEarlyWhenHeaderIsPresent() {
    EarlyReturnExamplePlugin plugin = new EarlyReturnExamplePlugin();
    LiveHttpRequest request = LiveHttpRequest.get("/").header("X-Respond", "foo").build();
    HttpInterceptor.Chain chain = request1 -> Eventual.of(LiveHttpResponse.response().build());
    Eventual<LiveHttpResponse> eventualLive = plugin.intercept(request, chain);
    Eventual<HttpResponse> eventual = eventualLive.flatMap(response -> response.aggregate(100));
    HttpResponse response = Mono.from(eventual).block();
    assertThat(response.bodyAs(UTF_8), is("Responding from plugin"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 60 with LiveHttpResponse

use of com.hotels.styx.api.LiveHttpResponse in project styx by ExpediaGroup.

the class ModifyHeadersExamplePluginTest method addsExtraHeaders.

/**
 * This tests the behaviours added in the ModifyHeadersExamplePlugin.
 */
@Test
public void addsExtraHeaders() {
    // a simple way to mock the downstream system
    HttpInterceptor.Chain chain = request -> {
        assertThat(request.header("myRequestHeader").orElse(null), is("foo"));
        return Eventual.of(response(OK).build());
    };
    // an example request you expect your plugin to receive
    LiveHttpRequest request = get("/foo").build();
    // since this is a test, we want to wait for the response
    LiveHttpResponse response = Mono.from(plugin.intercept(request, chain)).block();
    assertThat(response.header("myResponseHeader").orElse(null), is("bar"));
}
Also used : HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Eventual(com.hotels.styx.api.Eventual) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) Mono(reactor.core.publisher.Mono) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)80 Test (org.junit.jupiter.api.Test)69 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)25 Support.requestContext (com.hotels.styx.support.Support.requestContext)21 Eventual (com.hotels.styx.api.Eventual)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)15 HttpHandler (com.hotels.styx.api.HttpHandler)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 Mono (reactor.core.publisher.Mono)14 ByteStream (com.hotels.styx.api.ByteStream)13 Context (com.hotels.styx.api.HttpInterceptor.Context)13 OK (com.hotels.styx.api.HttpResponseStatus.OK)13 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)13 TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)12 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Matchers.is (org.hamcrest.Matchers.is)12 Buffer (com.hotels.styx.api.Buffer)11 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)11 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)11