Search in sources :

Example 1 with Cookie

use of com.github.mjeanroy.junit.servers.client.Cookie in project junit-servers by mjeanroy.

the class BaseHttpClientTest method testResponse_with_cookies.

@Test
void testResponse_with_cookies() {
    final String name = "id";
    final String value = "foo";
    final String domain = "localhost";
    final String path = "/";
    final long maxAge = 3600;
    final String cookieValue = name + "=" + value + "; " + "Domain=" + domain + "; " + "Path=" + path + "; " + "Max-Age=" + maxAge + "; " + "Secure; " + "HttpOnly";
    final String endpoint = ENDPOINT;
    final int status = 200;
    final Collection<Pair> headers = singleton(pair(SET_COOKIE, cookieValue));
    final String body = null;
    stubGetRequest(endpoint, status, headers, body);
    final HttpResponse rsp = createDefaultClient().prepareGet(endpoint).executeJson();
    final List<Cookie> cookies = rsp.getCookies();
    assertThat(cookies).hasSize(1);
    final Cookie cookie = cookies.get(0);
    assertThat(cookie.getName()).isEqualTo(name);
    assertThat(cookie.getValue()).isEqualTo(value);
    assertThat(cookie.getDomain()).isEqualTo(domain);
    assertThat(cookie.getPath()).isEqualTo(path);
    assertThat(cookie.getMaxAge()).isEqualTo(maxAge);
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(rsp.getCookie(name)).isEqualTo(cookie);
    assertThat(rsp.getCookie("foobar")).isNull();
}
Also used : WireMockTestUtils.assertRequestWithCookie(com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie) Cookie(com.github.mjeanroy.junit.servers.client.Cookie) HttpResponse(com.github.mjeanroy.junit.servers.client.HttpResponse) Pair(com.github.mjeanroy.junit.servers.utils.commons.Pair) WireMockTest(com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest) Test(org.junit.jupiter.api.Test)

Example 2 with Cookie

use of com.github.mjeanroy.junit.servers.client.Cookie in project junit-servers by mjeanroy.

the class AbstractHttpResponseImplTest method it_get_given_cookie_and_return_null_without_any_cookie.

@Test
void it_get_given_cookie_and_return_null_without_any_cookie() {
    final V response = createHttpResponseWithHeaders();
    final Cookie cookie = response.getCookie("fake_cookie");
    assertThat(cookie).isNull();
}
Also used : Cookie(com.github.mjeanroy.junit.servers.client.Cookie) Test(org.junit.jupiter.api.Test)

Example 3 with Cookie

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

use of com.github.mjeanroy.junit.servers.client.Cookie in project junit-servers by mjeanroy.

the class AbstractHttpResponse method getCookie.

@Override
public Cookie getCookie(String name) {
    notBlank(name, "name");
    HttpHeader header = getHeader(SET_COOKIE);
    if (header == null) {
        return null;
    }
    // Check each cookie to find cookie by its name
    for (String value : header.getValues()) {
        Cookie cookie = read(value);
        if (cookie.getName().equals(name)) {
            return cookie;
        }
    }
    // No matching
    return null;
}
Also used : Cookie(com.github.mjeanroy.junit.servers.client.Cookie) HttpHeader(com.github.mjeanroy.junit.servers.client.HttpHeader)

Example 5 with Cookie

use of com.github.mjeanroy.junit.servers.client.Cookie in project junit-servers by mjeanroy.

the class AbstractHttpResponse method getCookies.

@Override
public List<Cookie> getCookies() {
    HttpHeader header = getHeader(SET_COOKIE);
    if (header == null) {
        return emptyList();
    }
    List<String> values = header.getValues();
    List<Cookie> cookies = new ArrayList<>(values.size());
    for (String value : values) {
        cookies.add(read(value));
    }
    return unmodifiableList(cookies);
}
Also used : Cookie(com.github.mjeanroy.junit.servers.client.Cookie) HttpHeader(com.github.mjeanroy.junit.servers.client.HttpHeader) ArrayList(java.util.ArrayList)

Aggregations

Cookie (com.github.mjeanroy.junit.servers.client.Cookie)7 Test (org.junit.jupiter.api.Test)4 HttpHeader (com.github.mjeanroy.junit.servers.client.HttpHeader)3 HttpRequest (com.github.mjeanroy.junit.servers.client.HttpRequest)1 HttpResponse (com.github.mjeanroy.junit.servers.client.HttpResponse)1 HttpUrl (com.github.mjeanroy.junit.servers.client.HttpUrl)1 WireMockTestUtils.assertRequestWithCookie (com.github.mjeanroy.junit.servers.client.it.WireMockTestUtils.assertRequestWithCookie)1 Pair (com.github.mjeanroy.junit.servers.utils.commons.Pair)1 WireMockTest (com.github.mjeanroy.junit.servers.utils.jupiter.WireMockTest)1 ArrayList (java.util.ArrayList)1