Search in sources :

Example 1 with OK

use of com.hotels.styx.api.HttpResponseStatus.OK in project styx by ExpediaGroup.

the class HttpAggregatorTest method aggregatesRequestAndStreamsResponses.

@Test
public void aggregatesRequestAndStreamsResponses() {
    AtomicReference<String> result = new AtomicReference<>();
    WebServiceHandler webServiceHandler = (request, ctx) -> {
        result.set(request.bodyAs(UTF_8));
        return Eventual.of(response(OK).body("abcdef", UTF_8).build());
    };
    LiveHttpResponse response = Mono.from(new HttpAggregator(500, webServiceHandler).handle(LiveHttpRequest.post("/").body(ByteStream.from("ABCDEF", UTF_8)).build(), requestContext())).block();
    assertThat(result.get(), is("ABCDEF"));
    assertThat(Mono.from(response.aggregate(500)).block().bodyAs(UTF_8), is("abcdef"));
}
Also used : Eventual(com.hotels.styx.api.Eventual) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test) Support.requestContext(com.hotels.styx.support.Support.requestContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteStream(com.hotels.styx.api.ByteStream) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) WebServiceHandler(com.hotels.styx.api.WebServiceHandler) Test(org.junit.jupiter.api.Test)

Example 2 with OK

use of com.hotels.styx.api.HttpResponseStatus.OK in project styx by ExpediaGroup.

the class HttpStreamerTest method streamsRequestAndAggregatesResponses.

@Test
public void streamsRequestAndAggregatesResponses() {
    HttpHandler httpHandler = (request, ctx) -> Eventual.of(LiveHttpResponse.response(OK).body(ByteStream.from("abcdef", UTF_8)).build());
    HttpResponse response = Mono.from(new HttpStreamer(500, httpHandler).handle(HttpRequest.post("/").body("ABCDEF", UTF_8).build(), requestContext())).block();
    assertThat(response.bodyAs(UTF_8), is("abcdef"));
}
Also used : Eventual(com.hotels.styx.api.Eventual) HttpResponse(com.hotels.styx.api.HttpResponse) HttpHandler(com.hotels.styx.api.HttpHandler) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mono(reactor.core.publisher.Mono) HttpRequest(com.hotels.styx.api.HttpRequest) Test(org.junit.jupiter.api.Test) Support.requestContext(com.hotels.styx.support.Support.requestContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteStream(com.hotels.styx.api.ByteStream) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) HttpHandler(com.hotels.styx.api.HttpHandler) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with OK

use of com.hotels.styx.api.HttpResponseStatus.OK in project styx by ExpediaGroup.

the class HttpResponseWriterTest method releasesContentChunksWhenFailsToConvertToNettyHeaders.

@Test
public void releasesContentChunksWhenFailsToConvertToNettyHeaders() throws Exception {
    CaptureHttpResponseWriteEventsHandler writeEventsCollector = new CaptureHttpResponseWriteEventsHandler();
    Buffer chunk1 = new Buffer("aaa", UTF_8);
    Buffer chunk2 = new Buffer("aaa", UTF_8);
    AtomicBoolean unsubscribed = new AtomicBoolean(false);
    EmbeddedChannel ch = new EmbeddedChannel(new CaptureChannelArgumentsHandler(channelArgs), writeEventsCollector, new SimpleChannelInboundHandler<LiveHttpResponse>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
            HttpResponseWriter writer = new HttpResponseWriter(ctx, httpResponse -> {
                throw new RuntimeException();
            });
            CompletableFuture<Void> future = writer.write(response);
            contentObservable.onNext(chunk1);
            contentObservable.onNext(chunk2);
            contentObservable.onComplete();
            assertThat(future.isDone(), is(true));
            assertThat(toByteBuf(chunk1).refCnt(), is(0));
            assertThat(toByteBuf(chunk2).refCnt(), is(0));
            channelRead.set(true);
        }
    });
    LiveHttpResponse.Builder response = response(OK).cookies(responseCookie(",,,,", ",,,,").build());
    ch.writeInbound(response.body(new ByteStream(contentObservable.doOnCancel(() -> unsubscribed.set(true)))).build());
    assertThat(channelRead.get(), is(true));
}
Also used : Buffer(com.hotels.styx.api.Buffer) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) LoggingHandler(io.netty.handler.logging.LoggingHandler) Buffers.toByteBuf(com.hotels.styx.api.Buffers.toByteBuf) LoggingTestSupport(com.hotels.styx.support.matchers.LoggingTestSupport) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Disabled(org.junit.jupiter.api.Disabled) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LoggingEventMatcher.loggingEvent(com.hotels.styx.support.matchers.LoggingEventMatcher.loggingEvent) ChannelPromise(io.netty.channel.ChannelPromise) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) InetAddress.getLoopbackAddress(java.net.InetAddress.getLoopbackAddress) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) EMPTY_LAST_CONTENT(io.netty.handler.codec.http.LastHttpContent.EMPTY_LAST_CONTENT) InetSocketAddress(java.net.InetSocketAddress) Buffer(com.hotels.styx.api.Buffer) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Level(ch.qos.logback.classic.Level) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) ByteStream(com.hotels.styx.api.ByteStream) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) ResponseCookie.responseCookie(com.hotels.styx.api.ResponseCookie.responseCookie) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStream(com.hotels.styx.api.ByteStream) Test(org.junit.jupiter.api.Test)

