Search in sources :

Example 11 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin 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 Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method serverDoesNotStartUntilPluginsAreStarted.

@Test
public void serverDoesNotStartUntilPluginsAreStarted() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    StyxServer styxServer = styxServerWithPlugins(Map.of("slowlyStartingPlugin", new Plugin() {

        @Override
        public void styxStarting() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request1, Chain chain) {
            return Eventual.of(response(OK).build());
        }
    }));
    try {
        styxServer.startAsync();
        Thread.sleep(10);
        assertThat(styxServer.proxyHttpAddress(), nullValue());
        latch.countDown();
        eventually(() -> assertThat(styxServer.proxyHttpAddress().getPort(), is(greaterThan(0))));
    } finally {
        stopIfRunning(styxServer);
    }
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 13 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method allPluginsAreStartedEvenIfSomeFail.

@Test
public void allPluginsAreStartedEvenIfSomeFail() {
    StyxServer styxServer = null;
    try {
        Plugin pluginMock2 = mock(Plugin.class);
        Plugin pluginMock4 = mock(Plugin.class);
        styxServer = styxServerWithPlugins(Map.of("plug1", new NonStarterPlugin("plug1"), "plug2", pluginMock2, "plug3", new NonStarterPlugin("plug3"), "plug4", pluginMock4));
        Service service = styxServer.startAsync();
        eventually(() -> assertThat(service.state(), is(FAILED)));
        assertThat(pssLog.log(), hasItem(loggingEvent(ERROR, "Error starting plugin 'plug1'", RuntimeException.class, "Plugin start test error: plug1")));
        assertThat(pssLog.log(), hasItem(loggingEvent(ERROR, "Error starting plugin 'plug3'", RuntimeException.class, "Plugin start test error: plug3")));
        verify(pluginMock2).styxStarting();
        verify(pluginMock4).styxStarting();
    } finally {
        stopIfRunning(styxServer);
    }
}
Also used : StyxService(com.hotels.styx.api.extension.service.spi.StyxService) BackendService(com.hotels.styx.api.extension.service.BackendService) Service(com.google.common.util.concurrent.Service) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 14 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class StyxServerTest method stopsTheServerWhenPluginFailsToStart.

@Test
public void stopsTheServerWhenPluginFailsToStart() {
    StyxServer styxServer = null;
    try {
        styxServer = styxServerWithPlugins(Map.of("foo", new NonStarterPlugin("foo"), "mockplugin3", mock(Plugin.class)));
        Service service = styxServer.startAsync();
        eventually(() -> assertThat(service.state(), is(FAILED)));
        assertThat(pssLog.log(), hasItem(loggingEvent(ERROR, "Error starting plugin 'foo'", RuntimeException.class, "Plugin start test error: foo")));
        assertThat(styxServer.state(), is(FAILED));
    } finally {
        stopIfRunning(styxServer);
    }
}
Also used : StyxService(com.hotels.styx.api.extension.service.spi.StyxService) BackendService(com.hotels.styx.api.extension.service.BackendService) Service(com.google.common.util.concurrent.Service) NamedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Test(org.junit.jupiter.api.Test)

Example 15 with Plugin

use of com.hotels.styx.api.plugins.spi.Plugin in project styx by ExpediaGroup.

the class InstrumentedPluginTest method metricsAreNotRecordedWhenExceptionIsReturnedByChain.

@Test
public void metricsAreNotRecordedWhenExceptionIsReturnedByChain() {
    String pluginName = "passThrough";
    Chain chain = request -> error(new SomeException());
    InstrumentedPlugin plugin = instrumentedPlugin(pluginName, PASS_THROUGH);
    assertThatEventualHasErrorOnly(SomeException.class, plugin.intercept(someRequest, chain));
    assertThat(getExceptionCount(pluginName, SOME_EXCEPTION), is(0.0));
    assertThat(getErrorCount(pluginName), is(0.0));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) LiveHttpResponse.response(com.hotels.styx.api.LiveHttpResponse.response) Eventual.error(com.hotels.styx.api.Eventual.error) Matchers.not(org.hamcrest.Matchers.not) PluginException(com.hotels.styx.api.plugins.spi.PluginException) LiveHttpRequest.get(com.hotels.styx.api.LiveHttpRequest.get) NamedPlugin.namedPlugin(com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) PASS_THROUGH(com.hotels.styx.api.plugins.spi.Plugin.PASS_THROUGH) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Counter(io.micrometer.core.instrument.Counter) Eventual(com.hotels.styx.api.Eventual) INTERNAL_SERVER_ERROR(com.hotels.styx.api.HttpResponseStatus.INTERNAL_SERVER_ERROR) Plugin(com.hotels.styx.api.plugins.spi.Plugin) Mono(reactor.core.publisher.Mono) HttpResponseStatus(com.hotels.styx.api.HttpResponseStatus) Metrics.formattedExceptionName(com.hotels.styx.api.Metrics.formattedExceptionName) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) MeterRegistry(com.hotels.styx.api.MeterRegistry) Environment(com.hotels.styx.Environment) OK(com.hotels.styx.api.HttpResponseStatus.OK) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) BAD_GATEWAY(com.hotels.styx.api.HttpResponseStatus.BAD_GATEWAY) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Mockito.mock(org.mockito.Mockito.mock) Chain(com.hotels.styx.api.HttpInterceptor.Chain) Test(org.junit.jupiter.api.Test)

Aggregations

Plugin (com.hotels.styx.api.plugins.spi.Plugin)16 Test (org.junit.jupiter.api.Test)13 NamedPlugin.namedPlugin (com.hotels.styx.proxy.plugin.NamedPlugin.namedPlugin)10 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)9 Eventual (com.hotels.styx.api.Eventual)8 OK (com.hotels.styx.api.HttpResponseStatus.OK)7 Optional (java.util.Optional)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Matchers.is (org.hamcrest.Matchers.is)7 Matchers.not (org.hamcrest.Matchers.not)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.verify (org.mockito.Mockito.verify)7 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)6 Environment (com.hotels.styx.Environment)5 NamedPlugin (com.hotels.styx.proxy.plugin.NamedPlugin)5 Eventual.error (com.hotels.styx.api.Eventual.error)4 HttpHandler (com.hotels.styx.api.HttpHandler)4 Chain (com.hotels.styx.api.HttpInterceptor.Chain)4 HttpResponse (com.hotels.styx.api.HttpResponse)4