Search in sources :

Example 21 with HttpClient

use of com.github.mjeanroy.junit.servers.client.HttpClient in project junit-servers by mjeanroy.

the class EmbeddedServerRunner method openClient.

private HttpClient openClient(HttpClientStrategy strategy, HttpClientConfiguration configuration) {
    log.debug("Opening HTTP client using strategy: {}", strategy);
    notNull(strategy, "strategy");
    notNull(configuration, "configuration");
    synchronized (clients) {
        HttpClientId id = new HttpClientId(strategy, configuration);
        if (!clients.containsKey(id) || clients.get(id).isDestroyed()) {
            HttpClient client = strategy.build(configuration, server);
            clients.put(id, client);
        }
        return clients.get(id);
    }
}
Also used : HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient)

Example 22 with HttpClient

use of com.github.mjeanroy.junit.servers.client.HttpClient in project junit-servers by mjeanroy.

the class HttpClientAnnotationHandler method after.

@Override
public void after(Object target, Field field) {
    log.debug("Clearing HTTP client to {} # {}", target, field);
    HttpClient httpClient = getter(target, field);
    httpClient.destroy();
    setter(target, field, null);
}
Also used : TestHttpClient(com.github.mjeanroy.junit.servers.annotations.TestHttpClient) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient)

Example 23 with HttpClient

use of com.github.mjeanroy.junit.servers.client.HttpClient in project junit-servers by mjeanroy.

the class EmbeddedServerRunnerTest method it_should_get_client_with_given_strategy_custom_configuration_and_return_new_one_with_different_configuration.

@Test
public void it_should_get_client_with_given_strategy_custom_configuration_and_return_new_one_with_different_configuration() {
    final EmbeddedServer<?> server = new EmbeddedServerMockBuilder().build();
    final EmbeddedServerRunner adapter = new EmbeddedServerRunner(server);
    final HttpClientStrategy strategy = HttpClientStrategy.OK_HTTP3;
    final HttpClientConfiguration configuration1 = new HttpClientConfiguration.Builder().disableFollowRedirect().build();
    final HttpClientConfiguration configuration2 = new HttpClientConfiguration.Builder().build();
    final HttpClient client1 = adapter.getClient(strategy, configuration1);
    final HttpClient client2 = adapter.getClient(strategy, configuration2);
    assertThat(client1).isNotSameAs(client2);
    assertThat(client1).isInstanceOf(OkHttpClient.class);
    assertThat(client2).isInstanceOf(OkHttpClient.class);
    assertThat(client1.getConfiguration()).isSameAs(configuration1);
    assertThat(client2.getConfiguration()).isSameAs(configuration2);
}
Also used : EmbeddedServerMockBuilder(com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder) FakeEmbeddedServerConfigurationBuilder(com.github.mjeanroy.junit.servers.utils.impl.FakeEmbeddedServerConfigurationBuilder) OkHttpClient(com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpClientStrategy(com.github.mjeanroy.junit.servers.client.HttpClientStrategy) EmbeddedServerMockBuilder(com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder) HttpClientConfiguration(com.github.mjeanroy.junit.servers.client.HttpClientConfiguration) Test(org.junit.jupiter.api.Test)

Example 24 with HttpClient

use of com.github.mjeanroy.junit.servers.client.HttpClient in project junit-servers by mjeanroy.

the class EmbeddedServerRunnerTest method it_should_get_client_and_returns_previous_one.

@Test
void it_should_get_client_and_returns_previous_one() {
    final EmbeddedServer<?> server = new EmbeddedServerMockBuilder().build();
    final EmbeddedServerRunner adapter = new EmbeddedServerRunner(server);
    final HttpClient client1 = adapter.getClient();
    final HttpClient client2 = adapter.getClient();
    assertThat(client1).isNotNull();
    assertThat(client2).isNotNull();
    assertThat(client1).isSameAs(client2);
}
Also used : OkHttpClient(com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) EmbeddedServerMockBuilder(com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder) Test(org.junit.jupiter.api.Test)

Example 25 with HttpClient

use of com.github.mjeanroy.junit.servers.client.HttpClient in project junit-servers by mjeanroy.

the class EmbeddedServerRunnerTest method it_should_stop_server_and_close_clients.

@Test
void it_should_stop_server_and_close_clients() {
    final EmbeddedServer<?> server = new EmbeddedServerMockBuilder().build();
    final EmbeddedServerRunner adapter = new EmbeddedServerRunner(server);
    final HttpClient client = adapter.getClient();
    assertThat(client).isNotNull();
    assertThat(client.isDestroyed()).isFalse();
    adapter.stop();
    assertThat(client).isNotNull();
    assertThat(client.isDestroyed()).isTrue();
}
Also used : OkHttpClient(com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) EmbeddedServerMockBuilder(com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)41 Test (org.junit.jupiter.api.Test)38 EmbeddedServerMockBuilder (com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder)23 OkHttpClient (com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient)12 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)9 TestHttpClient (com.github.mjeanroy.junit.servers.annotations.TestHttpClient)8 HttpClientConfiguration (com.github.mjeanroy.junit.servers.client.HttpClientConfiguration)7 HttpClientStrategy (com.github.mjeanroy.junit.servers.client.HttpClientStrategy)6 HttpMethod (com.github.mjeanroy.junit.servers.client.HttpMethod)5 BaseHttpClientTest (com.github.mjeanroy.junit.servers.client.impl.BaseHttpClientTest)4 FakeEmbeddedServerConfigurationBuilder (com.github.mjeanroy.junit.servers.utils.impl.FakeEmbeddedServerConfigurationBuilder)4 NingAsyncHttpClient (com.github.mjeanroy.junit.servers.client.impl.ning.NingAsyncHttpClient)3 HttpClientAnnotationHandler.newHttpClientAnnotationHandler (com.github.mjeanroy.junit.servers.engine.HttpClientAnnotationHandler.newHttpClientAnnotationHandler)3 Field (java.lang.reflect.Field)3 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)2 ParameterContext (org.junit.jupiter.api.extension.ParameterContext)2 ApacheHttpClient.defaultApacheHttpClient (com.github.mjeanroy.junit.servers.client.impl.apache.ApacheHttpClient.defaultApacheHttpClient)1 ApacheHttpClient.newApacheHttpClient (com.github.mjeanroy.junit.servers.client.impl.apache.ApacheHttpClient.newApacheHttpClient)1 AsyncHttpClient.defaultAsyncHttpClient (com.github.mjeanroy.junit.servers.client.impl.async.AsyncHttpClient.defaultAsyncHttpClient)1 AsyncHttpClient.newAsyncHttpClient (com.github.mjeanroy.junit.servers.client.impl.async.AsyncHttpClient.newAsyncHttpClient)1