Search in sources :

Example 6 with HttpResponse

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

the class BaseHttpClientTest method testPut.

@Test
void testPut() {
    final String endpoint = ENDPOINT + "/1";
    final int status = 204;
    final Collection<Pair> headers = emptyList();
    stubPutRequest(endpoint, status, headers);
    final HttpResponse rsp = createDefaultClient().preparePut(endpoint).acceptJson().asXmlHttpRequest().setBody(jsonBody("{\"id\": 1, \"name\": \"Jane Doe\"}")).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 7 with HttpResponse

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

the class BaseHttpClientTest method testResponse_without_cookies.

@Test
void testResponse_without_cookies() {
    final String endpoint = ENDPOINT;
    stubDefaultRequest(endpoint);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).executeJson();
    assertThat(rsp.getCookies()).isNotNull().isEmpty();
    assertThat(rsp.getCookie("id")).isNull();
}
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 8 with HttpResponse

use of com.github.mjeanroy.junit.servers.client.HttpResponse 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 9 with HttpResponse

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

the class BaseHttpClientTest method testGet.

@Test
void testGet() {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String body = "[{\"id\": 1, \"name\": \"John Doe\"}]";
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).acceptJson().asXmlHttpRequest().execute();
    assertRequest(endpoint, HttpMethod.GET);
    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 10 with HttpResponse

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

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