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

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
« no previous file with comments | « android_webview/browser/aw_cookie_access_policy.cc ('k') | content/browser/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..08c6e3ebf9f552a52cf316b4000c7b9352f83246 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,51 @@ public class CookieManagerTest extends AwTestBase {
}
}
+ private String thirdPartyCookieForWebSocket(boolean acceptCookie) throws Throwable {
+ TestWebServer webServer = TestWebServer.start();
+ try {
+ // Turn global allow on.
+ mCookieManager.setAcceptCookie(true);
+ assertTrue(mCookieManager.acceptCookie());
+
+ // Sets the per-WebView value.
+ mAwContents.getSettings().setAcceptThirdPartyCookies(acceptCookie);
+ assertEquals(acceptCookie, 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"));
+ return mCookieManager.getCookie(cookieUrl);
+ } finally {
+ webServer.shutdown();
+ }
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testThirdPartyCookieForWebSocketDisabledCase() throws Throwable {
+ assertNull(thirdPartyCookieForWebSocket(false));
+ }
+
+ @MediumTest
+ @Feature({"AndroidWebView", "Privacy"})
+ public void testThirdPartyCookieForWebSocketEnabledCase() throws Throwable {
+ assertEquals("test1=value1", thirdPartyCookieForWebSocket(true));
+ }
+
/**
* 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 +465,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 +493,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 {
« no previous file with comments | « android_webview/browser/aw_cookie_access_policy.cc ('k') | content/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698