Example 4 with OK

use of com.hotels.styx.api.HttpResponseStatus.OK in project styx by ExpediaGroup.

the class StyxServerTest method executesPluginsWhenProxying.

@Test
public void executesPluginsWhenProxying() {
    Plugin responseDecorator = (request, chain) -> chain.proceed(request).map(response -> response.newBuilder().header("plugin-executed", "yes").build());
    PluginFactory pluginFactory = environment -> responseDecorator;
    styxServer = new StyxServer.Builder().addRoute("/", originServer1.port()).addPluginFactory("response-decorator", pluginFactory, null).start();
    HttpResponse response = await(client.sendRequest(get(format("http://localhost:%d/foo", styxServer.proxyHttpPort())).build()));
    assertThat(response.status(), is(OK));
    assertThat(response.header("origin"), isValue("first"));
    assertThat(response.header("plugin-executed"), isValue("yes"));
    configureFor(originServer1.port());
    WireMock.verify(getRequestedFor(urlPathEqualTo("/foo")));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpResponse(com.hotels.styx.api.HttpResponse) StyxFutures.await(com.hotels.styx.common.StyxFutures.await) Matchers.not(org.hamcrest.Matchers.not) StyxHttpClient(com.hotels.styx.client.StyxHttpClient) HttpClient(com.hotels.styx.client.HttpClient) WireMock(com.github.tomakehurst.wiremock.client.WireMock) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) HttpRequest.get(com.hotels.styx.api.HttpRequest.get) Map(java.util.Map) Origins.origin(com.hotels.styx.testapi.Origins.origin) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) WireMock.configureFor(com.github.tomakehurst.wiremock.client.WireMock.configureFor) WireMock.urlPathEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo) Collections.emptyMap(java.util.Collections.emptyMap) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Plugin(com.hotels.styx.api.plugins.spi.Plugin) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Matchers.allOf(org.hamcrest.Matchers.allOf) Mockito.when(org.mockito.Mockito.when) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) TlsSettings(com.hotels.styx.api.extension.service.TlsSettings) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) IsOptional.isValue(com.hotels.styx.support.matchers.IsOptional.isValue) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.getRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) PluginFactory(com.hotels.styx.api.plugins.spi.PluginFactory) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 5 with OK

use of com.hotels.styx.api.HttpResponseStatus.OK in project styx by ExpediaGroup.

the class InterceptorPipelineBuilderTest method setUp.

@BeforeEach
public void setUp() {
    environment = new Environment.Builder().registry(new MicrometerRegistry(new SimpleMeterRegistry())).configuration(StyxConfig.defaultConfig()).build();
    plugins = List.of(namedPlugin("plug1", (request, chain) -> chain.proceed(request).map(response -> response.newBuilder().header("plug1", "1").build())), namedPlugin("plug2", (request, chain) -> chain.proceed(request).map(response -> response.newBuilder().header("plug2", "1").build())));
    handler = mock(RoutingObject.class);
    when(handler.handle(any(LiveHttpRequest.class), any(HttpInterceptor.Context.class))).thenReturn(Eventual.of(response(OK).build()));
}
Also used : RoutingObject(com.hotels.styx.routing.RoutingObject) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) StyxConfig(com.hotels.styx.StyxConfig) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) Support.requestContext(com.hotels.styx.support.Support.requestContext) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) List(java.util.List) IsOptional.isValue(com.hotels.styx.support.matchers.IsOptional.isValue) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) RoutingObject(com.hotels.styx.routing.RoutingObject) Support.requestContext(com.hotels.styx.support.Support.requestContext) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

OK (com.hotels.styx.api.HttpResponseStatus.OK)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 Test (org.junit.jupiter.api.Test)17 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)16 Eventual (com.hotels.styx.api.Eventual)15 Matchers.is (org.hamcrest.Matchers.is)15 Mono (reactor.core.publisher.Mono)12 HttpHandler (com.hotels.styx.api.HttpHandler)11 LiveHttpResponse.response (com.hotels.styx.api.LiveHttpResponse.response)11 LiveHttpRequest.get (com.hotels.styx.api.LiveHttpRequest.get)10 Support.requestContext (com.hotels.styx.support.Support.requestContext)9 List (java.util.List)9 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 InetSocketAddress (java.net.InetSocketAddress)7 ArrayList (java.util.ArrayList)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)7 HttpInterceptorContext (com.hotels.styx.server.HttpInterceptorContext)6 RequestTracker (com.hotels.styx.server.track.RequestTracker)6