Search in sources :

Example 66 with HttpResponse

use of com.hotels.styx.api.HttpResponse 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 67 with HttpResponse

use of com.hotels.styx.api.HttpResponse 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 68 with HttpResponse

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

the class ModifyContentByAggregationExamplePluginTest method modifiesContent.

@Test
public void modifiesContent() {
    // Set up
    Config config = new Config("MyExtraText");
    ModifyContentByAggregationExamplePlugin plugin = new ModifyContentByAggregationExamplePlugin(config);
    LiveHttpRequest request = get("/").build();
    HttpInterceptor.Chain chain = anyRequest -> Eventual.of(response().body("OriginalBody", UTF_8).build().stream());
    // Execution
    HttpResponse response = Mono.from(plugin.intercept(request, chain).flatMap(liveResponse -> liveResponse.aggregate(100))).block();
    // Assertion
    assertThat(response.bodyAs(UTF_8), is("OriginalBodyMyExtraText"));
}
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) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Config(com.hotels.styx.ModifyContentByAggregationExamplePlugin.Config) HttpResponse.response(com.hotels.styx.api.HttpResponse.response) 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) Config(com.hotels.styx.ModifyContentByAggregationExamplePlugin.Config) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptor(com.hotels.styx.api.HttpInterceptor) HttpResponse(com.hotels.styx.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 69 with HttpResponse

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

the class ReplaceLiveContentExamplePluginTest method replacesLiveContent.

@Test
public void replacesLiveContent() {
    // Set up
    ReplaceLiveContentExampleConfig config = new ReplaceLiveContentExampleConfig("myNewContent");
    ReplaceLiveContentExamplePlugin plugin = new ReplaceLiveContentExamplePlugin(config);
    LiveHttpRequest request = get("/").build();
    HttpInterceptor.Chain chain = request1 -> Eventual.of(LiveHttpResponse.response().build());
    // Execution
    HttpResponse response = Mono.from(plugin.intercept(request, chain).flatMap(liveResponse -> liveResponse.aggregate(100))).block();
    // Assertion
    assertThat(response.bodyAs(UTF_8), is("myNewContent"));
}
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) 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) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 70 with HttpResponse

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

the class ProtocolMetricsTest method recordsServerSideHttps.

@Test
public void recordsServerSideHttps() {
    styxServer = new StyxServer.Builder().addRoute("/", origin.port()).start();
    HttpResponse response = doHttpsGet("/");
    assertThat(response.status(), is(OK));
    assertThat(styxServer.metrics().meter("styx.server.http.requests").getCount(), is(0L));
    assertThat(styxServer.metrics().meter("styx.server.https.requests").getCount(), is(1L));
    assertThat(styxServer.metrics().meter("styx.server.http.responses.200").getCount(), is(0L));
    assertThat(styxServer.metrics().meter("styx.server.https.responses.200").getCount(), is(1L));
}
Also used : StyxServer(com.hotels.styx.testapi.StyxServer) HttpResponse(com.hotels.styx.api.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.hotels.styx.api.HttpResponse)107 Test (org.junit.jupiter.api.Test)99 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)30 HttpRequest (com.hotels.styx.api.HttpRequest)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Eventual (com.hotels.styx.api.Eventual)10 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)9 StyxHttpClient (com.hotels.styx.client.StyxHttpClient)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 TlsSettings (com.hotels.styx.api.extension.service.TlsSettings)7 StyxObjectRecord (com.hotels.styx.StyxObjectRecord)6 HttpHandler (com.hotels.styx.api.HttpHandler)6 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)6 OK (com.hotels.styx.api.HttpResponseStatus.OK)5 HttpClient (com.hotels.styx.client.HttpClient)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)4 Plugin (com.hotels.styx.api.plugins.spi.Plugin)4 PluginFactory (com.hotels.styx.api.plugins.spi.PluginFactory)4 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3