Chromium Code Reviews| 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..9c1ac39cea4f630e98a9d7a2953b4fc3d8cda6dd |
| --- /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) { |
| + var 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, |
|
battre
2017/02/07 15:03:02
Hm, do you think this can be fixed?
pkalinnikov
2017/02/09 18:15:05
TL;DR: This is not a bug. See chrome/test/data/ext
|
| + 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", |
| + }, |
| + }, |
| + { label: "onCompleted", |
| + event: "onCompleted", |
| + details: { |
| + url: webSocketUrl, |
| + type: "other", |
| + tabId: 1, |
| + 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 |
| + ); |
| + |
| + testBody(testWebSocketPort); |
| +} |
| + |
| +runTests([ |
| + // TODO(pkalinnikov): Add tests exercising redirects and blocking. |
|
battre
2017/02/07 15:03:02
yes, please.
pkalinnikov
2017/02/09 18:15:04
Added a test with blocking.
I also tried adding a
|
| + |
| + // 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); |
| + }, |
| +]); |