Search in sources :

Example 6 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class HttpClientModuleTest method testOkHttpClientIsConfigured.

/**
 * Ensure that the OkHttpClient instance provided by the module is configured as expected. This
 * duplicates tests in {@link com.spotify.apollo.http.client.OkHttpClientProviderTest} but
 * verifies that the provider is actually used.
 */
@Test
public void testOkHttpClientIsConfigured() throws Exception {
    final Config config = ConfigFactory.parseString("http.client.connectTimeout: 7982");
    final Service service = Services.usingName("test").withModule(HttpClientModule.create()).build();
    try (Service.Instance i = service.start(new String[] {}, config)) {
        final OkHttpClient underlying = i.resolve(OkHttpClient.class);
        assertThat(underlying.connectTimeoutMillis(), is(7982));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Config(com.typesafe.config.Config) Service(com.spotify.apollo.core.Service) Test(org.junit.Test)

Example 7 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class HttpServerModuleTest method testParsesQueryParameters.

@Test
public void testParsesQueryParameters() throws Exception {
    int port = 9084;
    try (Service.Instance instance = service().start(NO_ARGS, onPort(port))) {
        HttpServer server = HttpServerModule.server(instance);
        TestHandler testHandler = new TestHandler();
        server.start(testHandler);
        Request httpRequest = new Request.Builder().get().url(baseUrl(port) + "/query?a=foo&b=bar&b=baz").build();
        okhttp3.Response response = okHttpClient.newCall(httpRequest).execute();
        assertThat(response.code(), is(IM_A_TEAPOT.code()));
        assertThat(testHandler.requests.size(), is(1));
        final com.spotify.apollo.Request apolloRequest = testHandler.requests.get(0).request();
        assertThat(apolloRequest.uri(), is("/query?a=foo&b=bar&b=baz"));
        assertThat(apolloRequest.parameter("a"), is(Optional.of("foo")));
        assertThat(apolloRequest.parameter("b"), is(Optional.of("bar")));
        assertThat(apolloRequest.parameters().get("b"), is(asList("bar", "baz")));
        assertThat(apolloRequest.parameter("c"), is(Optional.empty()));
    }
}
Also used : OngoingRequest(com.spotify.apollo.request.OngoingRequest) Request(okhttp3.Request) Service(com.spotify.apollo.core.Service) Test(org.junit.Test)

Example 8 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class App method main.

public static void main(String... args) throws IOException, InterruptedException {
    Service service = Services.usingName("test").withModule(HttpServerModule.create()).withModule(HttpClientModule.create()).build();
    try (Service.Instance instance = service.start(args)) {
        final RequestHandler handler = new Handler();
        HttpServerModule.server(instance).start(handler);
        LOG.info("Started service '{}'", service.getServiceName());
        final String string = instance.getConfig().getString("config.key");
        LOG.info("Value of config.key: {}", string);
        instance.waitForShutdown();
        LOG.info("Stopping service '{}'", service.getServiceName());
    }
}
Also used : RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) RequestHandler(com.spotify.apollo.request.RequestHandler)

Example 9 with Service

use of com.spotify.apollo.core.Service 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 10 with Service

use of com.spotify.apollo.core.Service in project apollo by spotify.

the class HttpServerModuleTest method testParsesHeadersParameters.

@Test
public void testParsesHeadersParameters() throws Exception {
    int port = 9085;
    try (Service.Instance instance = service().start(NO_ARGS, onPort(port))) {
        HttpServer server = HttpServerModule.server(instance);
        TestHandler testHandler = new TestHandler();
        server.start(testHandler);
        Request httpRequest = new Request.Builder().get().url(baseUrl(port) + "/headers").addHeader("Foo", "bar").addHeader("Repeat", "once").addHeader("Repeat", "twice").build();
        okhttp3.Response response = okHttpClient.newCall(httpRequest).execute();
        assertThat(response.code(), is(IM_A_TEAPOT.code()));
        assertThat(testHandler.requests.size(), is(1));
        final com.spotify.apollo.Request apolloRequest = testHandler.requests.get(0).request();
        assertThat(apolloRequest.uri(), is("/headers"));
        assertThat(apolloRequest.header("Foo"), is(Optional.of("bar")));
        assertThat(apolloRequest.header("Repeat"), is(Optional.of("once,twice")));
        assertThat(apolloRequest.header("Baz"), is(Optional.empty()));
        System.out.println("apolloRequest.headers() = " + apolloRequest.headers());
    }
}
Also used : OngoingRequest(com.spotify.apollo.request.OngoingRequest) Request(okhttp3.Request) Service(com.spotify.apollo.core.Service) Test(org.junit.Test)

Aggregations

Service (com.spotify.apollo.core.Service)14 Test (org.junit.Test)9 RequestHandler (com.spotify.apollo.request.RequestHandler)6 OngoingRequest (com.spotify.apollo.request.OngoingRequest)5 IOException (java.io.IOException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Request (okhttp3.Request)3 HttpService (com.spotify.apollo.httpservice.HttpService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 AppInit (com.spotify.apollo.AppInit)1 Environment (com.spotify.apollo.Environment)1 Request (com.spotify.apollo.Request)1 RequestMetadata (com.spotify.apollo.RequestMetadata)1 Response (com.spotify.apollo.Response)1 Status (com.spotify.apollo.Status)1 Services (com.spotify.apollo.core.Services)1 ApolloConfig (com.spotify.apollo.environment.ApolloConfig)1 MetaDescriptor (com.spotify.apollo.meta.MetaDescriptor)1 MetaModule (com.spotify.apollo.meta.MetaModule)1