use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testRequest_should_fail_to_set_body_on_get_request.
@Test
void testRequest_should_fail_to_set_body_on_get_request() {
final HttpRequest httpRequest = createDefaultClient().prepareGet(ENDPOINT);
final String body = "{\"id\": 1, \"firstName\": \"John\", \"lastName\": \"Doe\"}";
assertThatThrownBy(() -> httpRequest.setBody(requestBody(body))).isExactlyInstanceOf(UnsupportedOperationException.class).hasMessage("Http method GET does not support request body");
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testRequestHeader.
private void testRequestHeader(HttpClient client, String name, String value, Function<HttpRequest> func) {
// GIVEN
final String endpoint = ENDPOINT;
stubDefaultRequest(ENDPOINT);
// WHEN
final HttpRequest rq = client.prepareGet(endpoint);
func.apply(rq);
// THEN
final HttpResponse rsp = rq.execute();
assertThat(rsp).isNotNull();
assertThat(rsp.status()).isEqualTo(200);
assertRequestWithHeader(endpoint, HttpMethod.GET, name, value);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testRequestBody.
private void testRequestBody(String body, Function<HttpRequest> func) {
final String endpoint = ENDPOINT;
final int rspStatus = 204;
final Collection<Pair> headers = emptyList();
final String rspBody = "";
stubPostRequest(endpoint, rspStatus, headers, rspBody);
final HttpRequest rq = createDefaultClient().preparePost(ENDPOINT);
func.apply(rq);
final HttpResponse rsp = rq.execute();
assertThat(rsp).isNotNull();
assertThat(rsp.status()).isEqualTo(rspStatus);
assertThat(rsp.body()).isEqualTo(rspBody);
assertRequestWithBody(endpoint, HttpMethod.POST, body);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method testQueryParams.
private void testQueryParams(String expectedUrl, Function<HttpRequest> func) {
// GIVEN
final int rspStatus = 200;
final Collection<Pair> headers = singleton(pair(CONTENT_TYPE, APPLICATION_JSON));
final String rspBody = "[]";
stubGetRequest(expectedUrl, rspStatus, headers, rspBody);
// WHEN
final HttpRequest rq = createDefaultClient().prepareGet(ENDPOINT);
func.apply(rq);
// THEN
final HttpResponse rsp = rq.execute();
assertThat(rsp).isNotNull();
assertThat(rsp.status()).isEqualTo(rspStatus);
assertThat(rsp.body()).isEqualTo(rspBody);
assertRequest(expectedUrl, HttpMethod.GET);
}
use of com.github.mjeanroy.junit.servers.client.HttpRequest in project junit-servers by mjeanroy.
the class BaseHttpClientTest method it_should_create_request_and_do_not_prepend_server_url.
@Test
void it_should_create_request_and_do_not_prepend_server_url() {
final HttpClient client = createDefaultClient(server);
final String endpoint = "/foo";
final String absoluteUrl = server.getUrl() + endpoint;
final HttpMethod httpMethod = HttpMethod.POST;
final HttpRequest httpRequest = client.prepareRequest(httpMethod, absoluteUrl);
assertThat(httpRequest.getMethod()).isEqualTo(httpMethod);
assertThat(httpRequest.getEndpoint()).isNotNull();
assertThat(httpRequest.getEndpoint().getScheme()).isEqualTo(scheme);
assertThat(httpRequest.getEndpoint().getHost()).isEqualTo(host);
assertThat(httpRequest.getEndpoint().getPort()).isEqualTo(port);
assertThat(httpRequest.getEndpoint().getPath()).isEqualTo(path + endpoint);
assertThat(httpRequest.getEndpoint().toString()).isEqualTo(url(scheme, host, port, path + endpoint));
}
Aggregations