Chromium Code Reviews| 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 getEchoTestURL(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 = true) { | |
| 13 var ws = new WebSocket(url); | |
| 14 var MESSAGE = "test message"; | |
|
Devlin
2017/02/14 01:21:38
single quotes in js. Also, typically prefer kMess
pkalinnikov
2017/02/14 13:49:51
Done.
| |
| 15 | |
| 16 var connectionOpenExpected; | |
|
Devlin
2017/02/14 01:21:38
why not just something like:
var keepalive = chrom
pkalinnikov
2017/02/14 13:49:51
Indeed.
| |
| 17 var connectionErrorExpected; | |
| 18 if (expectedToConnect) | |
| 19 connectionOpenExpected = chrome.test.callbackAdded() | |
| 20 else | |
| 21 connectionErrorExpected = chrome.test.callbackAdded(); | |
| 22 | |
| 23 ws.onerror = function(error) { | |
| 24 chrome.test.log("WebSocket error: " + error); | |
|
Devlin
2017/02/14 01:21:38
single quotes (whole file)
pkalinnikov
2017/02/14 13:49:51
Done.
| |
| 25 chrome.test.assertFalse(expectedToConnect); | |
| 26 connectionErrorExpected(); | |
| 27 }; | |
| 28 ws.onmessage = function(messageEvent) { | |
| 29 chrome.test.log("Message received: " + messageEvent.data); | |
| 30 chrome.test.assertTrue(expectedToConnect); | |
| 31 chrome.test.assertEq(MESSAGE, messageEvent.data); | |
| 32 ws.close(); | |
| 33 }; | |
| 34 ws.onclose = function(event) { | |
| 35 chrome.test.log("WebSocket closed."); | |
| 36 chrome.test.assertEq(expectedToConnect, event.wasClean); | |
| 37 } | |
| 38 | |
| 39 ws.onopen = function() { | |
| 40 chrome.test.log("WebSocket opened."); | |
| 41 chrome.test.assertTrue(expectedToConnect); | |
| 42 connectionOpenExpected(); | |
| 43 ws.send(MESSAGE); | |
| 44 }; | |
| 45 } | |
| 46 | |
| 47 chrome.tabs.getCurrent(function(tab) { | |
| 48 runTestsForTab([ | |
| 49 // Opens a WebSocket connection, writes a message to it, and closes the | |
| 50 // connection. WebRequest API should observe the entire handshake. | |
| 51 function handshakeSucceeds() { | |
| 52 var webSocketUrl = getEchoTestURL(testWebSocketPort); | |
| 53 expect( | |
| 54 [ //events | |
| 55 { label: "onBeforeRequest", | |
| 56 event: "onBeforeRequest", | |
| 57 details: { | |
| 58 url: webSocketUrl, | |
| 59 type: "other", | |
| 60 frameUrl: "unknown frame URL", | |
| 61 }, | |
| 62 }, | |
| 63 { label: "onBeforeSendHeaders", | |
| 64 event: "onBeforeSendHeaders", | |
| 65 details: { | |
| 66 url: webSocketUrl, | |
| 67 type: "other", | |
| 68 }, | |
| 69 }, | |
| 70 { label: "onSendHeaders", | |
| 71 event: "onSendHeaders", | |
| 72 details: { | |
| 73 url: webSocketUrl, | |
| 74 type: "other", | |
| 75 }, | |
| 76 }, | |
| 77 { label: "onHeadersReceived", | |
| 78 event: "onHeadersReceived", | |
| 79 details: { | |
| 80 url: webSocketUrl, | |
| 81 type: "other", | |
| 82 statusCode: 101, | |
| 83 statusLine: "HTTP/1.1 101 Switching Protocols", | |
| 84 }, | |
| 85 }, | |
| 86 { label: "onResponseStarted", | |
| 87 event: "onResponseStarted", | |
| 88 details: { | |
| 89 url: webSocketUrl, | |
| 90 type: "other", | |
| 91 ip: "127.0.0.1", | |
| 92 fromCache: false, | |
| 93 statusCode: 101, | |
| 94 statusLine: "HTTP/1.1 101 Switching Protocols", | |
| 95 }, | |
| 96 }, | |
| 97 { label: "onCompleted", | |
| 98 event: "onCompleted", | |
| 99 details: { | |
| 100 url: webSocketUrl, | |
| 101 type: "other", | |
| 102 fromCache: false, | |
| 103 statusCode: 101, | |
| 104 statusLine: "HTTP/1.1 101 Switching Protocols", | |
| 105 } | |
| 106 }, | |
| 107 ], | |
| 108 [ // event order | |
| 109 ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", | |
| 110 "onHeadersReceived", "onResponseStarted", "onCompleted"] | |
| 111 ], | |
| 112 {urls: ["<all_urls>"]}, // filter | |
| 113 ["blocking"] // extraInfoSpec | |
| 114 ); | |
| 115 testWebSocketConnection(webSocketUrl); | |
| 116 }, | |
| 117 | |
| 118 // Tries to open a WebSocket connection, with a blocking handler that | |
| 119 // cancels the request. The connection will not establish. | |
| 120 function handshakeRequestCancelled() { | |
| 121 var webSocketUrl = getEchoTestURL(testWebSocketPort); | |
| 122 expect( | |
| 123 [ // events | |
| 124 { label: "onBeforeRequest", | |
| 125 event: "onBeforeRequest", | |
| 126 details: { | |
| 127 url: webSocketUrl, | |
| 128 type: "other", | |
| 129 frameUrl: "unknown frame URL", | |
| 130 }, | |
| 131 retval: {cancel: true} | |
| 132 }, | |
| 133 // Cancelling is considered an error. | |
| 134 { label: "onErrorOccurred", | |
| 135 event: "onErrorOccurred", | |
| 136 details: { | |
| 137 url: webSocketUrl, | |
| 138 type: "other", | |
| 139 fromCache: false, | |
| 140 error: "net::ERR_BLOCKED_BY_CLIENT" | |
| 141 } | |
| 142 }, | |
| 143 ], | |
| 144 [ // event order | |
| 145 ["onBeforeRequest", "onErrorOccurred"] | |
| 146 ], | |
| 147 {urls: ["<all_urls>"]}, // filter | |
| 148 ["blocking"] // extraInfoSpec | |
| 149 ); | |
| 150 testWebSocketConnection(webSocketUrl, false); | |
| 151 }, | |
|
Devlin
2017/02/14 01:21:38
redirect test?
pkalinnikov
2017/02/14 13:49:51
I tried doing redirect, and it didn't work (see my
Devlin
2017/02/14 15:55:24
I think that disallowing redirect is fine, especia
pkalinnikov
2017/02/15 19:15:00
Added redirect test.
| |
| 152 ], tab); | |
| 153 }); | |
| OLD | NEW |