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);
}
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);
}
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;
}
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);
}
}
Aggregations