Search in sources :

Example 36 with HttpResponse

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

the class BaseHttpClientTest method testRequest_Response_Duration.

@Test
void testRequest_Response_Duration() {
    final String endpoint = ENDPOINT;
    stubDefaultRequest(endpoint);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).executeJson();
    final long durationNano = rsp.getRequestDuration();
    final long durationMillis = rsp.getRequestDurationInMillis();
    assertThat(durationNano).isGreaterThan(0);
    assertThat(durationMillis).isEqualTo(durationNano / 1000 / 1000);
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 37 with HttpResponse

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

the class BaseHttpClientTest method testPost.

@Test
void testPost() {
    final String endpoint = ENDPOINT;
    final int status = 201;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String body = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    stubPostRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().preparePost(endpoint).acceptJson().asXmlHttpRequest().setBody(jsonBody("{\"name\": \"Jane Doe\"}")).execute();
    assertRequest(endpoint, HttpMethod.POST);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEqualTo(body);
    assertThat(rsp.getContentType().getFirstValue()).isEqualTo(APPLICATION_JSON);
    assertThat(rsp.getContentType().getLastValue()).isEqualTo(APPLICATION_JSON);
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 38 with HttpResponse

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

the class BaseHttpClientTest method testResponseWithSeveralValues.

private void testResponseWithSeveralValues(String name, List<String> values, MapperFunction<HttpResponse, HttpHeader> func) {
    // GIVEN
    final String endpoint = ENDPOINT;
    final int status = 200;
    final String body = null;
    final Collection<Pair> headers = singleton(pair(name, values));
    stubGetRequest(endpoint, status, headers, body);
    // WHEN
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).addAcceptEncoding("identity").executeJson();
    // THEN
    final HttpHeader header = rsp.getHeader(name);
    assertThat(rsp.containsHeader(name)).isTrue();
    assertThat(func.apply(rsp)).isEqualTo(header);
    assertThat(header.getName()).isEqualTo(name);
    assertThat(header.getValues()).isEqualTo(values);
    assertThat(header.getFirstValue()).isEqualTo(values.get(0));
    assertThat(header.getLastValue()).isEqualTo(values.get(values.size() - 1));
    assertThat(rsp.getHeaders()).extracting("name", "values").contains(tuple(name, values));
}
Also used : HttpHeader(com.github.mjeanroy.junit.servers.client.HttpHeader) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair)

Example 39 with HttpResponse

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

the class BaseHttpClientTest method testPreparePutWithBody.

@Test
void testPreparePutWithBody() {
    final String endpoint = ENDPOINT + "/1";
    final int status = 204;
    final Collection<Pair> headers = emptyList();
    final String body = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    stubPutRequest(endpoint, status, headers);
    final HttpResponse rsp = createDefaultClient().preparePut(endpoint, jsonBody(body)).execute();
    assertRequest(endpoint, HttpMethod.PUT);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEmpty();
    assertThat(rsp.getContentType()).isNull();
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 40 with HttpResponse

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

the class BaseHttpClientTest method testPreparePatchWithBody.

@Test
void testPreparePatchWithBody() {
    final String endpoint = ENDPOINT + "/1";
    final int status = 204;
    final Collection<Pair> headers = emptyList();
    final String body = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    stubPatchRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().preparePatch(endpoint, jsonBody(body)).execute();
    assertRequest(endpoint, HttpMethod.PATCH);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEmpty();
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)50 Test (org.junit.jupiter.api.Test)41 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)25 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)23 JettyTest (com.github.mjeanroy.junit.servers.jetty.jupiter.JettyTest)14 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)7 HttpRequestBody (com.github.mjeanroy.junit.servers.client.HttpRequestBody)4 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)3 AbstractHttpRequest (com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest)3 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)2 Response (com.ning.http.client.Response)2 Response (okhttp3.Response)2 Response (org.asynchttpclient.Response)2 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 HttpParameter (com.github.mjeanroy.junit.servers.client.HttpParameter)1 WireMockTestUtils.assertRequestWithCookie (com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie)1 TomcatTest (com.github.mjeanroy.junit.servers.tomcat.jupiter.TomcatTest)1 AsyncHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.AsyncHttpResponseBuilder)1 NingHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.NingHttpResponseBuilder)1 OkHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.OkHttpResponseBuilder)1