Search in sources :

Example 26 with HttpResponse

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

the class IndexControllerTest method it_should_render_handlebars_template_using_model_and_mustache_view.

@Test
void it_should_render_handlebars_template_using_model_and_mustache_view(HttpClient client) {
    HttpResponse response = client.prepareGet("/jane").execute();
    assertThat(response.status()).isEqualTo(200);
    assertThat(response.body()).contains("Hello, my name is Jane Doe");
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Test(org.junit.jupiter.api.Test) JettyTest(com.github.mjeanroy.junit.servers.jetty.jupiter.JettyTest)

Example 27 with HttpResponse

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

the class BaseHttpClientTest method testPostWithJsonBodyString.

@Test
void testPostWithJsonBodyString() {
    final String endpoint = ENDPOINT;
    final int status = 201;
    final String rawBody = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    final HttpRequestBody body = jsonBody(rawBody);
    final Collection<Pair> expectedHeaders = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    stubPostRequest(endpoint, status, expectedHeaders, rawBody);
    final HttpResponse rsp = createDefaultClient().preparePost(endpoint).acceptJson().asXmlHttpRequest().setBody(body).execute();
    assertRequest(endpoint, HttpMethod.POST);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEqualTo(rawBody);
    assertThat(rsp.getContentType().getFirstValue()).isEqualTo(APPLICATION_JSON);
    assertThat(rsp.getContentType().getLastValue()).isEqualTo(APPLICATION_JSON);
}
Also used : HttpRequestBody(com.github.mjeanroy.junit.servers.client.HttpRequestBody) 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 28 with HttpResponse

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

the class AsyncHttpResponseFactoryTest method it_should_create_http_response.

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

Example 29 with HttpResponse

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

the class BaseHttpClientTest method testRequestExecuteAs.

private void testRequestExecuteAs(String contentType, MapperFunction<HttpRequest, HttpResponse> func) {
    // GIVEN
    final String endpoint = ENDPOINT;
    final int rspStatus = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String rspBody = "[]";
    stubGetRequest(endpoint, rspStatus, headers, rspBody);
    // WHEN
    final HttpRequest rq = createDefaultClient().prepareGet(ENDPOINT);
    final HttpResponse rsp = func.apply(rq);
    // THEN
    assertThat(rsp).isNotNull();
    assertThat(rsp.status()).isEqualTo(rspStatus);
    assertThat(rsp.body()).isEqualTo(rspBody);
    assertRequestWithHeader(endpoint, HttpMethod.GET, CONTENT_TYPE, contentType);
}
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 30 with HttpResponse

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

the class BaseHttpClientTest method testPostWithoutBodyElement.

@Test
void testPostWithoutBodyElement() {
    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().asJson().asXmlHttpRequest().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)

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