Index: chrome/test/data/extensions/api_test/webrequest/websocket.js |
diff --git a/chrome/test/data/extensions/api_test/webrequest/websocket.js b/chrome/test/data/extensions/api_test/webrequest/websocket.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e9414faf1359d0fbda772efbbc40173f5678501c |
--- /dev/null |
+++ b/chrome/test/data/extensions/api_test/webrequest/websocket.js |
@@ -0,0 +1,40 @@ |
+// 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 getWSTestURL(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) { |
+ 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); |
+ }; |
+} |