|
2 | 2 |
|
3 | 3 | const { once } = require('node:events') |
4 | 4 | const { createServer } = require('node:http') |
5 | | -const { test } = require('node:test') |
| 5 | +const { test, before, after, describe } = require('node:test') |
6 | 6 | const { tspl } = require('@matteo.collina/tspl') |
7 | 7 | const { fetch } = require('../..') |
8 | 8 |
|
9 | | -test('content-encoding header', async (t) => { |
10 | | - const { strictEqual } = tspl(t, { plan: 2 }) |
11 | | - |
12 | | - const contentEncoding = 'deflate, gzip' |
13 | | - const text = 'Hello, World!' |
| 9 | +describe('content-encoding handling', () => { |
14 | 10 | const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64') |
15 | | - |
16 | | - const server = createServer((req, res) => { |
17 | | - res.writeHead(200, |
18 | | - { |
19 | | - 'Content-Encoding': contentEncoding, |
20 | | - 'Content-Type': 'text/plain' |
| 11 | + const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64') |
| 12 | + |
| 13 | + let server |
| 14 | + before(async () => { |
| 15 | + server = createServer({ |
| 16 | + noDelay: true |
| 17 | + }, (req, res) => { |
| 18 | + res.socket.setNoDelay(true) |
| 19 | + if ( |
| 20 | + req.headers['accept-encoding'] === 'deflate, gzip' || |
| 21 | + req.headers['accept-encoding'] === 'DeFlAtE, GzIp' |
| 22 | + ) { |
| 23 | + res.writeHead(200, |
| 24 | + { |
| 25 | + 'Content-Encoding': 'deflate, gzip', |
| 26 | + 'Content-Type': 'text/plain' |
| 27 | + } |
| 28 | + ) |
| 29 | + res.flushHeaders() |
| 30 | + res.end(gzipDeflateText) |
| 31 | + } else if (req.headers['accept-encoding'] === 'zstd') { |
| 32 | + res.writeHead(200, |
| 33 | + { |
| 34 | + 'Content-Encoding': 'zstd', |
| 35 | + 'Content-Type': 'text/plain' |
| 36 | + } |
| 37 | + ) |
| 38 | + res.flushHeaders() |
| 39 | + res.end(zstdText) |
| 40 | + } else { |
| 41 | + res.writeHead(200, |
| 42 | + { |
| 43 | + 'Content-Type': 'text/plain' |
| 44 | + } |
| 45 | + ) |
| 46 | + res.flushHeaders() |
| 47 | + res.end('Hello, World!') |
21 | 48 | } |
22 | | - ) |
23 | | - .end(gzipDeflateText) |
| 49 | + }) |
| 50 | + await once(server.listen(0), 'listening') |
24 | 51 | }) |
25 | | - await once(server.listen(0), 'listening') |
26 | | - |
27 | | - const response = await fetch(`http://localhost:${server.address().port}`) |
28 | | - |
29 | | - strictEqual(response.headers.get('content-encoding'), contentEncoding) |
30 | | - strictEqual(await response.text(), text) |
31 | | - |
32 | | - await t.completed |
33 | | - server.close() |
34 | | -}) |
35 | 52 |
|
36 | | -test('content-encoding header is case-iNsENsITIve', async (t) => { |
37 | | - const { strictEqual } = tspl(t, { plan: 2 }) |
38 | | - |
39 | | - const contentEncoding = 'DeFlAtE, GzIp' |
40 | | - const text = 'Hello, World!' |
41 | | - const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64') |
42 | | - |
43 | | - const server = createServer((req, res) => { |
44 | | - res.writeHead(200, |
45 | | - { |
46 | | - 'Content-Encoding': contentEncoding, |
47 | | - 'Content-Type': 'text/plain' |
48 | | - } |
49 | | - ) |
50 | | - .end(gzipDeflateText) |
| 53 | + after(() => { |
| 54 | + server.close() |
51 | 55 | }) |
52 | 56 |
|
53 | | - await once(server.listen(0), 'listening') |
| 57 | + test('content-encoding header', async (t) => { |
| 58 | + const { strictEqual } = tspl(t, { plan: 3 }) |
54 | 59 |
|
55 | | - const response = await fetch(`http://localhost:${server.address().port}`) |
| 60 | + const response = await fetch(`http://localhost:${server.address().port}`, { |
| 61 | + keepalive: false, |
| 62 | + headers: { 'accept-encoding': 'deflate, gzip' } |
| 63 | + }) |
56 | 64 |
|
57 | | - strictEqual(response.headers.get('content-encoding'), contentEncoding) |
58 | | - strictEqual(await response.text(), text) |
| 65 | + strictEqual(response.headers.get('content-encoding'), 'deflate, gzip') |
| 66 | + strictEqual(response.headers.get('content-type'), 'text/plain') |
| 67 | + strictEqual(await response.text(), 'Hello, World!') |
59 | 68 |
|
60 | | - await t.completed |
61 | | - server.close() |
62 | | -}) |
| 69 | + await t.completed |
| 70 | + }) |
63 | 71 |
|
64 | | -test('should decompress zstandard response', |
65 | | - { skip: typeof require('node:zlib').createZstdDecompress !== 'function' }, |
66 | | - async (t) => { |
| 72 | + test('content-encoding header is case-iNsENsITIve', async (t) => { |
67 | 73 | const { strictEqual } = tspl(t, { plan: 3 }) |
68 | 74 |
|
69 | | - const contentEncoding = 'zstd' |
70 | | - const text = 'Hello, World!' |
71 | | - const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64') |
72 | | - |
73 | | - const server = createServer((req, res) => { |
74 | | - res.writeHead(200, |
75 | | - { |
76 | | - 'Content-Encoding': contentEncoding, |
77 | | - 'Content-Type': 'text/plain' |
78 | | - }) |
79 | | - .end(zstdText) |
| 75 | + const response = await fetch(`http://localhost:${server.address().port}`, { |
| 76 | + keepalive: false, |
| 77 | + headers: { 'accept-encoding': 'DeFlAtE, GzIp' } |
80 | 78 | }) |
81 | 79 |
|
82 | | - await once(server.listen(0), 'listening') |
83 | | - |
84 | | - const url = `http://localhost:${server.address().port}` |
85 | | - |
86 | | - const response = await fetch(url) |
87 | | - strictEqual(await response.text(), text) |
88 | | - strictEqual(response.headers.get('content-encoding'), contentEncoding) |
| 80 | + strictEqual(response.headers.get('content-encoding'), 'deflate, gzip') |
89 | 81 | strictEqual(response.headers.get('content-type'), 'text/plain') |
| 82 | + strictEqual(await response.text(), 'Hello, World!') |
90 | 83 |
|
91 | 84 | await t.completed |
92 | | - server.close() |
93 | 85 | }) |
| 86 | + |
| 87 | + test('should decompress zstandard response', |
| 88 | + { skip: typeof require('node:zlib').createZstdDecompress !== 'function' }, |
| 89 | + async (t) => { |
| 90 | + const { strictEqual } = tspl(t, { plan: 3 }) |
| 91 | + |
| 92 | + const response = await fetch(`http://localhost:${server.address().port}`, { |
| 93 | + keepalive: false, |
| 94 | + headers: { 'accept-encoding': 'zstd' } |
| 95 | + }) |
| 96 | + |
| 97 | + strictEqual(response.headers.get('content-encoding'), 'zstd') |
| 98 | + strictEqual(response.headers.get('content-type'), 'text/plain') |
| 99 | + strictEqual(await response.text(), 'Hello, World!') |
| 100 | + |
| 101 | + await t.completed |
| 102 | + }) |
| 103 | +}) |
0 commit comments