| OLD | NEW |
| 1 var ECHO_REQUEST_HEADERS_WS_URL = 'ws://127.0.0.1:8880/echo-request-headers'; | 1 var WS_ORIGIN = 'ws://127.0.0.1:8880'; |
| 2 var WS_REMOTE_ORIGIN = 'ws://localhost:8880'; |
| 3 var WS_ECHO_REQUEST_HEADERS_URL = WS_ORIGIN + '/echo-request-headers'; |
| 2 | 4 |
| 3 // Returns a Promise which will create a new WebSocket, and resolve to the value | 5 // Returns a Promise which will create a new WebSocket, and resolve to the value |
| 4 // of the specified header in the request, or reject with an error message if | 6 // of the specified header in the request, or reject with an error message if |
| 5 // something goes wrong. The header must be specified in lower-case. | 7 // something goes wrong. The header must be specified in lower-case. |
| 6 function connectAndGetRequestHeader(request_header) | 8 function connectAndGetRequestHeader(request_header) |
| 7 { | 9 { |
| 8 return connectAndGetRequestHeaders().then(function(headers) | 10 return connectAndGetRequestHeaders(WS_ORIGIN).then( |
| 9 { | 11 headers => headers[request_header]); |
| 10 return headers[request_header]; | 12 } |
| 11 }); | 13 |
| 14 function connectAndGetRequestHeaders() |
| 15 { |
| 16 return connectAndGetRequestHeadersFrom(WS_ORIGIN); |
| 12 } | 17 } |
| 13 | 18 |
| 14 // Returns a Promise which will create a new WebSocket, and return all the | 19 // Returns a Promise which will create a new WebSocket, and return all the |
| 15 // request headers as an object, with the header names as keys in lower-case. | 20 // request headers as an object, with the header names as keys in lower-case. |
| 16 function connectAndGetRequestHeaders() | 21 function connectAndGetRequestHeadersFrom(server) |
| 17 { | 22 { |
| 18 return new Promise(function(resolve, reject) { | 23 return new Promise(function(resolve, reject) { |
| 19 var header_ws = new WebSocket(ECHO_REQUEST_HEADERS_WS_URL); | 24 var header_ws = new WebSocket(server + '/echo-request-headers'); |
| 20 header_ws.onmessage = function (evt) { | 25 header_ws.onmessage = function (evt) { |
| 21 try { | 26 try { |
| 22 var headers = JSON.parse(evt.data); | 27 var headers = JSON.parse(evt.data); |
| 23 resolve(headers); | 28 resolve(headers); |
| 24 } catch (e) { | 29 } catch (e) { |
| 25 reject("Unexpected exception from JSON.parse: " + e); | 30 reject("Unexpected exception from JSON.parse: " + e); |
| 26 } | 31 } |
| 27 // The stringify() call in header_ws.onclose can cause a | 32 // The stringify() call in header_ws.onclose can cause a |
| 28 // "'WebSocket.URL' is deprecated." error as a side-effect even | 33 // "'WebSocket.URL' is deprecated." error as a side-effect even |
| 29 // though the reject() call itself does nothing. Clear out the | 34 // though the reject() call itself does nothing. Clear out the |
| (...skipping 20 matching lines...) Expand all Loading... |
| 50 function finishAsFailed(message) { | 55 function finishAsFailed(message) { |
| 51 testFailed(message); | 56 testFailed(message); |
| 52 finishJSTest(); | 57 finishJSTest(); |
| 53 } | 58 } |
| 54 | 59 |
| 55 // Extracts the value of a header from a WebSocket MessageEvent. | 60 // Extracts the value of a header from a WebSocket MessageEvent. |
| 56 function getRequestHeaderFromEvent(message_event, request_header) | 61 function getRequestHeaderFromEvent(message_event, request_header) |
| 57 { | 62 { |
| 58 return JSON.parse(message_event.data)[request_header]; | 63 return JSON.parse(message_event.data)[request_header]; |
| 59 } | 64 } |
| OLD | NEW |