Search in sources :

Example 1 with HttpUrl

use of com.github.mjeanroy.junit.servers.client.HttpUrl 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 2 with HttpUrl

use of com.github.mjeanroy.junit.servers.client.HttpUrl 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)

Example 3 with HttpUrl

use of com.github.mjeanroy.junit.servers.client.HttpUrl 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 4 with HttpUrl

use of com.github.mjeanroy.junit.servers.client.HttpUrl 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)

Aggregations

HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)4 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)4 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)3 AbstractHttpRequest (com.github.mjeanroy.junit.servers.client.impl.AbstractHttpRequest)3 Cookie (com.github.mjeanroy.junit.servers.client.Cookie)1 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)1 HttpParameter (com.github.mjeanroy.junit.servers.client.HttpParameter)1 Request (com.ning.http.client.Request)1 RequestBuilder (com.ning.http.client.RequestBuilder)1 Response (com.ning.http.client.Response)1 Uri (com.ning.http.client.uri.Uri)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Request (org.asynchttpclient.Request)1 RequestBuilder (org.asynchttpclient.RequestBuilder)1 Response (org.asynchttpclient.Response)1 Uri (org.asynchttpclient.uri.Uri)1