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