summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webtransport/streams-echo.https.any.js
blob: 32781419ebf7bd83d376419ca07e2829196ed4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// META: global=window,worker
// META: script=/common/get-host-info.sub.js
// META: script=resources/webtransport-test-helpers.sub.js

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a bidirectional stream.
  const bidi_stream = await wt.createBidirectionalStream();

  // Write a message to the writable end, and close it.
  const writer = bidi_stream.writable.getWriter();
  const encoder = new TextEncoder();
  await writer.write(encoder.encode('Hello World'));
  await writer.close();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(bidi_stream.readable);

  // Check that the message from the readable end matches the writable end.
  assert_equals(reply, 'Hello World');
}, 'WebTransport server should be able to create and handle a bidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // The echo handler creates a bidirectional stream when a WebTransport session
  // is established. Accept the bidirectional stream.
  const stream_reader = wt.incomingBidirectionalStreams.getReader();
  const { value: bidi_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Write a message to the writable end, and close it.
  const encoder = new TextEncoderStream();
  encoder.readable.pipeTo(bidi_stream.writable);
  const writer = encoder.writable.getWriter();
  await writer.write('Hello World');
  await writer.close();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(bidi_stream.readable);

  // Check that the message from the readable end matches the writable end.
  assert_equals(reply, 'Hello World');
}, 'WebTransport server should be able to accept and handle a bidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a unidirectional stream.
  const writable = await wt.createUnidirectionalStream();

  // Write a message to the writable end, and close it.
  const encoder = new TextEncoderStream();
  encoder.readable.pipeTo(writable);
  const writer = encoder.writable.getWriter();
  await writer.write('Hello World');
  await writer.close();

  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const { value: recv_stream } = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end.
  const reply = await read_stream_as_string(recv_stream);

  // Make sure the message on the writable and readable ends of the streams
  // match.
  assert_equals(reply, 'Hello World');
}, 'WebTransport server should be able to create, accept, and handle a unidirectional stream');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // The echo handler creates a bidirectional stream when a WebTransport session
  // is established. Accept the bidirectional stream.
  const stream_reader = wt.incomingBidirectionalStreams.getReader();
  const {value: bidi_stream} = await stream_reader.read();
  stream_reader.releaseLock();

  // Write data to the writable end, and close it.
  const buffer_size = 256;
  const data = new Uint8Array(buffer_size);
  for (let i = 0; i < data.byteLength; ++i) {
    data[i] = i;
  }
  const writer = bidi_stream.writable.getWriter();
  writer.write(data);
  await writer.close();

  // Read the data on the readable end and check if it matches the writable end.
  const reader = bidi_stream.readable.getReader({mode: 'byob'});
  assert_true(reader instanceof ReadableStreamBYOBReader);
  const half_buffer_size = buffer_size / 2;
  for (let i = 0; i < 2; i++) {
    let buffer = new ArrayBuffer(half_buffer_size);
    buffer = await readInto(reader, buffer);
    assert_array_equals(
        new Uint8Array(buffer),
        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
  }
  reader.releaseLock();
}, 'Can read data from a bidirectional stream with BYOB reader');

promise_test(async t => {
  // Establish a WebTransport session.
  const wt = new WebTransport(webtransport_url('echo.py'));
  await wt.ready;

  // Create a unidirectional stream.
  const writable = await wt.createUnidirectionalStream();

  // Write data to the writable end, and close it.
  const buffer_size = 256;
  const data = new Uint8Array(buffer_size);
  for (let i = 0; i < data.byteLength; ++i) {
    data[i] = i;
  }
  const writer = writable.getWriter();
  writer.write(data);
  await writer.close();

  // The echo handler creates a new unidirectional stream to echo back data from
  // the server to client. Accept the unidirectional stream.
  const readable = wt.incomingUnidirectionalStreams;
  const stream_reader = readable.getReader();
  const {value: recv_stream} = await stream_reader.read();
  stream_reader.releaseLock();

  // Read the data on the readable end and check if it matches the writable end.
  const reader = recv_stream.getReader({mode: 'byob'});
  assert_true(reader instanceof ReadableStreamBYOBReader);
  const half_buffer_size = buffer_size / 2;
  let buffer = new ArrayBuffer(half_buffer_size);
  for (let i = 0; i < 2; i++) {
    buffer = await readInto(reader, buffer);
    assert_array_equals(
        new Uint8Array(buffer),
        data.subarray(half_buffer_size * i, half_buffer_size * (i + 1)))
  }
  reader.releaseLock();
}, 'Can read data from a unidirectional stream with BYOB reader');