Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java

Issue 2397393002: Provide child/frame IDs for WebSocket handshake request (Closed)
Patch Set: fix Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
index c96fdeb0ef5f752fd41f366c65c75187ce69cfd8..084061af134bda05e3118fdea5112544fe6771a7 100644
--- a/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/CookieManagerTest.java
@@ -17,6 +17,8 @@ import org.chromium.android_webview.AwSettings;
import org.chromium.android_webview.test.util.CookieUtils;
import org.chromium.android_webview.test.util.JSUtils;
import org.chromium.base.test.util.Feature;
+import org.chromium.content.browser.test.util.JavaScriptUtils;
+import org.chromium.content_public.browser.WebContents;
import org.chromium.net.test.util.TestWebServer;
import java.util.ArrayList;
@@ -401,6 +403,76 @@ public class CookieManagerTest extends AwTestBase {
}
}
+ @MediumTest
Torne 2016/10/14 09:59:24 Can you put the common code for these tests in a s
yhirano 2016/10/19 11:32:13 Done.
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testThirdPartyCookieForWebSocketDisabledCase() throws Throwable {
jbudorick 2016/10/18 17:27:30 If possible, new tests should use EmbeddedTestServ
yhirano 2016/10/19 14:45:00 We need to add a response header (Sec-WebSocket-Ac
jbudorick 2016/10/19 15:40:40 Ah. Yeah, we haven't implemented that yet. Given t
+ TestWebServer webServer = TestWebServer.start();
+ try {
+ // Turn global allow on.
+ mCookieManager.setAcceptCookie(true);
+ assertTrue(mCookieManager.acceptCookie());
+
+ // When third party cookies are disabled...
+ mAwContents.getSettings().setAcceptThirdPartyCookies(false);
+ assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies());
+
+ // |cookieUrl| is a third-party url that sets a cookie on response.
+ String cookieUrl = toThirdPartyUrl(
+ makeCookieWebSocketUrl(webServer, "/cookie_1", "test1", "value1"));
+ // This html file includes a script establishing a WebSocket connection to |cookieUrl|.
+ String url = makeWebSocketScriptUrl(webServer, "/content_1.html", cookieUrl);
+ loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
+ final String connecting = "0"; // WebSocket.CONNECTING
+ final String closed = "3"; // WebSocket.CLOSED
+ String readyState = connecting;
+ WebContents webContents = mAwContents.getWebContents();
+ while (!readyState.equals(closed)) {
+ readyState = JavaScriptUtils.executeJavaScriptAndWaitForResult(
+ webContents, "ws.readyState");
+ }
+ assertEquals("true",
+ JavaScriptUtils.executeJavaScriptAndWaitForResult(webContents, "hasOpened"));
+ assertNull(mCookieManager.getCookie(cookieUrl));
+ } finally {
+ webServer.shutdown();
+ }
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testThirdPartyCookieForWebSocketEnabledCase() throws Throwable {
+ TestWebServer webServer = TestWebServer.start();
+ try {
+ // Turn global allow on.
+ mCookieManager.setAcceptCookie(true);
+ assertTrue(mCookieManager.acceptCookie());
+
+ // When third party cookies are enabled...
+ mAwContents.getSettings().setAcceptThirdPartyCookies(true);
+ assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies());
+
+ // |cookieUrl| is a third-party url that sets a cookie on response.
+ String cookieUrl = toThirdPartyUrl(
+ makeCookieWebSocketUrl(webServer, "/cookie_1", "test1", "value1"));
+ // This html file includes a script establishing a WebSocket connection to |cookieUrl|.
+ String url = makeWebSocketScriptUrl(webServer, "/content_1.html", cookieUrl);
+ loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
+ WebContents webContents = mAwContents.getWebContents();
+ final String connecting = "0"; // WebSocket.CONNECTING
+ final String closed = "3"; // WebSocket.CLOSED
+ String readyState = connecting;
+ while (!readyState.equals(closed)) {
+ readyState = JavaScriptUtils.executeJavaScriptAndWaitForResult(
+ webContents, "ws.readyState");
+ }
+ assertEquals("true",
+ JavaScriptUtils.executeJavaScriptAndWaitForResult(webContents, "hasOpened"));
+ assertEquals("test1=value1", mCookieManager.getCookie(cookieUrl));
+ } finally {
+ webServer.shutdown();
+ }
+ }
+
/**
* Creates a response on the TestWebServer which attempts to set a cookie when fetched.
* @param webServer the webServer on which to create the response
@@ -418,6 +490,22 @@ public class CookieManagerTest extends AwTestBase {
}
/**
+ * Creates a response on the TestWebServer which attempts to set a cookie when establishing a
+ * WebSocket connection.
+ * @param webServer the webServer on which to create the response
+ * @param path the path component of the url (e.g "/cookie_test.html")
+ * @param key the key of the cookie
+ * @param value the value of the cookie
+ * @return the url which gets the response
+ */
+ private String makeCookieWebSocketUrl(
+ TestWebServer webServer, String path, String key, String value) {
+ List<Pair<String, String>> responseHeaders = new ArrayList<Pair<String, String>>();
+ responseHeaders.add(Pair.create("Set-Cookie", key + "=" + value + "; path=" + path));
+ return webServer.setResponseForWebSocket(path, responseHeaders);
+ }
+
+ /**
* Creates a response on the TestWebServer which contains a script tag with an external src.
* @param webServer the webServer on which to create the response
* @param path the path component of the url (e.g "/my_thing_with_script.html")
@@ -430,6 +518,24 @@ public class CookieManagerTest extends AwTestBase {
return webServer.setResponse(path, responseStr, null);
}
+ /**
+ * Creates a response on the TestWebServer which contains a script establishing a WebSocket
+ * connection.
+ * @param webServer the webServer on which to create the response
+ * @param path the path component of the url (e.g "/my_thing_with_script.html")
+ * @param url the url which which should appear as the src of the script tag.
+ * @return the url which gets the response
+ */
+ private String makeWebSocketScriptUrl(TestWebServer webServer, String path, String url) {
+ String responseStr = "<html><head><title>Content!</title></head>"
+ + "<body><script>\n"
+ + "let ws = new WebSocket('" + url.replaceAll("^http", "ws") + "');\n"
+ + "let hasOpened = false;\n"
+ + "ws.onopen = () => hasOpened = true;\n"
+ + "</script></body></html>";
+ return webServer.setResponse(path, responseStr, null);
+ }
+
@MediumTest
@Feature({"AndroidWebView", "Privacy"})
public void testThirdPartyJavascriptCookie() throws Throwable {

Powered by Google App Engine
This is Rietveld 408576698