OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 function getWSTestURL(port) { |
| 6 return 'ws://localhost:' + port + '/echo-with-no-extension'; |
| 7 } |
| 8 |
| 9 // Tries to: open a WebSocket, write a test message to it, close it. Verifies |
| 10 // that all the necessary events are triggered if |expectedToConnect|, otherwise |
| 11 // makes sure WebSocket terminates with an error. |
| 12 function testWebSocketConnection(url, expectedToConnect) { |
| 13 var ws = new WebSocket(url); |
| 14 var kMessage = 'test message'; |
| 15 |
| 16 var keepAlive = chrome.test.callbackAdded(); |
| 17 |
| 18 ws.onerror = function(error) { |
| 19 chrome.test.log('WebSocket error: ' + error); |
| 20 chrome.test.assertFalse(expectedToConnect); |
| 21 keepAlive(); |
| 22 }; |
| 23 ws.onmessage = function(messageEvent) { |
| 24 chrome.test.log('Message received: ' + messageEvent.data); |
| 25 chrome.test.assertTrue(expectedToConnect); |
| 26 chrome.test.assertEq(kMessage, messageEvent.data); |
| 27 ws.close(); |
| 28 }; |
| 29 ws.onclose = function(event) { |
| 30 chrome.test.log('WebSocket closed.'); |
| 31 chrome.test.assertEq(expectedToConnect, event.wasClean); |
| 32 } |
| 33 |
| 34 ws.onopen = function() { |
| 35 chrome.test.log('WebSocket opened.'); |
| 36 chrome.test.assertTrue(expectedToConnect); |
| 37 keepAlive(); |
| 38 ws.send(kMessage); |
| 39 }; |
| 40 } |
OLD | NEW |