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..9da6668c1242458fd2b122bda4b5b1f8fc084c1e |
--- /dev/null |
+++ b/chrome/test/data/extensions/api_test/webrequest/test_websocket.js |
@@ -0,0 +1,129 @@ |
+// Copyright 2016 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"; |
+} |
+ |
+function openAndCloseWebSocket(port) { |
+ var url = getEchoTestURL(port); |
+ var ws = new WebSocket(url); |
+ |
+ ws.onclose = function() { |
+ chrome.test.log("WebSocket closed."); |
+ } |
+ ws.onopen = function() { |
+ chrome.test.log("WebSocket opened."); |
+ }; |
+} |
+ |
+function openWebSocketThenWriteAndClose(port) { |
+ var url = getEchoTestURL(port); |
+ var ws = new WebSocket(url); |
+ var MESSAGE = "test message"; |
+ |
+ ws.onmessage = function(messageEvent) { |
+ chrome.test.log("Message received: " + messageEvent.data); |
+ chrome.test.assertEq(MESSAGE, messageEvent.data); |
+ ws.close(); |
+ }; |
+ ws.onclose = function() { |
+ chrome.test.log("WebSocket closed."); |
+ } |
+ ws.onopen = function() { |
+ chrome.test.log("WebSocket opened."); |
+ ws.send(MESSAGE); |
+ }; |
+} |
+ |
+function webSocketEchoTestImpl(testBody) { |
battre (please use the other)
2016/11/16 21:51:58
I think that ultimately we want more tests that ex
pkalinnikov
2016/11/17 11:34:11
Added a TODO.
|
+ webSocketUrl = getEchoTestURL(testWebSocketPort); |
+ expect( |
+ [ //events |
+ { label: "onBeforeRequest", |
+ event: "onBeforeRequest", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ // Note: The tab is different from the one where navigation happened. |
+ tabId: 1, |
+ frameUrl: "unknown frame URL", |
+ }, |
+ }, |
+ { label: "onBeforeSendHeaders", |
+ event: "onBeforeSendHeaders", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ tabId: 1, |
+ }, |
+ }, |
+ { label: "onSendHeaders", |
+ event: "onSendHeaders", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ tabId: 1, |
+ }, |
+ }, |
+ { label: "onHeadersReceived", |
+ event: "onHeadersReceived", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ tabId: 1, |
+ statusCode: 101, |
+ statusLine: "HTTP/1.1 101 Switching Protocols", |
+ }, |
+ }, |
+ { label: "onResponseStarted", |
+ event: "onResponseStarted", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ tabId: 1, |
+ ip: "127.0.0.1", |
+ fromCache: false, |
+ statusCode: 101, |
+ statusLine: "HTTP/1.1 101 Switching Protocols", |
+ }, |
+ }, |
+ // FIXME(pkalinnikov): Switch to onCompleted once crbug/663672 is fixed. |
+ { label: "onErrorOccurred", |
+ event: "onErrorOccurred", |
+ details: { |
+ url: webSocketUrl, |
+ type: "other", |
+ tabId: 1, |
+ fromCache: false, |
+ error: "net::ERR_ABORTED", |
+ }, |
+ }, |
+ ], |
+ [ // event order |
+ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", |
+ "onHeadersReceived", "onResponseStarted", "onErrorOccurred"] |
+ ], |
+ {urls: ["<all_urls>"]}, // filter |
+ ["blocking"] // extraInfoSpec |
+ ); |
+ |
+ testBody(testWebSocketPort); |
+} |
+ |
+runTests([ |
+ // FIXME(pkalinnikov): Make the tests work. Now they hang forever. |
battre (please use the other)
2016/11/16 21:51:58
Did you fix this?
pkalinnikov
2016/11/17 11:34:11
Yes. Forgot to delete the comment.
|
+ |
+ // Opens a WebSocket connection and closes it. WebRequest API should observe |
+ // only the handshake. |
+ function openAndCloseWebSocketTest() { |
+ webSocketEchoTestImpl(openAndCloseWebSocket); |
+ }, |
+ |
+ // Opens a WebSocket connection, writes a message to it, and closes the |
+ // connection. WebRequest API should observe only the handshake. |
+ function openWebSocketThenWriteAndCloseTest() { |
+ webSocketEchoTestImpl(openWebSocketThenWriteAndClose); |
+ }, |
+]); |