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

Unified Diff: chrome/test/data/extensions/api_test/webrequest/test_websocket.js

Issue 2449913002: Support WebSocket in WebRequest API. (Closed)
Patch Set: Refactor tests; add test; update documentation. Created 3 years, 10 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: chrome/test/data/extensions/api_test/webrequest/test_websocket.js
diff --git a/chrome/test/data/extensions/api_test/webrequest/test_websocket.js b/chrome/test/data/extensions/api_test/webrequest/test_websocket.js
new file mode 100644
index 0000000000000000000000000000000000000000..59a1a674568bb361bfec03aa6d6d4e00e0640dee
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/webrequest/test_websocket.js
@@ -0,0 +1,153 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+function getEchoTestURL(port) {
+ return "ws://localhost:" + port + "/echo-with-no-extension";
+}
+
+// Tries to: open a WebSocket, write a test message to it, close it. Verifies
+// that all the necessary events are triggered if |expectedToConnect|, otherwise
+// makes sure WebSocket terminates with an error.
+function testWebSocketConnection(url, expectedToConnect = true) {
+ var ws = new WebSocket(url);
+ var MESSAGE = "test message";
Devlin 2017/02/14 01:21:38 single quotes in js. Also, typically prefer kMess
pkalinnikov 2017/02/14 13:49:51 Done.
+
+ var connectionOpenExpected;
Devlin 2017/02/14 01:21:38 why not just something like: var keepalive = chrom
pkalinnikov 2017/02/14 13:49:51 Indeed.
+ var connectionErrorExpected;
+ if (expectedToConnect)
+ connectionOpenExpected = chrome.test.callbackAdded()
+ else
+ connectionErrorExpected = chrome.test.callbackAdded();
+
+ ws.onerror = function(error) {
+ chrome.test.log("WebSocket error: " + error);
Devlin 2017/02/14 01:21:38 single quotes (whole file)
pkalinnikov 2017/02/14 13:49:51 Done.
+ chrome.test.assertFalse(expectedToConnect);
+ connectionErrorExpected();
+ };
+ ws.onmessage = function(messageEvent) {
+ chrome.test.log("Message received: " + messageEvent.data);
+ chrome.test.assertTrue(expectedToConnect);
+ chrome.test.assertEq(MESSAGE, messageEvent.data);
+ ws.close();
+ };
+ ws.onclose = function(event) {
+ chrome.test.log("WebSocket closed.");
+ chrome.test.assertEq(expectedToConnect, event.wasClean);
+ }
+
+ ws.onopen = function() {
+ chrome.test.log("WebSocket opened.");
+ chrome.test.assertTrue(expectedToConnect);
+ connectionOpenExpected();
+ ws.send(MESSAGE);
+ };
+}
+
+chrome.tabs.getCurrent(function(tab) {
+ runTestsForTab([
+ // Opens a WebSocket connection, writes a message to it, and closes the
+ // connection. WebRequest API should observe the entire handshake.
+ function handshakeSucceeds() {
+ var webSocketUrl = getEchoTestURL(testWebSocketPort);
+ expect(
+ [ //events
+ { label: "onBeforeRequest",
+ event: "onBeforeRequest",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ frameUrl: "unknown frame URL",
+ },
+ },
+ { label: "onBeforeSendHeaders",
+ event: "onBeforeSendHeaders",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ },
+ },
+ { label: "onSendHeaders",
+ event: "onSendHeaders",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ },
+ },
+ { label: "onHeadersReceived",
+ event: "onHeadersReceived",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ statusCode: 101,
+ statusLine: "HTTP/1.1 101 Switching Protocols",
+ },
+ },
+ { label: "onResponseStarted",
+ event: "onResponseStarted",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ ip: "127.0.0.1",
+ fromCache: false,
+ statusCode: 101,
+ statusLine: "HTTP/1.1 101 Switching Protocols",
+ },
+ },
+ { label: "onCompleted",
+ event: "onCompleted",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ fromCache: false,
+ statusCode: 101,
+ statusLine: "HTTP/1.1 101 Switching Protocols",
+ }
+ },
+ ],
+ [ // event order
+ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
+ "onHeadersReceived", "onResponseStarted", "onCompleted"]
+ ],
+ {urls: ["<all_urls>"]}, // filter
+ ["blocking"] // extraInfoSpec
+ );
+ testWebSocketConnection(webSocketUrl);
+ },
+
+ // Tries to open a WebSocket connection, with a blocking handler that
+ // cancels the request. The connection will not establish.
+ function handshakeRequestCancelled() {
+ var webSocketUrl = getEchoTestURL(testWebSocketPort);
+ expect(
+ [ // events
+ { label: "onBeforeRequest",
+ event: "onBeforeRequest",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ frameUrl: "unknown frame URL",
+ },
+ retval: {cancel: true}
+ },
+ // Cancelling is considered an error.
+ { label: "onErrorOccurred",
+ event: "onErrorOccurred",
+ details: {
+ url: webSocketUrl,
+ type: "other",
+ fromCache: false,
+ error: "net::ERR_BLOCKED_BY_CLIENT"
+ }
+ },
+ ],
+ [ // event order
+ ["onBeforeRequest", "onErrorOccurred"]
+ ],
+ {urls: ["<all_urls>"]}, // filter
+ ["blocking"] // extraInfoSpec
+ );
+ testWebSocketConnection(webSocketUrl, false);
+ },
Devlin 2017/02/14 01:21:38 redirect test?
pkalinnikov 2017/02/14 13:49:51 I tried doing redirect, and it didn't work (see my
Devlin 2017/02/14 15:55:24 I think that disallowing redirect is fine, especia
pkalinnikov 2017/02/15 19:15:00 Added redirect test.
+ ], tab);
+});

Powered by Google App Engine
This is Rietveld 408576698