use of okhttp3.Cookie in project keywhiz by square.
the class SessionLogoutResourceIntegrationTest method sendsExpiredCookie.
@Test
public void sendsExpiredCookie() throws Exception {
Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), "")).url(testUrl("/admin/logout")).build();
Response response = client.newCall(request).execute();
assertThat(response.code()).isEqualTo(303);
List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
assertThat(cookies).hasSize(1);
NewCookie cookie = NewCookie.valueOf(cookies.get(0));
assertThat(cookie.getName()).isEqualTo("session");
assertThat(cookie.getValue()).isEqualTo("expired");
assertThat(cookie.getVersion()).isEqualTo(1);
assertThat(cookie.getPath()).isEqualTo("/admin");
assertThat(cookie.isSecure()).isTrue();
assertThat(cookie.isHttpOnly()).isTrue();
assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}
use of okhttp3.Cookie in project okhttp by square.
the class HttpOverHttp2Test method receiveResponseCookies.
@Test
public void receiveResponseCookies() throws Exception {
RecordingCookieJar cookieJar = new RecordingCookieJar();
client = client.newBuilder().cookieJar(cookieJar).build();
server.enqueue(new MockResponse().addHeader("set-cookie: a=b"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
cookieJar.assertResponseCookies("a=b; path=/");
}
use of okhttp3.Cookie in project okhttp by square.
the class HttpOverHttp2Test method sendRequestCookies.
@Test
public void sendRequestCookies() throws Exception {
RecordingCookieJar cookieJar = new RecordingCookieJar();
Cookie requestCookie = new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build();
cookieJar.enqueueRequestCookies(requestCookie);
client = client.newBuilder().cookieJar(cookieJar).build();
server.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("a=b", request.getHeader("Cookie"));
}
use of okhttp3.Cookie in project okhttp by square.
the class URLConnectionTest method disconnectDuringConnect_cookieJar.
@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
class DisconnectingCookieJar implements CookieJar {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
connectionHolder.get().disconnect();
return Collections.emptyList();
}
}
OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
URL url = server.url("path that should never be accessed").url();
HttpURLConnection connection = new OkHttpURLConnection(url, client);
connectionHolder.set(connection);
try {
connection.getInputStream();
fail("Connection should not be established");
} catch (IOException expected) {
assertEquals("Canceled", expected.getMessage());
} finally {
connection.disconnect();
}
}
use of okhttp3.Cookie in project okhttp by square.
the class CallTest method responseCookies.
@Test
public void responseCookies() throws Exception {
server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=b; Expires=Thu, 01 Jan 1970 00:00:00 GMT").addHeader("Set-Cookie", "c=d; Expires=Fri, 02 Jan 1970 23:59:59 GMT; path=/bar; secure"));
RecordingCookieJar cookieJar = new RecordingCookieJar();
client = client.newBuilder().cookieJar(cookieJar).build();
executeSynchronously("/").assertCode(200);
List<Cookie> responseCookies = cookieJar.takeResponseCookies();
assertEquals(2, responseCookies.size());
assertEquals("a=b; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/", responseCookies.get(0).toString());
assertEquals("c=d; expires=Fri, 02 Jan 1970 23:59:59 GMT; path=/bar; secure", responseCookies.get(1).toString());
}
Aggregations