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

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

Issue 2449913002: Support WebSocket in WebRequest API. (Closed)
Patch Set: Fix unittest; add 'websocket' type to WebRequest API. 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..ad43c9ea99f7a9aba5d75d116502940ad59cbf28
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/webrequest/test_websocket.js
@@ -0,0 +1,223 @@
+// 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) {
Devlin 2017/02/15 22:12:58 nitty nit: I don't think the default argument here
pkalinnikov 2017/02/16 21:31:07 Done.
+ var ws = new WebSocket(url);
+ var kMessage = 'test message';
+
+ var keepAlive = chrome.test.callbackAdded();
+
+ ws.onerror = function(error) {
+ chrome.test.log('WebSocket error: ' + error);
+ chrome.test.assertFalse(expectedToConnect);
+ keepAlive();
+ };
+ ws.onmessage = function(messageEvent) {
+ chrome.test.log('Message received: ' + messageEvent.data);
+ chrome.test.assertTrue(expectedToConnect);
+ chrome.test.assertEq(kMessage, 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);
+ keepAlive();
+ ws.send(kMessage);
+ };
+}
+
+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: 'websocket',
+ frameUrl: 'unknown frame URL',
+ },
+ },
+ { label: 'onBeforeSendHeaders',
+ event: 'onBeforeSendHeaders',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ },
+ },
+ { label: 'onSendHeaders',
+ event: 'onSendHeaders',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ },
+ },
+ { label: 'onHeadersReceived',
+ event: 'onHeadersReceived',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ statusCode: 101,
+ statusLine: 'HTTP/1.1 101 Switching Protocols',
+ },
+ },
+ { label: 'onResponseStarted',
+ event: 'onResponseStarted',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ ip: '127.0.0.1',
+ fromCache: false,
+ statusCode: 101,
+ statusLine: 'HTTP/1.1 101 Switching Protocols',
+ },
+ },
+ { label: 'onCompleted',
+ event: 'onCompleted',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ 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: 'websocket',
+ frameUrl: 'unknown frame URL',
+ },
+ retval: {cancel: true}
+ },
+ // Cancelling is considered an error.
+ { label: 'onErrorOccurred',
+ event: 'onErrorOccurred',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ fromCache: false,
+ error: 'net::ERR_BLOCKED_BY_CLIENT'
+ }
+ },
+ ],
+ [ // event order
+ ['onBeforeRequest', 'onErrorOccurred']
+ ],
+ {urls: ['<all_urls>']}, // filter
+ ['blocking'] // extraInfoSpec
+ );
+ testWebSocketConnection(webSocketUrl, false);
+ },
+
+ // Opens a WebSocket connection, with a blocking handler that tries to
+ // redirect the request. The redirect will be ignored.
+ function redirectIsIgnoredAndHandshakeSucceeds() {
+ var webSocketUrl = getEchoTestURL(testWebSocketPort);
+ var webSocketRedirectedUrl1 = getEchoTestURL(testWebSocketPort)
+ + '?redirected1';
+ var webSocketRedirectedUrl2 = getEchoTestURL(testWebSocketPort)
+ + '?redirected2';
+ expect(
+ [ // events
+ { label: 'onBeforeRequest',
+ event: 'onBeforeRequest',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ frameUrl: 'unknown frame URL',
+ },
+ retval: {redirectUrl: webSocketRedirectedUrl1}
+ },
+ { label: 'onBeforeSendHeaders',
+ event: 'onBeforeSendHeaders',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ },
+ },
+ { label: 'onSendHeaders',
+ event: 'onSendHeaders',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ },
+ },
+ { label: 'onHeadersReceived',
+ event: 'onHeadersReceived',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ statusCode: 101,
+ statusLine: 'HTTP/1.1 101 Switching Protocols',
+ },
+ retval: {redirectUrl: webSocketRedirectedUrl2}
+ },
+ { label: 'onResponseStarted',
+ event: 'onResponseStarted',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ ip: '127.0.0.1',
+ fromCache: false,
+ statusCode: 101,
+ statusLine: 'HTTP/1.1 101 Switching Protocols',
+ },
+ },
+ { label: 'onCompleted',
+ event: 'onCompleted',
+ details: {
+ url: webSocketUrl,
+ type: 'websocket',
+ fromCache: false,
+ statusCode: 101,
+ statusLine: 'HTTP/1.1 101 Switching Protocols',
+ }
+ },
+ ],
+ [ // event order
+ ['onBeforeRequest', 'onBeforeSendHeaders', 'onHeadersReceived',
+ 'onResponseStarted', 'onCompleted']
+ ],
+ {urls: ['<all_urls>']}, // filter
+ ['blocking'] // extraInfoSpec
+ );
+ testWebSocketConnection(webSocketUrl);
+ },
+ ], tab);
+});

Powered by Google App Engine
This is Rietveld 408576698