tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

remote-close.any.js (3751B)


      1 // META: script=../../constants.sub.js
      2 // META: script=resources/url-constants.js
      3 // META: global=window,worker
      4 // META: variant=?default
      5 // META: variant=?wss
      6 // META: variant=?wpt_flags=h2
      7 
      8 'use strict';
      9 
     10 promise_test(async t => {
     11  const wss = new WebSocketStream(`${BASEURL}/remote-close?code=1000`);
     12  const { readable, writable } = await wss.opened;
     13  const { closeCode, reason } = await wss.closed;
     14  assert_equals(closeCode, 1000, 'code should be 1000');
     15  assert_equals(reason, '', 'reason should be empty');
     16  const { value, done } = await readable.getReader().read();
     17  assert_true(done, 'readable should be closed');
     18  await promise_rejects_dom(t, 'InvalidStateError', writable.getWriter().ready,
     19                            'writable should be errored');
     20 }, 'clean close should be clean');
     21 
     22 promise_test(async () => {
     23  const wss = new WebSocketStream(`${BASEURL}/remote-close`);
     24  const { closeCode, reason } = await wss.closed;
     25  assert_equals(closeCode, 1005, 'code should be No Status Rcvd');
     26  assert_equals(reason, '', 'reason should be empty');
     27 }, 'close frame with no body should result in status code 1005');
     28 
     29 promise_test(async () => {
     30  const wss = new WebSocketStream(`${BASEURL}/remote-close?code=4000&reason=robot`);
     31  const { closeCode, reason } = await wss.closed;
     32  assert_equals(closeCode, 4000, 'code should be 4000');
     33  assert_equals(reason, 'robot', 'reason should be set');
     34 }, 'reason should be passed through');
     35 
     36 promise_test(async () => {
     37  const wss = new WebSocketStream(`${BASEURL}/remote-close?code=4000&` +
     38                                  'reason=%E3%83%AD%E3%83%9C%E3%83%83%E3%83%88');
     39  const { reason } = await wss.closed;
     40  assert_equals(reason, 'ロボット', 'reason should be set');
     41 }, 'UTF-8 reason should work');
     42 
     43 promise_test(async t => {
     44  const wss = new WebSocketStream(`${BASEURL}/remote-close?code=4567`);
     45  const { writable } = await wss.opened;
     46  const veryLargeMessage = new Uint8Array(20 * 1024 * 1024);  // 20MB.
     47  const writePromise = writable.getWriter().write(veryLargeMessage);
     48  const closedError = await wss.closed.then(t.unreached_func('closed should reject'), e => e);
     49  assert_equals(closedError.constructor, WebSocketError, 'error should be WebSocketError');
     50  assert_equals(closedError.closeCode, 4567, 'closeCode should be set');
     51  await promise_rejects_dom(
     52      t, 'InvalidStateError', writePromise, 'write() should reject');
     53 }, 'close with unwritten data should not be considered clean');
     54 
     55 promise_test(async t => {
     56  const wss = new WebSocketStream(`${BASEURL}/remote-close?code=4222&reason=remote`);
     57  await wss.opened;
     58  wss.close({closeCode: 4111, reason: 'local'});
     59  const { closeCode, reason } = await wss.closed;
     60  assert_equals(closeCode, 4222, 'remote code should be used');
     61  assert_equals(reason, 'remote', 'remote reason should be used');
     62 }, 'remote code and reason should be used');
     63 
     64 promise_test(async t => {
     65  const wss = new WebSocketStream(`${BASEURL}/remote-close?abrupt=1`);
     66  const { readable, writable } = await wss.opened;
     67  const closedError = await wss.closed.then(t.unreached_func('closed should reject'), e => e);
     68  assert_equals(closedError.constructor, WebSocketError, 'error should be a WebSocketError');
     69  assert_equals(closedError.name, 'WebSocketError', 'error name should be WebSocketError');
     70  assert_equals(closedError.closeCode, 1006, 'code should be Abnormal Closure');
     71  await promise_rejects_exactly(t, closedError, readable.getReader().read(),
     72                                'readable should be errored with the same object');
     73  await promise_rejects_exactly(t, closedError, writable.getWriter().ready,
     74                                'writable should be errored with the same object');
     75 }, 'abrupt close should give an error');