Search in sources :

Example 16 with HttpResponse

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

the class BaseHttpClientTest method testPatchWithRawBody.

@Test
void testPatchWithRawBody() {
    final String endpoint = ENDPOINT;
    final int status = 201;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String responseBody = "{\"id\": 1, \"name\": \"Jane Doe\"}";
    final String rawBody = "{\"name\": \"Jane Doe\"}";
    final HttpRequestBody body = jsonBody(rawBody);
    stubPatchRequest(endpoint, status, headers, responseBody);
    final HttpResponse rsp = createDefaultClient().preparePatch(endpoint).acceptJson().asJson().asXmlHttpRequest().setBody(body).execute();
    assertRequest(endpoint, HttpMethod.PATCH);
    assertThat(rsp.status()).isEqualTo(status);
    assertThat(rsp.body()).isEqualTo(responseBody);
    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 17 with HttpResponse

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

the class BaseHttpClientTest method testGetWithNonEncodedPath.

@Test
void testGetWithNonEncodedPath() {
    final String endpoint = ENDPOINT + "/john doe";
    final String encodedPath = encodePath(endpoint);
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String body = "[{\"id\": 1, \"name\": \"John Doe\"}]";
    stubGetRequest(encodedPath, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).acceptJson().asXmlHttpRequest().execute();
    assertRequest(encodedPath, HttpMethod.GET);
    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 18 with HttpResponse

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

the class BaseHttpClientTest method testResponseHeader.

private void testResponseHeader(String name, String value, MapperFunction<HttpResponse, HttpHeader> func) {
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(name, value));
    final String body = null;
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).addAcceptEncoding("identity").executeJson();
    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(singletonList(value));
    assertThat(header.getFirstValue()).isEqualTo(value);
    assertThat(header.getLastValue()).isEqualTo(value);
    assertThat(rsp.getHeaders()).extracting("name", "values").contains(tuple(name, singletonList(value)));
}
Also used : HttpHeader(com.github.mjeanroy.junit.servers.client.HttpHeader) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair)

Example 19 with HttpResponse

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

the class AsyncHttpRequest method doExecute.

@Override
protected HttpResponse doExecute() throws Exception {
    final HttpUrl endpoint = getEndpoint();
    final String scheme = endpoint.getScheme();
    final String userInfo = null;
    final String host = endpoint.getHost();
    final int port = endpoint.getPort();
    final String path = Utf8UrlEncoder.encodePath(endpoint.getPath());
    final String query = null;
    final String fragment = null;
    final Uri uri = new Uri(scheme, userInfo, host, port, path, query, fragment);
    final String method = getMethod().getVerb();
    final RequestBuilder builder = new RequestBuilder(method, true).setUri(uri);
    handleQueryParameters(builder);
    handleBody(builder);
    handleHeaders(builder);
    handleCookies(builder);
    final Request request = builder.build();
    final ListenableFuture<Response> future = client.executeRequest(request);
    final long start = nanoTime();
    final Response response = future.get();
    final long duration = nanoTime() - start;
    return AsyncHttpResponseFactory.of(response, duration);
}
Also used : Response(org.asynchttpclient.Response) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) RequestBuilder(org.asynchttpclient.RequestBuilder) Request(org.asynchttpclient.Request) HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) AbstractHttpRequest(com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest) Uri(org.asynchttpclient.uri.Uri) HttpUrl(com.github.mjeanroy.junit.servers.client.HttpUrl)

Example 20 with HttpResponse

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

the class NingAsyncHttpRequest method doExecute.

@Override
protected HttpResponse doExecute() throws Exception {
    final HttpUrl endpoint = getEndpoint();
    final String scheme = endpoint.getScheme();
    final String userInfo = null;
    final String host = endpoint.getHost();
    final int port = endpoint.getPort();
    final String path = UTF8UrlEncoder.encodePath(endpoint.getPath());
    final String query = null;
    final Uri uri = new Uri(scheme, userInfo, host, port, path, query);
    final String method = getMethod().getVerb();
    final RequestBuilder builder = new RequestBuilder(method, true).setUri(uri);
    handleQueryParameters(builder);
    handleBody(builder);
    handleHeaders(builder);
    handleCookies(builder);
    final Request request = builder.build();
    final long start = nanoTime();
    final Response response = client.executeRequest(request).get();
    final long duration = nanoTime() - start;
    return NingAsyncHttpResponseFactory.of(response, duration);
}
Also used : HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Response(com.ning.http.client.Response) RequestBuilder(com.ning.http.client.RequestBuilder) Request(com.ning.http.client.Request) HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) AbstractHttpRequest(com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest) Uri(com.ning.http.client.uri.Uri) HttpUrl(com.github.mjeanroy.junit.servers.client.HttpUrl)

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