Search in sources :

Example 1 with OngoingRequest

use of com.spotify.apollo.request.OngoingRequest in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldUseEndpointRunnableFactoryDecorator.

@Test
public void shouldUseEndpointRunnableFactoryDecorator() throws Exception {
    AtomicReference<String> lastResponseRef = new AtomicReference<>();
    final Service service = this.service.withModule(new LastResponseModule(lastResponseRef)).build();
    try (Service.Instance i = service.start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(env -> {
            env.routingEngine().registerAutoRoute(Route.sync("GET", "/", ctx -> "hello"));
        });
        assertNotNull(handler);
        final FakeOngoingRequest ongoingRequest = ongoingRequest("http://foo");
        handler.handle(ongoingRequest);
        assertThat(ongoingRequest.getReply(), hasStatus(Status.GONE));
        assertEquals("hello", lastResponseRef.get());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : Response(com.spotify.apollo.Response) Request(com.spotify.apollo.Request) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) OngoingRequest(com.spotify.apollo.request.OngoingRequest) RequestHandler(com.spotify.apollo.request.RequestHandler) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ByteString(okio.ByteString) Assert.fail(org.junit.Assert.fail) RequestMetadata(com.spotify.apollo.RequestMetadata) Service(com.spotify.apollo.core.Service) Environment(com.spotify.apollo.Environment) Status(com.spotify.apollo.Status) Before(org.junit.Before) Description(org.hamcrest.Description) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Route(com.spotify.apollo.route.Route) Instant(java.time.Instant) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Consumer(java.util.function.Consumer) MetaModule(com.spotify.apollo.meta.MetaModule) Services(com.spotify.apollo.core.Services) Matcher(org.hamcrest.Matcher) RequestMetadataImpl(com.spotify.apollo.request.RequestMetadataImpl) Optional(java.util.Optional) AppInit(com.spotify.apollo.AppInit) Assert.assertEquals(org.junit.Assert.assertEquals) RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(okio.ByteString) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with OngoingRequest

use of com.spotify.apollo.request.OngoingRequest in project apollo by spotify.

the class RequestLoggingDecoratorTest method collectLoggingEventsForRequest.

private List<LoggingEvent> collectLoggingEventsForRequest(OngoingRequest ongoingRequest) {
    RequestRunnableFactory decorated = decorator.apply(delegateFactory);
    decorated.create(ongoingRequest).run(EMPTY_CONTINUATION);
    return testLogger.getLoggingEvents().stream().filter(event -> event.getLevel() == Level.INFO).collect(Collectors.toList());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Response(com.spotify.apollo.Response) Endpoint(com.spotify.apollo.dispatch.Endpoint) Arrays(java.util.Arrays) Request(com.spotify.apollo.Request) Level(uk.org.lidalia.slf4jext.Level) TestLoggerFactory(uk.org.lidalia.slf4jtest.TestLoggerFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) OngoingRequest(com.spotify.apollo.request.OngoingRequest) Assert.assertThat(org.junit.Assert.assertThat) ByteString.encodeUtf8(okio.ByteString.encodeUtf8) TestLogger(uk.org.lidalia.slf4jtest.TestLogger) RuleMatch(com.spotify.apollo.route.RuleMatch) ByteString(okio.ByteString) BiConsumer(java.util.function.BiConsumer) RequestMetadata(com.spotify.apollo.RequestMetadata) LoggingEvent(uk.org.lidalia.slf4jtest.LoggingEvent) Before(org.junit.Before) Description(org.hamcrest.Description) Iterator(java.util.Iterator) Test(org.junit.Test) TestLoggerFactoryResetRule(uk.org.lidalia.slf4jtest.TestLoggerFactoryResetRule) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Collectors(java.util.stream.Collectors) List(java.util.List) Rule(org.junit.Rule) Matcher(org.hamcrest.Matcher) RequestRunnableFactory(com.spotify.apollo.request.RequestRunnableFactory) RequestRunnableFactory(com.spotify.apollo.request.RequestRunnableFactory)

Example 3 with OngoingRequest

use of com.spotify.apollo.request.OngoingRequest in project apollo by spotify.

the class AsyncContextOngoingRequestTest method shouldNotAllowOverridingDropHandling.

@Test
public void shouldNotAllowOverridingDropHandling() throws Exception {
    OngoingRequest ongoingRequest = new Subclassed(REQUEST, asyncContext, logger);
    ongoingRequest.drop();
    verify(logger).accept(ongoingRequest, Optional.of(DROPPED));
}
Also used : OngoingRequest(com.spotify.apollo.request.OngoingRequest) Test(org.junit.Test)

Example 4 with OngoingRequest

use of com.spotify.apollo.request.OngoingRequest in project apollo by spotify.

the class HttpServerModuleTest method testCanStartRegularModule.

@Test
public void testCanStartRegularModule() throws Exception {
    int port = 9083;
    try (Service.Instance instance = service().start(NO_ARGS, onPort(port))) {
        HttpServer server = HttpServerModule.server(instance);
        assertCanNotConnect(port);
        TestHandler testHandler = new TestHandler();
        server.start(testHandler);
        assertCanConnect(port);
        Request request = new Request.Builder().get().url(baseUrl(port) + "/hello/world").build();
        okhttp3.Response response = okHttpClient.newCall(request).execute();
        assertThat(response.code(), is(IM_A_TEAPOT.code()));
        assertThat(testHandler.requests.size(), is(1));
        OngoingRequest incomingRequest = testHandler.requests.get(0);
        assertThat(incomingRequest.request().uri(), is("/hello/world"));
        server.close();
        assertCanNotConnect(port);
    }
}
Also used : OngoingRequest(com.spotify.apollo.request.OngoingRequest) Request(okhttp3.Request) Service(com.spotify.apollo.core.Service) OngoingRequest(com.spotify.apollo.request.OngoingRequest) Test(org.junit.Test)

Example 5 with OngoingRequest

use of com.spotify.apollo.request.OngoingRequest in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldUseRequestRunnableFactoryDecorator.

@Test
public void shouldUseRequestRunnableFactoryDecorator() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    final Service service = this.service.withModule(new RequestInspectingModule("http://foo", counter)).build();
    try (Service.Instance i = service.start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(env -> {
        });
        assertNotNull(handler);
        final OngoingRequest ongoingRequest = ongoingRequest("http://foo");
        handler.handle(ongoingRequest);
        assertEquals(1, counter.get());
        handler.handle(ongoingRequest);
        assertEquals(2, counter.get());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : RequestHandler(com.spotify.apollo.request.RequestHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) OngoingRequest(com.spotify.apollo.request.OngoingRequest) Test(org.junit.Test)

Aggregations

OngoingRequest (com.spotify.apollo.request.OngoingRequest)6 Test (org.junit.Test)5 Service (com.spotify.apollo.core.Service)3 Before (org.junit.Before)3 Request (com.spotify.apollo.Request)2 RequestMetadata (com.spotify.apollo.RequestMetadata)2 Response (com.spotify.apollo.Response)2 RequestHandler (com.spotify.apollo.request.RequestHandler)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ByteString (okio.ByteString)2 Description (org.hamcrest.Description)2 Matcher (org.hamcrest.Matcher)2 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)2 Assert.assertThat (org.junit.Assert.assertThat)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 AppInit (com.spotify.apollo.AppInit)1 Environment (com.spotify.apollo.Environment)1 Status (com.spotify.apollo.Status)1