|
16 | 16 |
|
17 | 17 | import { describe, beforeEach, afterEach, beforeAll, afterAll, it, vi, expect } from 'vitest'; |
18 | 18 |
|
| 19 | +import http from 'http'; |
19 | 20 | import nock from 'nock'; |
20 | 21 | import zlib from 'zlib'; |
21 | 22 | import { NodeRequestHandler } from './request_handler.node'; |
@@ -202,6 +203,90 @@ describe('NodeRequestHandler', () => { |
202 | 203 | scope.done(); |
203 | 204 | }); |
204 | 205 |
|
| 206 | + describe('content-length header', () => { |
| 207 | + let server: http.Server; |
| 208 | + let port: number; |
| 209 | + let receivedContentLength: string | undefined; |
| 210 | + let receivedBodyByteLength: number; |
| 211 | + |
| 212 | + beforeAll(async () => { |
| 213 | + nock.enableNetConnect('localhost'); |
| 214 | + server = http.createServer((req, res) => { |
| 215 | + receivedContentLength = req.headers['content-length']; |
| 216 | + |
| 217 | + const chunks: Buffer[] = []; |
| 218 | + req.on('data', (chunk: Buffer) => chunks.push(chunk)); |
| 219 | + req.on('end', () => { |
| 220 | + receivedBodyByteLength = Buffer.concat(chunks).length; |
| 221 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 222 | + res.end(JSON.stringify({ ok: true })); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + await new Promise<void>((resolve) => { |
| 227 | + server.listen(0, () => { |
| 228 | + port = (server.address() as { port: number }).port; |
| 229 | + resolve(); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + afterAll(async () => { |
| 235 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 236 | + nock.disableNetConnect(); |
| 237 | + }); |
| 238 | + |
| 239 | + it('should set correct content-length for ASCII-only data', async () => { |
| 240 | + const data = '{"key":"value"}'; |
| 241 | + |
| 242 | + const { responsePromise } = nodeRequestHandler.makeRequest( |
| 243 | + `http://localhost:${port}/test`, |
| 244 | + { 'content-type': 'application/json' }, |
| 245 | + 'POST', |
| 246 | + data, |
| 247 | + ); |
| 248 | + await responsePromise; |
| 249 | + |
| 250 | + const expectedByteLength = Buffer.byteLength(data, 'utf8'); |
| 251 | + expect(Number(receivedContentLength)).toBe(expectedByteLength); |
| 252 | + expect(Number(receivedContentLength)).toBe(receivedBodyByteLength); |
| 253 | + }); |
| 254 | + |
| 255 | + it('should set correct content-length for multi-byte UTF-8 data (emoji)', async () => { |
| 256 | + const data = JSON.stringify({ message: '🚀 launch' }); |
| 257 | + |
| 258 | + const { responsePromise } = nodeRequestHandler.makeRequest( |
| 259 | + `http://localhost:${port}/test`, |
| 260 | + { 'content-type': 'application/json' }, |
| 261 | + 'POST', |
| 262 | + data, |
| 263 | + ); |
| 264 | + await responsePromise; |
| 265 | + |
| 266 | + const expectedByteLength = Buffer.byteLength(data, 'utf8'); |
| 267 | + expect(data.length).not.toBe(expectedByteLength); |
| 268 | + expect(Number(receivedContentLength)).toBe(expectedByteLength); |
| 269 | + expect(Number(receivedContentLength)).toBe(receivedBodyByteLength); |
| 270 | + }); |
| 271 | + |
| 272 | + it('should set correct content-length for multi-byte UTF-8 data (CJK characters)', async () => { |
| 273 | + const data = JSON.stringify({ greeting: '你好世界' }); |
| 274 | + |
| 275 | + const { responsePromise } = nodeRequestHandler.makeRequest( |
| 276 | + `http://localhost:${port}/test`, |
| 277 | + { 'content-type': 'application/json' }, |
| 278 | + 'POST', |
| 279 | + data, |
| 280 | + ); |
| 281 | + await responsePromise; |
| 282 | + |
| 283 | + const expectedByteLength = Buffer.byteLength(data, 'utf8'); |
| 284 | + expect(data.length).not.toBe(expectedByteLength); |
| 285 | + expect(Number(receivedContentLength)).toBe(expectedByteLength); |
| 286 | + expect(Number(receivedContentLength)).toBe(receivedBodyByteLength); |
| 287 | + }); |
| 288 | + }); |
| 289 | + |
205 | 290 | describe('timeout', () => { |
206 | 291 | beforeEach(() => { |
207 | 292 | vi.useFakeTimers(); |
|
0 commit comments