Search in sources :

Example 1 with HttpClientStrategy

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

the class EmbeddedServerRunnerTest method it_should_get_client_of_given_strategy.

@Test
void it_should_get_client_of_given_strategy() {
    final EmbeddedServer<?> server = new EmbeddedServerMockBuilder().build();
    final EmbeddedServerRunner adapter = new EmbeddedServerRunner(server);
    final HttpClientStrategy strategy = HttpClientStrategy.OK_HTTP3;
    final HttpClient client = adapter.getClient(strategy);
    assertThat(client).isNotNull();
}
Also used : 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) Test(org.junit.jupiter.api.Test)

Example 2 with HttpClientStrategy

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

the class EmbeddedServerRunnerTest method it_should_get_client_with_given_strategy_custom_configuration_and_return_previous_one_with_same_configuration.

@Test
public void it_should_get_client_with_given_strategy_custom_configuration_and_return_previous_one_with_same_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().build();
    final HttpClientConfiguration configuration2 = new HttpClientConfiguration.Builder().build();
    final HttpClient client1 = adapter.getClient(strategy, configuration1);
    final HttpClient client2 = adapter.getClient(strategy, configuration2);
    assertThat(client1).isSameAs(client2);
}
Also used : 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 3 with HttpClientStrategy

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

the class HttpClientParameterResolverFunction method resolve.

@Override
public Object resolve(ParameterContext parameterContext, EmbeddedServerRunner serverAdapter) {
    log.debug("Resolving HTTP Client for parameter: {}", parameterContext);
    final Optional<TestHttpClient> maybeAnnotation = parameterContext.findAnnotation(TestHttpClient.class);
    final HttpClientConfiguration configuration;
    final HttpClientStrategy strategy;
    if (maybeAnnotation.isPresent()) {
        TestHttpClient annotation = maybeAnnotation.get();
        strategy = getStrategy(annotation);
        configuration = getConfiguration(annotation);
    } else {
        strategy = defaultStrategy();
        configuration = defaultConfiguration();
    }
    return serverAdapter.getClient(strategy, configuration);
}
Also used : HttpClientStrategy(com.github.mjeanroy.junit.servers.client.HttpClientStrategy) HttpClientConfiguration(com.github.mjeanroy.junit.servers.client.HttpClientConfiguration) TestHttpClient(com.github.mjeanroy.junit.servers.annotations.TestHttpClient)

Example 4 with HttpClientStrategy

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

the class HttpClientAnnotationHandler method before.

@Override
public void before(Object target, Field field) {
    log.debug("Inject HTTP client to {} # {}", target, field);
    TestHttpClient httpClient = Annotations.findAnnotation(field, TestHttpClient.class);
    if (httpClient != null) {
        HttpClientStrategy strategy = httpClient.strategy();
        setter(target, field, strategy.build(server));
    }
}
Also used : HttpClientStrategy(com.github.mjeanroy.junit.servers.client.HttpClientStrategy) TestHttpClient(com.github.mjeanroy.junit.servers.annotations.TestHttpClient)

Example 5 with HttpClientStrategy

use of com.github.mjeanroy.junit.servers.client.HttpClientStrategy 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)

Aggregations

HttpClientStrategy (com.github.mjeanroy.junit.servers.client.HttpClientStrategy)8 HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)6 OkHttpClient (com.github.mjeanroy.junit.servers.client.impl.okhttp3.OkHttpClient)6 EmbeddedServerMockBuilder (com.github.mjeanroy.junit.servers.utils.builders.EmbeddedServerMockBuilder)6 Test (org.junit.jupiter.api.Test)6 HttpClientConfiguration (com.github.mjeanroy.junit.servers.client.HttpClientConfiguration)4 TestHttpClient (com.github.mjeanroy.junit.servers.annotations.TestHttpClient)2 FakeEmbeddedServerConfigurationBuilder (com.github.mjeanroy.junit.servers.utils.impl.FakeEmbeddedServerConfigurationBuilder)2