use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method receiveAndSendMultipleCookies.
@Test
public void receiveAndSendMultipleCookies() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=android").addHeader("Set-Cookie", "b=banana"));
server.enqueue(new MockResponse());
server.start();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
get(urlWithIpAddress(server, "/"));
RecordedRequest request1 = server.takeRequest();
assertNull(request1.getHeader("Cookie"));
get(urlWithIpAddress(server, "/"));
RecordedRequest request2 = server.takeRequest();
assertEquals("a=android; b=banana", request2.getHeader("Cookie"));
}
use of java.net.CookieManager in project okhttp by square.
the class CallTest method redirectsDoNotIncludeTooManyCookies.
@Test
public void redirectsDoNotIncludeTooManyCookies() throws Exception {
server2.enqueue(new MockResponse().setBody("Page 2"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/")));
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(server.getHostName());
cookie.setPath("/");
String portList = Integer.toString(server.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
Response response = client.newCall(new Request.Builder().url(server.url("/page1")).build()).execute();
assertEquals("Page 2", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("c=cookie", request1.getHeader("Cookie"));
RecordedRequest request2 = server2.takeRequest();
assertNull(request2.getHeader("Cookie"));
}
use of java.net.CookieManager in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnections.
private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
// The pool will only reuse sockets if the SSL socket factories are the same.
SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
ConnectionPool connectionPool = new ConnectionPool();
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
connection = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection);
if (rebuildClient) {
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
}
connection = urlFactory.open(server.url("/").url());
assertContent("another response via HTTPS", connection);
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of java.net.CookieManager in project robovm by robovm.
the class CookiesTest method testCookieStoreUriDropsFragment.
public void testCookieStoreUriDropsFragment() throws URISyntaxException {
CookieStore cookieStore = new CookieManager().getCookieStore();
cookieStore.add(new URI("http://a.com/a/foo#fragment"), new HttpCookie("a", "android"));
assertEquals(Arrays.asList(new URI("http://a.com")), cookieStore.getURIs());
}
use of java.net.CookieManager in project robovm by robovm.
the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.
public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
try {
// RoboVM note: Modified to call redirectTarget.shutdown() after test finishes.
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.play();
MockWebServer redirectSource = new MockWebServer();
try {
// RoboVM note: Modified to call redirectSource.shutdown() after test finishes.
redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTarget.getUrl("/")));
redirectSource.play();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSource.getCookieDomain());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSource.getUrl("/").toURI(), cookie);
CookieHandler.setDefault(cookieManager);
get(redirectSource, "/");
RecordedRequest request = redirectSource.takeRequest();
assertContains(request.getHeaders(), "Cookie: $Version=\"1\"; " + "c=\"cookie\";$Path=\"/\";$Domain=\"" + redirectSource.getCookieDomain() + "\";$Port=\"" + portList + "\"");
for (String header : redirectTarget.takeRequest().getHeaders()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
} finally {
redirectSource.shutdown();
}
} finally {
redirectTarget.shutdown();
}
}
Aggregations