Search in sources :

Example 11 with Pair

use of com.github.mjeanroy.junit.servers.utils.commons.Pair 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)

Example 12 with Pair

use of com.github.mjeanroy.junit.servers.utils.commons.Pair in project junit-servers by mjeanroy.

the class BaseHttpClientTest method testPatchWithRawBody.

@Test
void testPatchWithRawBody() {
    final String endpoint = ENDPOINT;
    final int status = 201;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String responseBody = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    final String rawBody = "{\"name\": \"Jane Doe\"}";
    final HttpRequestBody body = jsonBody(rawBody);
    stubPatchRequest(endpoint, status, headers, responseBody);
    final HttpResponse rsp = createDefaultClient().preparePatch(endpoint).acceptJson().asJson().asXmlHttpRequest().setBody(body).execute();
    assertRequest(endpoint, HttpMethod.PATCH);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEqualTo(responseBody);
    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 13 with Pair

use of com.github.mjeanroy.junit.servers.utils.commons.Pair in project junit-servers by mjeanroy.

the class BaseHttpClientTest method testGetWithNonEncodedPath.

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

use of com.github.mjeanroy.junit.servers.utils.commons.Pair in project junit-servers by mjeanroy.

the class BaseHttpClientTest method testResponseHeader.

private void testResponseHeader(String name, String value, MapperFunction<HttpResponse, HttpHeader> func) {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(name, value));
    final String body = null;
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).addAcceptEncoding("identity").executeJson();
    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(singletonList(value));
    assertThat(header.getFirstValue()).isEqualTo(value);
    assertThat(header.getLastValue()).isEqualTo(value);
    assertThat(rsp.getHeaders()).extracting("name", "values").contains(tuple(name, singletonList(value)));
}
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 15 with Pair

use of com.github.mjeanroy.junit.servers.utils.commons.Pair 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)

Aggregations

Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)27 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)25 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)20 Test (org.junit.jupiter.api.Test)20 HttpRequestBody (com.github.mjeanroy.junit.servers.client.HttpRequestBody)4 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)3 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)2 UrlPattern (com.github.tomakehurst.wiremock.matching.UrlPattern)2 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 WireMockTestUtils.assertRequestWithCookie (com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie)1 MappingBuilder (com.github.tomakehurst.wiremock.client.MappingBuilder)1 ResponseDefinitionBuilder (com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder)1 HttpHeader (com.github.tomakehurst.wiremock.http.HttpHeader)1 HttpHeaders (com.github.tomakehurst.wiremock.http.HttpHeaders)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1 RequestPatternBuilder (com.github.tomakehurst.wiremock.matching.RequestPatternBuilder)1