Search in sources :

Example 1 with HttpParameter

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

the class BaseHttpClientTest method testRequest_add_several_query_params.

@Test
void testRequest_add_several_query_params() {
    final String n1 = "firstName";
    final String v1 = "john";
    final HttpParameter p1 = param(n1, v1);
    final String n2 = "lastName";
    final String v2 = "doe";
    final HttpParameter p2 = param(n2, v2);
    final String expectedUrl = ENDPOINT + "?" + encodeQueryParam(n1, v1) + "&" + encodeQueryParam(n2, v2);
    testQueryParams(expectedUrl, rq -> rq.addQueryParams(p1, p2));
}
Also used : HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 2 with HttpParameter

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

the class AbstractHttpRequest method addQueryParams.

@Override
public HttpRequest addQueryParams(HttpParameter parameter, HttpParameter... parameters) {
    notNull(parameter, "parameter");
    // Add first parameter.
    queryParams.put(parameter.getName(), parameter);
    // Add other parameters if available
    if (parameters != null) {
        for (HttpParameter p : parameters) {
            notNull(p, "parameter");
            queryParams.put(p.getName(), p);
        }
    }
    return this;
}
Also used : HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter)

Example 3 with HttpParameter

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

the class OkHttpRequest method doExecute.

@Override
protected HttpResponse doExecute() throws Exception {
    final HttpUrl endpoint = getEndpoint();
    final okhttp3.HttpUrl.Builder httpUrlBuilder = new okhttp3.HttpUrl.Builder().scheme(endpoint.getScheme()).host(endpoint.getHost()).port(endpoint.getPort()).addPathSegments(endpoint.getPath().substring(1));
    // Append all query parameters.
    for (HttpParameter queryParam : queryParams.values()) {
        httpUrlBuilder.addEncodedQueryParameter(queryParam.getEncodedName(), queryParam.getEncodedValue());
    }
    final Request.Builder builder = new Request.Builder().url(httpUrlBuilder.build());
    handleBody(builder);
    handleCookies(builder);
    handleHeaders(builder);
    final Call call = client.newCall(builder.build());
    final long start = System.nanoTime();
    try (Response response = call.execute()) {
        final long duration = System.nanoTime() - start;
        return OkHttpResponseFactory.of(response, duration);
    }
}
Also used : Response(okhttp3.Response) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Call(okhttp3.Call) Request(okhttp3.Request) HttpRequest(com.github.mjeanroy.junit.servers.client.HttpRequest) AbstractHttpRequest(com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest) HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter) HttpUrl(com.github.mjeanroy.junit.servers.client.HttpUrl)

Example 4 with HttpParameter

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

the class ApacheHttpRequest method createRequestURI.

/**
 * Create request URI.
 * Each additional query parameters will be appended to final URI.
 *
 * @return Created URI.
 * @throws URISyntaxException If an error occurred while building URI.
 * @see URIBuilder
 */
private URI createRequestURI() throws URISyntaxException {
    URI uri = getEndpoint().toURI();
    URIBuilder builder = new URIBuilder(uri).setCharset(StandardCharsets.UTF_8);
    for (HttpParameter parameter : queryParams.values()) {
        builder.addParameter(parameter.getName(), parameter.getValue());
    }
    return builder.build();
}
Also used : URI(java.net.URI) HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 5 with HttpParameter

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

the class BaseHttpClientTest method testRequest_add_several_form_params.

@Test
void testRequest_add_several_form_params() {
    final String n1 = "firstName";
    final String v1 = "John";
    final HttpParameter p1 = param(n1, v1);
    final String n2 = "lastName";
    final String v2 = "Doe";
    final HttpParameter p2 = param(n2, v2);
    final HttpRequestBody body = formUrlEncodedBody(p1, p2);
    final String expectedBody = encodeFormParam(n1, v1) + "&" + encodeFormParam(n2, v2);
    testRequestBody(expectedBody, rq -> rq.setBody(body));
}
Also used : HttpRequestBody(com.github.mjeanroy.junit.servers.client.HttpRequestBody) HttpParameter(com.github.mjeanroy.junit.servers.client.HttpParameter) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Aggregations

HttpParameter (com.github.mjeanroy.junit.servers.client.HttpParameter)6 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)3 Test (org.junit.jupiter.api.Test)3 HttpRequestBody (com.github.mjeanroy.junit.servers.client.HttpRequestBody)2 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)1 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)1 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)1 AbstractHttpRequest (com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest)1 URI (java.net.URI)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1