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