Search in sources :

Example 1 with HttpMethod

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

the class BaseHttpClientTest method it_should_create_request_and_do_not_prepend_server_url.

@Test
void it_should_create_request_and_do_not_prepend_server_url() {
    final HttpClient client = createDefaultClient(server);
    final String endpoint = "/foo";
    final String absoluteUrl = server.getUrl() + endpoint;
    final HttpMethod httpMethod = HttpMethod.POST;
    final HttpRequest httpRequest = client.prepareRequest(httpMethod, absoluteUrl);
    assertThat(httpRequest.getMethod()).isEqualTo(httpMethod);
    assertThat(httpRequest.getEndpoint()).isNotNull();
    assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
    assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
    assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(path + endpoint);
    assertThat(httpRequest.getEndpoint().toString()).isEqualTo(url(scheme, host, port, path + endpoint));
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 2 with HttpMethod

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

the class BaseHttpClientTest method it_should_create_request_with_root_path.

@Test
void it_should_create_request_with_root_path() {
    final String serverUrl = url(scheme, host, 8080, "");
    final String path = "/";
    when(server.getUrl()).thenReturn(serverUrl);
    when(server.getPath()).thenReturn(path);
    final HttpClient client = createDefaultClient(server);
    final HttpMethod httpMethod = HttpMethod.GET;
    final HttpRequest httpRequest = client.prepareRequest(httpMethod, path);
    assertThat(httpRequest.getEndpoint()).isNotNull();
    assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
    assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
    assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(path);
    assertThat(httpRequest.getEndpoint().toString()).isEqualTo(localUrl(port));
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 3 with HttpMethod

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

the class BaseHttpClientTest method it_should_create_request_and_set_path_separator.

@Test
void it_should_create_request_and_set_path_separator() {
    final String serverUrl = url(scheme, host, port, "");
    when(server.getUrl()).thenReturn(serverUrl);
    when(server.getPath()).thenReturn("/");
    final HttpClient client = createDefaultClient(server);
    final String path = "/foo";
    final HttpMethod httpMethod = HttpMethod.GET;
    final HttpRequest httpRequest = client.prepareRequest(httpMethod, path);
    assertThat(httpRequest.getEndpoint()).isNotNull();
    assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
    assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
    assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(path);
    assertThat(httpRequest.getEndpoint().toString()).isEqualTo(url(scheme, host, port, path));
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMethod

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

the class BaseHttpClientTest method it_should_create_request_and_do_not_prepend_server_path.

@Test
void it_should_create_request_and_do_not_prepend_server_path() {
    final HttpClient client = createDefaultClient(server);
    final String endpoint = "/foo";
    final String fullPath = server.getPath() + endpoint;
    final HttpMethod httpMethod = HttpMethod.POST;
    final HttpRequest httpRequest = client.prepareRequest(httpMethod, fullPath);
    assertThat(httpRequest.getMethod()).isEqualTo(httpMethod);
    assertThat(httpRequest.getEndpoint()).isNotNull();
    assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
    assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
    assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(fullPath);
    assertThat(httpRequest.getEndpoint().toString()).isEqualTo(url(scheme, host, port, fullPath));
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 5 with HttpMethod

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

the class ApacheHttpRequest method doExecute.

@Override
protected HttpResponse doExecute() throws Exception {
    final HttpMethod method = getMethod();
    final HttpRequestBase httpRequest = FACTORY.create(method);
    httpRequest.setURI(createRequestURI());
    handleBody(httpRequest);
    handleHeaders(httpRequest);
    handleCookies(httpRequest);
    final long start = nanoTime();
    final org.apache.http.HttpResponse httpResponse = client.execute(httpRequest);
    final long duration = nanoTime() - start;
    return ApacheHttpResponseFactory.of(httpResponse, duration);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod)

Aggregations

HttpMethod (com.github.mjeanroy.junit.servers.client.HttpMethod)7 HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)5 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)5 Test (org.junit.jupiter.api.Test)5 RequestBody (okhttp3.RequestBody)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1