use of com.github.mjeanroy.junit.servers.client.HttpResponse in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testDelete.
@Test
void testDelete() {
final int status = 204;
final Collection<Pair> headers = emptyList();
final String endpoint = ENDPOINT + "/1";
stubDeleteRequest(endpoint, status, headers);
final HttpResponse rsp = createDefaultClient().prepareDelete(endpoint).acceptJson().asJson().asXmlHttpRequest().execute();
assertRequest(endpoint, HttpMethod.DELETE);
assertThat(rsp.status()).isEqualTo(status);
assertThat(rsp.body()).isEmpty();
assertThat(rsp.getContentType()).isNull();
}
use of com.github.mjeanroy.junit.servers.client.HttpResponse in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testUploadWithMultipartRequest.
@Test
void testUploadWithMultipartRequest() {
final String endpoint = ENDPOINT;
final int status = 201;
final File file = classpathFile("/img1.jpg");
stubUploadRequest(endpoint, status);
final HttpResponse rsp = createDefaultClient().preparePost(endpoint).acceptJson().asXmlHttpRequest().setBody(multipartBuilder().addFormDataPart(file, "image").build()).execute();
assertUploadRequest(endpoint, HttpMethod.POST, file);
assertThat(rsp.status()).isEqualTo(status);
}
use of com.github.mjeanroy.junit.servers.client.HttpResponse 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.client.HttpResponse 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.client.HttpResponse 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);
}
Aggregations