Search in sources :

Example 1 with Pair

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

the class BaseHttpClientTest method testResponse_with_cookies.

@Test
void testResponse_with_cookies() {
    final String name = "id";
    final String value = "foo";
    final String domain = "localhost";
    final String path = "/";
    final long maxAge = 3600;
    final String cookieValue = name + "=" + value + "; " + "Domain=" + domain + "; " + "Path=" + path + "; " + "Max-Age=" + maxAge + "; " + "Secure; " + "HttpOnly";
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(SET_COOKIE, cookieValue));
    final String body = null;
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).executeJson();
    final List<Cookie> cookies = rsp.getCookies();
    assertThat(cookies).hasSize(1);
    final Cookie cookie = cookies.get(0);
    assertThat(cookie.getName()).isEqualTo(name);
    assertThat(cookie.getValue()).isEqualTo(value);
    assertThat(cookie.getDomain()).isEqualTo(domain);
    assertThat(cookie.getPath()).isEqualTo(path);
    assertThat(cookie.getMaxAge()).isEqualTo(maxAge);
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(rsp.getCookie(name)).isEqualTo(cookie);
    assertThat(rsp.getCookie("foobar")).isNull();
}
Also used : WireMockTestUtils.assertRequestWithCookie(com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie) Cookie(com.github.mjeanroy.junit.servers.client.Cookie) 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 2 with Pair

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

the class BaseHttpClientTest method testHead.

@Test
void testHead() {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    stubHeadRequest(endpoint, status, headers);
    final HttpResponse rsp = createDefaultClient().prepareHead(endpoint).acceptJson().asXmlHttpRequest().execute();
    assertRequest(endpoint, HttpMethod.HEAD);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isNullOrEmpty();
    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 3 with Pair

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

the class BaseHttpClientTest method testPreparePostWithBodyElement.

@Test
void testPreparePostWithBodyElement() {
    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, jsonBody(body)).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 4 with Pair

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

the class BaseHttpClientTest method testDeleteWithRequestBody.

@Test
void testDeleteWithRequestBody() {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String body = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    stubDeleteRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareDelete(endpoint, jsonBody(body)).acceptJson().execute();
    assertRequest(endpoint, HttpMethod.DELETE);
    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 5 with Pair

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

the class BaseHttpClientTest method testPutWithJsonBodyString.

@Test
void testPutWithJsonBodyString() {
    final String endpoint = ENDPOINT + "/1";
    final int status = 204;
    final Collection<Pair> headers = emptyList();
    final String rawBody = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    final HttpRequestBody body = jsonBody(rawBody);
    stubPutRequest(endpoint, status, headers);
    final HttpResponse rsp = createDefaultClient().preparePut(endpoint).acceptJson().asXmlHttpRequest().setBody(body).execute();
    assertRequest(endpoint, HttpMethod.PUT);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEmpty();
    assertThat(rsp.getContentType()).isNull();
}
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