Search in sources :

Example 11 with HttpRequest

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

the class AbstractHttpClient method prepareRequest.

@Override
public HttpRequest prepareRequest(HttpMethod httpMethod, String endpoint) {
    log.debug("Preparing HTTP request: {} -- {}", httpMethod, endpoint);
    notNull(endpoint, "endpoint");
    if (isDestroyed()) {
        log.error("Attempt to create HTTP request but HTTP client has already been destroyed");
        throw new IllegalStateException("Cannot create request from a destroyed client");
    }
    final HttpUrl requestEndpoint;
    if (startsWithHttpScheme(endpoint)) {
        requestEndpoint = HttpUrl.parse(endpoint);
    } else {
        String serverPath = server.getPath();
        requestEndpoint = new HttpUrl.Builder().withScheme(server.getScheme()).withHost(server.getHost()).withPort(server.getPort()).withPath(concatenatePath(serverPath, removePrefix(endpoint, serverPath))).build();
    }
    HttpRequest rq = buildRequest(httpMethod, requestEndpoint);
    // Add default headers.
    log.debug("Adding default headers");
    for (HttpHeader header : configuration.getDefaultHeaders().values()) {
        log.trace("Adding default header: {}", header);
        rq = rq.addHeader(header);
    }
    // Add default cookies.
    log.debug("Adding default cookies");
    for (Cookie cookie : configuration.getDefaultCookies()) {
        log.trace("Adding default cookie: {}", cookie);
        rq = rq.addCookie(cookie);
    }
    return rq;
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) Cookie(com.github.mjeanroy.junit.servers.client.Cookie) HttpHeader(com.github.mjeanroy.junit.servers.client.HttpHeader) HttpUrl(com.github.mjeanroy.junit.servers.client.HttpUrl)

Example 12 with HttpRequest

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

the class BaseHttpClientTest method testRequestUrl.

@Test
void testRequestUrl() {
    final String endpoint = ENDPOINT;
    final HttpRequest rq = createDefaultClient().prepareGet(endpoint).acceptJson().asXmlHttpRequest();
    assertThat(rq.getEndpoint()).isNotNull();
    assertThat(rq.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(rq.getEndpoint().getHost()).isEqualTo(host);
    assertThat(rq.getEndpoint().getPort()).isEqualTo(port);
    assertThat(rq.getEndpoint().getPath()).isEqualTo(endpoint);
    assertThat(rq.getMethod()).isEqualTo(HttpMethod.GET);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 13 with HttpRequest

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

the class BaseHttpClientTest method testRequestExecuteAs.

private void testRequestExecuteAs(String contentType, MapperFunction<HttpRequest, HttpResponse> func) {
    // GIVEN
    final String endpoint = ENDPOINT;
    final int rspStatus = 200;
    final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
    final String rspBody = "[]";
    stubGetRequest(endpoint, rspStatus, headers, rspBody);
    // WHEN
    final HttpRequest rq = createDefaultClient().prepareGet(ENDPOINT);
    final HttpResponse rsp = func.apply(rq);
    // THEN
    assertThat(rsp).isNotNull();
    assertThat(rsp.status()).isEqualTo(rspStatus);
    assertThat(rsp.body()).isEqualTo(rspBody);
    assertRequestWithHeader(endpoint, HttpMethod.GET, CONTENT_TYPE, contentType);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair)

Example 14 with HttpRequest

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

the class BaseHttpClientTest method it_should_create_delete_request.

@Test
void it_should_create_delete_request() {
    final HttpClient client = createDefaultClient(server);
    final HttpRequest httpRequest = client.prepareDelete("/foo");
    assertThat(httpRequest.getMethod()).isEqualTo(HttpMethod.DELETE);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) Test(org.junit.jupiter.api.Test)

Example 15 with HttpRequest

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

the class BaseHttpClientTest method it_should_create_put_request.

@Test
void it_should_create_put_request() {
    final HttpClient client = createDefaultClient(server);
    final HttpRequest httpRequest = client.preparePut("/foo");
    assertThat(httpRequest.getMethod()).isEqualTo(HttpMethod.PUT);
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)16 Test (org.junit.jupiter.api.Test)11 HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)9 HttpMethod (com.github.mjeanroy.junit.servers.client.HttpMethod)5 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)4 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)3 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)2 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)1 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)1