Repository navigation
Expand file tree
/
Copy pathresponse_http2_spec.ts
More file actions
193 lines (165 loc) · 5.26 KB
/
Copy pathresponse_http2_spec.ts
File metadata and controls
193 lines (165 loc) · 5.26 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
Http2Server,
IncomingHttpHeaders,
IncomingHttpStatusHeader,
connect,
createServer,
} from 'node:http2';
import { AddressInfo } from 'node:net';
import { writeResponseToNodeResponse } from '../src/response';
describe('writeResponseToNodeResponse (HTTP/2)', () => {
let server: Http2Server;
function simulateResponse(res: Response): Promise<{
body: string | null;
headers: IncomingHttpHeaders & IncomingHttpStatusHeader;
statusCode: number | undefined;
}> {
server.once('request', (req, nodeResponse) => {
void writeResponseToNodeResponse(res, nodeResponse);
});
const { port } = server.address() as AddressInfo;
const client = connect(`http://localhost:${port}`);
return new Promise<{
body: string | null;
headers: IncomingHttpHeaders & IncomingHttpStatusHeader;
statusCode: number | undefined;
}>((resolve, reject) => {
const req = client.request({
':path': '/',
});
req.once('response', (headers) => {
let body: string | null = null;
req
.on('data', (chunk) => {
body ??= '';
body += chunk;
})
.on('end', () => resolve({ headers, statusCode: headers[':status'], body }))
.on('error', reject);
});
}).finally(() => {
client.close();
});
}
beforeAll((done) => {
server = createServer();
server.listen(0, done);
});
afterAll((done) => {
server.close(done);
});
it('should write status, headers, and body to Node.js response', async () => {
const { headers, statusCode, body } = await simulateResponse(
new Response('Hello, world!', {
status: 201,
headers: {
'Content-Type': 'text/plain',
'X-Custom-Header': 'custom-value',
},
}),
);
expect(statusCode).toBe(201);
expect(headers['content-type']).toBe('text/plain');
expect(headers['x-custom-header']).toBe('custom-value');
expect(body).toBe('Hello, world!');
});
it('should handle empty body', async () => {
const { statusCode, body } = await simulateResponse(
new Response(null, {
status: 204,
}),
);
expect(statusCode).toBe(204);
expect(body).toBeNull();
});
it('should handle JSON content types', async () => {
const jsonData = JSON.stringify({ message: 'Hello JSON' });
const { statusCode, body } = await simulateResponse(
new Response(jsonData, {
headers: { 'Content-Type': 'application/json' },
}),
);
expect(statusCode).toBe(200);
expect(body).toBe(jsonData);
});
it('should set cookies on the ServerResponse', async () => {
const cookieValue: string[] = [
'myCookie=myValue; Path=/; HttpOnly',
'anotherCookie=anotherValue; Path=/test',
];
const headers = new Headers();
cookieValue.forEach((v) => headers.append('set-cookie', v));
const { headers: resHeaders } = await simulateResponse(new Response(null, { headers }));
expect(resHeaders['set-cookie']).toEqual(cookieValue);
});
it('should resolve and cancel reader when client disconnects while response is backpressured', async () => {
let writePromise!: Promise<void>;
let readerCancelled = false;
const largeChunk = 'x'.repeat(1024 * 1024); // 1MB to exceed HTTP/2 flow control window
const stream = new ReadableStream({
start(controller) {
controller.enqueue(largeChunk);
controller.enqueue(largeChunk);
},
cancel() {
readerCancelled = true;
},
});
server.once('request', (_, nodeResponse) => {
writePromise = writeResponseToNodeResponse(new Response(stream), nodeResponse);
});
await new Promise<void>((resolve) => {
const { port } = server.address() as AddressInfo;
const client = connect(`http://localhost:${port}`);
const req = client.request({
':path': '/',
});
req.once('response', () => {
req.once('data', () => {
req.destroy();
client.destroy();
resolve();
});
});
req.on('error', () => {
// Expected when destroying/closing the stream
});
req.end();
});
await writePromise;
expect(readerCancelled).toBeTrue();
});
it('should cancel response body when destination is already closed', async () => {
let writePromise!: Promise<void>;
let bodyCancelled = false;
const stream = new ReadableStream({
cancel() {
bodyCancelled = true;
},
});
server.once('request', (_, nodeResponse) => {
nodeResponse.end();
writePromise = writeResponseToNodeResponse(new Response(stream), nodeResponse);
});
const { port } = server.address() as AddressInfo;
const client = connect(`http://localhost:${port}`);
await new Promise<void>((resolve, reject) => {
const req = client.request({ ':path': '/' });
req.resume();
req.on('end', resolve);
req.on('error', reject);
req.end();
}).finally(() => {
client.close();
});
await writePromise;
expect(bodyCancelled).toBeTrue();
});
});