Search in sources :

Example 11 with HttpResponse

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

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

the class BaseHttpClientTest method testPutWithoutBody.

@Test
void testPutWithoutBody() {
    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().asJson().asXmlHttpRequest().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 13 with HttpResponse

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

the class NingAsyncHttpResponseFactoryTest method it_should_create_http_response.

@Test
void it_should_create_http_response() {
    final Response delegate = new NingHttpResponseBuilder().build();
    final long duration = 1000L;
    final HttpResponse response = NingAsyncHttpResponseFactory.of(delegate, duration);
    assertThat(response).isNotNull().isExactlyInstanceOf(NingAsyncHttpResponse.class);
    assertThat(response.getRequestDuration()).isEqualTo(duration);
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Response(com.ning.http.client.Response) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) NingHttpResponseBuilder(com.github.mjeanroy.junit.servers.utils.builders.NingHttpResponseBuilder) Test(org.junit.jupiter.api.Test)

Example 14 with HttpResponse

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

the class OkHttpResponseFactoryTest method it_should_create_http_response.

@Test
void it_should_create_http_response() {
    final Response delegate = new OkHttpResponseBuilder().build();
    final long duration = 1000L;
    final HttpResponse response = OkHttpResponseFactory.of(delegate, duration);
    assertThat(response).isNotNull().isExactlyInstanceOf(OkHttpResponse.class);
    assertThat(response.getRequestDuration()).isEqualTo(duration);
}
Also used : Response(okhttp3.Response) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) OkHttpResponseBuilder(com.github.mjeanroy.junit.servers.utils.builders.OkHttpResponseBuilder) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 15 with HttpResponse

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

the class BaseHttpClientTest method testGetWithFullEndpoint.

@Test
void testGetWithFullEndpoint() {
    final String endpoint = ENDPOINT;
    final String rqUrl = url(scheme, host, port, 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(rqUrl).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)

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