Search in sources :

Example 6 with HttpMethod

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

the class OkHttpRequest method handleBody.

/**
 * Add request body if appropriate.
 *
 * @param builder The native OkHttp request builder.
 * @see Request.Builder#method(String, RequestBody)
 * @see RequestBody
 */
private void handleBody(Request.Builder builder) throws IOException {
    log.debug("Adding request body");
    RequestBody okhttpRequestBody = hasBody() ? createBody() : null;
    log.debug("Created body: {}", okhttpRequestBody);
    // Force an empty body, as POST & PUT methods requires
    // a body element.
    HttpMethod method = getMethod();
    if (okhttpRequestBody == null && method.isBodyAllowed()) {
        log.debug("Request method {} requires a body, but no one found, adding empty request body)", method);
        okhttpRequestBody = createEmptyBody();
    }
    builder.method(method.getVerb(), okhttpRequestBody);
    if (body != null && body.getContentType() != null) {
        builder.header("Content-Type", body.getContentType());
    }
}
Also used : HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) RequestBody(okhttp3.RequestBody)

Example 7 with HttpMethod

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

the class BaseHttpClientTest method it_should_create_request.

@Test
void it_should_create_request() {
    final HttpClient client = createDefaultClient(server);
    final String endpoint = "/foo";
    final HttpMethod httpMethod = HttpMethod.POST;
    final HttpRequest httpRequest = client.prepareRequest(httpMethod, endpoint);
    assertThat(httpRequest.getMethod()).isEqualTo(httpMethod);
    assertThat(httpRequest.getEndpoint()).isNotNull();
    assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
    assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
    assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
    assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(path + endpoint);
    assertThat(httpRequest.getEndpoint().toString()).isEqualTo(url(scheme, host, port, path + endpoint));
}
Also used : HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) HttpClient(com.github.mjeanroy.junit.servers.client.HttpClient) HttpMethod(com.github.mjeanroy.junit.servers.client.HttpMethod) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMethod (com.github.mjeanroy.junit.servers.client.HttpMethod)7 HttpClient (com.github.mjeanroy.junit.servers.client.HttpClient)5 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)5 Test (org.junit.jupiter.api.Test)5 RequestBody (okhttp3.RequestBody)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1