Search in sources :

Example 31 with HttpResponse

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();
}
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 32 with HttpResponse

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);
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) TestUtils.classpathFile(com.github.mjeanroy.junit.servers.utils.commons.TestUtils.classpathFile) File(java.io.File) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 33 with HttpResponse

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();
    }
}
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 34 with HttpResponse

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);
}
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 35 with HttpResponse

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

HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)50 Test (org.junit.jupiter.api.Test)41 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)25 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)23 JettyTest (com.github.mjeanroy.junit.servers.jetty.jupiter.JettyTest)14 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)7 HttpRequestBody (com.github.mjeanroy.junit.servers.client.HttpRequestBody)4 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)3 AbstractHttpRequest (com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest)3 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)2 Response (com.ning.http.client.Response)2 Response (okhttp3.Response)2 Response (org.asynchttpclient.Response)2 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 HttpParameter (com.github.mjeanroy.junit.servers.client.HttpParameter)1 WireMockTestUtils.assertRequestWithCookie (com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie)1 TomcatTest (com.github.mjeanroy.junit.servers.tomcat.jupiter.TomcatTest)1 AsyncHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.AsyncHttpResponseBuilder)1 NingHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.NingHttpResponseBuilder)1 OkHttpResponseBuilder (com.github.mjeanroy.junit.servers.utils.builders.OkHttpResponseBuilder)1