Search in sources :

Example 21 with Pair

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

the class BaseHttpClientTest method testResponse_without_headers.

@Test
void testResponse_without_headers() {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final String body = null;
    final Collection<Pair> headers = emptyList();
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).executeJson();
    final List<String> testedHeaders = asList(ETAG, LOCATION, LAST_MODIFIED, CONTENT_ENCODING, CACHE_CONTROL, CONTENT_TYPE, STRICT_TRANSPORT_SECURITY, X_CONTENT_SECURITY_POLICY, X_WEBKIT_CSP, CONTENT_SECURITY_POLICY, X_XSS_PROTECTION, X_CONTENT_TYPE_OPTIONS);
    for (String header : testedHeaders) {
        assertThat(rsp.containsHeader(header)).overridingErrorMessage("Header %s should be missing", header).isFalse();
        assertThat(rsp.getHeader(header)).overridingErrorMessage("Header %s should be null", header).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 22 with Pair

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

the class BaseHttpClientTest method testGetReadingBodyTwice.

@Test
void testGetReadingBodyTwice() {
    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();
    final String r1 = rsp.body();
    final String r2 = rsp.body();
    assertThat(r1).isEqualTo(r2);
}
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 23 with Pair

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

the class BaseHttpClientTest method testPatch.

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

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

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

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