use of com.github.mjeanroy.junit.servers.client.HttpRequest 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.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testRequestUrl.
@Test
void testRequestUrl() {
final String endpoint = ENDPOINT;
final HttpRequest rq = createDefaultClient().prepareGet(endpoint).acceptJson().asXmlHttpRequest();
assertThat(rq.getEndpoint()).isNotNull();
assertThat(rq.getEndpoint().getScheme()).isEqualTo(scheme);
assertThat(rq.getEndpoint().getHost()).isEqualTo(host);
assertThat(rq.getEndpoint().getPort()).isEqualTo(port);
assertThat(rq.getEndpoint().getPath()).isEqualTo(endpoint);
assertThat(rq.getMethod()).isEqualTo(HttpMethod.GET);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testRequestExecuteAs.
private void testRequestExecuteAs(String contentType, MapperFunction<HttpRequest, HttpResponse> func) {
// GIVEN
final String endpoint = ENDPOINT;
final int rspStatus = 200;
final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
final String rspBody = "[]";
stubGetRequest(endpoint, rspStatus, headers, rspBody);
// WHEN
final HttpRequest rq = createDefaultClient().prepareGet(ENDPOINT);
final HttpResponse rsp = func.apply(rq);
// THEN
assertThat(rsp).isNotNull();
assertThat(rsp.status()).isEqualTo(rspStatus);
assertThat(rsp.body()).isEqualTo(rspBody);
assertRequestWithHeader(endpoint, HttpMethod.GET, CONTENT_TYPE, contentType);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method it_should_create_delete_request.
@Test
void it_should_create_delete_request() {
final HttpClient client = createDefaultClient(server);
final HttpRequest httpRequest = client.prepareDelete("/foo");
assertThat(httpRequest.getMethod()).isEqualTo(HttpMethod.DELETE);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method it_should_create_put_request.
@Test
void it_should_create_put_request() {
final HttpClient client = createDefaultClient(server);
final HttpRequest httpRequest = client.preparePut("/foo");
assertThat(httpRequest.getMethod()).isEqualTo(HttpMethod.PUT);
}
Aggregations