constructor.any.js (2831B)
1 // META: script=../../constants.sub.js 2 // META: script=resources/url-constants.js 3 // META: global=window,worker 4 // META: variant=?wss 5 // META: variant=?wpt_flags=h2 6 7 test(() => { 8 assert_throws_js(TypeError, () => new WebSocketStream(), 9 'constructor should throw'); 10 }, 'constructing with no URL should throw'); 11 12 test(() => { 13 assert_throws_dom('SyntaxError', () => new WebSocketStream('invalid:'), 14 'constructor should throw'); 15 }, 'constructing with an invalid URL should throw'); 16 17 test(() => { 18 assert_throws_js(TypeError, 19 () => new WebSocketStream(`${BASEURL}/`, true), 20 'constructor should throw'); 21 }, 'constructing with invalid options should throw'); 22 23 test(() => { 24 assert_throws_js(TypeError, 25 () => new WebSocketStream(`${BASEURL}/`, {protocols: 'hi'}), 26 'constructor should throw'); 27 }, 'protocols should be required to be a list'); 28 29 promise_test(async () => { 30 const wss = new WebSocketStream(ECHOURL); 31 await wss.opened; 32 assert_equals(wss.url, ECHOURL, 'url should match'); 33 wss.close(); 34 }, 'constructing with a valid URL should work'); 35 36 promise_test(async () => { 37 const wss = new WebSocketStream(`${BASEURL}/protocol_array`, 38 {protocols: ['alpha', 'beta']}); 39 const { readable, protocol } = await wss.opened; 40 assert_equals(protocol, 'alpha', 'protocol should be right'); 41 const reader = readable.getReader(); 42 const { value, done } = await reader.read(); 43 assert_equals(value, 'alpha', 'message contents should match'); 44 wss.close(); 45 }, 'setting a protocol in the constructor should work'); 46 47 function IsWebSocketError(e) { 48 return e.constructor == WebSocketError; 49 } 50 51 promise_test(t => { 52 const wss = new WebSocketStream(`${BASEURL}/404`); 53 return Promise.all([ 54 wss.opened.then(t.unreached_func('should have rejected')).catch(e => assert_true(IsWebSocketError(e))), 55 wss.closed.then(t.unreached_func('should have rejected')).catch(e => assert_true(IsWebSocketError(e))), 56 ]); 57 }, 'connection failure should reject the promises'); 58 59 promise_test(async () => { 60 const wss = new WebSocketStream(ECHOURL); 61 const { readable, writable, protocol, extensions} = await wss.opened; 62 // Verify that |readable| really is a ReadableStream using the getReader() 63 // brand check. If it doesn't throw the test passes. 64 ReadableStream.prototype.getReader.call(readable); 65 // Verify that |writable| really is a WritableStream using the getWriter() 66 // brand check. If it doesn't throw the test passes. 67 WritableStream.prototype.getWriter.call(writable); 68 assert_equals(typeof protocol, 'string', 'protocol should be a string'); 69 assert_equals(typeof extensions, 'string', 'extensions should be a string'); 70 wss.close(); 71 }, 'wss.opened should resolve to the right types');