Search in sources :

Example 11 with OK

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

the class StyxServerTest method exposesAdminEndpoints.

@Test
public void exposesAdminEndpoints() {
    setUpStyxAndPluginWithAdminPages(Map.of("adminPage1", (request, ctx) -> Eventual.of(LiveHttpResponse.response().header("AdminPage1", "yes").build()), "adminPage2", (request, ctx) -> Eventual.of(LiveHttpResponse.response().header("AdminPage2", "yes").build())));
    HttpResponse response = doAdminRequest("/admin/plugins/plugin-with-admin-pages/adminPage1");
    assertThat(response.status(), is(OK));
    assertThat(response.header("AdminPage1"), isValue("yes"));
    response = doAdminRequest("/admin/plugins/plugin-with-admin-pages/adminPage2");
    assertThat(response.status(), is(OK));
    assertThat(response.header("AdminPage2"), isValue("yes"));
}
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) Test(org.junit.jupiter.api.Test)

Example 12 with OK

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

Example 13 with OK

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

the class StandardHttpPipelineTest method interceptorsCanPassInformationThroughContextBeforeRequest.

@Test
public void interceptorsCanPassInformationThroughContextBeforeRequest() {
    HttpInterceptor addsToContext = (request, chain) -> {
        chain.context().add("contextValue", "expected");
        return chain.proceed(request);
    };
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return chain.proceed(request);
    };
    StandardHttpPipeline pipeline = pipeline(addsToContext, takesFromContext);
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(foundInContext.get(), is("expected"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with OK

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

the class StandardHttpPipelineTest method contextValuesAddedBeforeRequestCanBeRetrievedAfterward.

@Test
public void contextValuesAddedBeforeRequestCanBeRetrievedAfterward() {
    HttpInterceptor addsToContext = (request, chain) -> {
        chain.context().add("contextValue", "expected");
        return chain.proceed(request);
    };
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> chain.proceed(request).map(response -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return response;
    });
    StandardHttpPipeline pipeline = pipeline(addsToContext, takesFromContext);
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(foundInContext.get(), is("expected"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with OK

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

the class StandardHttpPipelineTest method interceptorsCanPassInformationThroughContextAfterRequest.

@Test
public void interceptorsCanPassInformationThroughContextAfterRequest() {
    HttpInterceptor addsToContext = (request, chain) -> chain.proceed(request).map(response -> {
        chain.context().add("contextValue", "expected");
        return response;
    });
    AtomicReference<String> foundInContext = new AtomicReference<>();
    HttpInterceptor takesFromContext = (request, chain) -> chain.proceed(request).map(response -> {
        foundInContext.set(chain.context().get("contextValue", String.class));
        return response;
    });
    // add + take happens on the way back, so order must be reserved
    StandardHttpPipeline pipeline = pipeline(takesFromContext, addsToContext);
    LiveHttpResponse response = sendRequestTo(pipeline);
    assertThat(response.status(), is(OK));
    assertThat(foundInContext.get(), is("expected"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RequestTracker(com.hotels.styx.server.track.RequestTracker) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) Support.requestContext(com.hotels.styx.support.Support.requestContext) Arrays.asList(java.util.Arrays.asList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MethodSource(org.junit.jupiter.params.provider.MethodSource) Eventual(com.hotels.styx.api.Eventual) HttpHandler(com.hotels.styx.api.HttpHandler) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) InetSocketAddress(java.net.InetSocketAddress) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) Stream(java.util.stream.Stream) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Matchers.contains(org.hamcrest.Matchers.contains) OK(com.hotels.styx.api.HttpResponseStatus.OK) Matchers.is(org.hamcrest.Matchers.is) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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