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();
}
}
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);
}
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);
}
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);
}
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));
}
Aggregations