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..115821042558abdda603a7bff35ecca84d3c28a7 |
| --- /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) { |
| + webSocketUrl = getEchoTestURL(testWebSocketPort); |
|
tyoshino (SeeGerritForStatus)
2016/12/06 08:05:06
var
pkalinnikov
2016/12/13 21:15:59
Done.
|
| + 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([ |
| + // TODO(pkalinnikov): Add tests excercising redirects and blocking. |
|
tyoshino (SeeGerritForStatus)
2016/12/06 08:05:06
exercise
pkalinnikov
2016/12/13 21:15:59
Done.
|
| + |
| + // 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); |
| + }, |
| +]); |