Search in sources :

Example 1 with HttpRequest

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

the class BaseHttpClientTest method testRequest_should_fail_to_set_body_on_get_request.

@Test
void testRequest_should_fail_to_set_body_on_get_request() {
    final HttpRequest httpRequest = createDefaultClient().prepareGet(ENDPOINT);
    final String body = "{\"id\": 1, \"firstName\": \"John\", \"lastName\": \"Doe\"}";
    assertThatThrownBy(() -> httpRequest.setBody(requestBody(body))).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Http method GET does not support request body");
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 2 with HttpRequest

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

the class BaseHttpClientTest method testRequestHeader.

private void testRequestHeader(HttpClient client, String name, String value, Function<HttpRequest> func) {
    // GIVEN
    final String endpoint = ENDPOINT;
    stubDefaultRequest(ENDPOINT);
    // WHEN
    final HttpRequest rq = client.prepareGet(endpoint);
    func.apply(rq);
    // THEN
    final HttpResponse rsp = rq.execute();
    assertThat(rsp).isNotNull();
    assertThat(rsp.status()).isEqualTo(200);
    assertRequestWithHeader(endpoint, HttpMethod.GET, name, value);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse)

Example 3 with HttpRequest

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

the class BaseHttpClientTest method testRequestBody.

private void testRequestBody(String body, Function<HttpRequest> func) {
    final String endpoint = ENDPOINT;
    final int rspStatus = 204;
    final Collection<Pair> headers = emptyList();
    final String rspBody = "";
    stubPostRequest(endpoint, rspStatus, headers, rspBody);
    final HttpRequest rq = createDefaultClient().preparePost(ENDPOINT);
    func.apply(rq);
    final HttpResponse rsp = rq.execute();
    assertThat(rsp).isNotNull();
    assertThat(rsp.status()).isEqualTo(rspStatus);
    assertThat(rsp.body()).isEqualTo(rspBody);
    assertRequestWithBody(endpoint, HttpMethod.POST, body);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair)

Example 4 with HttpRequest

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

the class BaseHttpClientTest method testQueryParams.

private void testQueryParams(String expectedUrl, Function<HttpRequest> func) {
    // GIVEN
    final int rspStatus = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String rspBody = "[]";
    stubGetRequest(expectedUrl, rspStatus, headers, rspBody);
    // WHEN
    final HttpRequest rq = createDefaultClient().prepareGet(ENDPOINT);
    func.apply(rq);
    // THEN
    final HttpResponse rsp = rq.execute();
    assertThat(rsp).isNotNull();
    assertThat(rsp.status()).isEqualTo(rspStatus);
    assertThat(rsp.body()).isEqualTo(rspBody);
    assertRequest(expectedUrl, HttpMethod.GET);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair)

Example 5 with HttpRequest

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

Aggregations

HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)16 Test (org.junit.jupiter.api.Test)11 HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)9 HttpMethod (com.github.mjeanroy.junit.servers.client.HttpMethod)5 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)4 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)3 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)2 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)1 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)1