Search in sources :

Example 6 with HttpRequestBody

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

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

the class BaseHttpClientTest method testRequest_add_non_escaped_form_param_value.

@Test
void testRequest_add_non_escaped_form_param_value() {
    final String name = "name";
    final String value = "john doe";
    final String expectedBody = encodeFormParam(name, value);
    final Map<String, String> parameters = singletonMap(name, value);
    final HttpRequestBody body = formUrlEncodedBody(parameters);
    testRequestBody(expectedBody, rq -> rq.setBody(body));
}
Also used : HttpRequestBody(com.github.mjeanroy.junit.servers.client.HttpRequestBody) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 8 with HttpRequestBody

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

the class BaseHttpClientTest method testRequest_add_non_escaped_form_param_name.

@Test
void testRequest_add_non_escaped_form_param_name() {
    final String name = "first name";
    final String value = "john";
    final String expectedBody = encodeFormParam(name, value);
    final Map<String, String> parameters = singletonMap(name, value);
    final HttpRequestBody body = formUrlEncodedBody(parameters);
    testRequestBody(expectedBody, rq -> rq.setBody(body));
}
Also used : HttpRequestBody(com.github.mjeanroy.junit.servers.client.HttpRequestBody) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 9 with HttpRequestBody

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

the class BaseHttpClientTest method testRequest_add_several_form_params.

@Test
void testRequest_add_several_form_params() {
    final String n1 = "firstName";
    final String v1 = "John";
    final HttpParameter p1 = param(n1, v1);
    final String n2 = "lastName";
    final String v2 = "Doe";
    final HttpParameter p2 = param(n2, v2);
    final HttpRequestBody body = formUrlEncodedBody(p1, p2);
    final String expectedBody = encodeFormParam(n1, v1) + "&" + encodeFormParam(n2, v2);
    testRequestBody(expectedBody, rq -> rq.setBody(body));
}
Also used : HttpRequestBody(com.github.mjeanroy.junit.servers.client.HttpRequestBody) HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 10 with HttpRequestBody

use of com.github.mjeanroy.junit.servers.client.HttpRequestBody 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)

Aggregations

HttpRequestBody (com.github.mjeanroy.junit.servers.client.HttpRequestBody)12 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)12 Test (org.junit.jupiter.api.Test)12 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)4 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)4 HttpParameter (com.github.mjeanroy.junit.servers.client.HttpParameter)2