|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const { test } = require('node:test') |
4 |
| -const assert = require('node:assert') |
5 |
| -const { createServer } = require('node:http') |
6 | 3 | const { once } = require('node:events')
|
| 4 | +const { createServer } = require('node:http') |
| 5 | +const { test } = require('node:test') |
| 6 | +const { tspl } = require('@matteo.collina/tspl') |
7 | 7 | const { fetch } = require('../..')
|
8 |
| -const zlib = require('node:zlib') |
9 |
| -const { closeServerAsPromise } = require('../utils/node-http') |
10 | 8 |
|
11 |
| -const skip = process.versions.node.split('.').map(Number)[0] === 20 && process.platform === 'darwin' |
| 9 | +test('content-encoding header', async (t) => { |
| 10 | + const { strictEqual } = tspl(t, { plan: 2 }) |
12 | 11 |
|
13 |
| -test('content-encoding header is case-iNsENsITIve', { skip }, async (t) => { |
14 |
| - const contentCodings = 'GZiP, bR' |
| 12 | + const contentEncoding = 'deflate, gzip' |
15 | 13 | const text = 'Hello, World!'
|
16 |
| - const gzipBrotliText = Buffer.from('CxCAH4sIAAAAAAAAA/NIzcnJ11EIzy/KSVEEANDDSuwNAAAAAw==', 'base64') |
17 |
| - |
18 |
| - const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
19 |
| - res.setHeader('Content-Encoding', contentCodings) |
20 |
| - res.setHeader('Content-Type', 'text/plain') |
21 |
| - res.write(gzipBrotliText) |
22 |
| - res.end() |
23 |
| - }).listen(0) |
| 14 | + const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64') |
24 | 15 |
|
25 |
| - t.after(closeServerAsPromise(server)) |
26 |
| - await once(server, 'listening') |
| 16 | + const server = createServer((req, res) => { |
| 17 | + res.writeHead(200, |
| 18 | + { |
| 19 | + 'Content-Encoding': contentEncoding, |
| 20 | + 'Content-Type': 'text/plain' |
| 21 | + } |
| 22 | + ) |
| 23 | + .end(gzipDeflateText) |
| 24 | + }) |
| 25 | + await once(server.listen(0), 'listening') |
27 | 26 |
|
28 | 27 | const response = await fetch(`http://localhost:${server.address().port}`)
|
29 | 28 |
|
30 |
| - assert.strictEqual(await response.text(), text) |
31 |
| - assert.strictEqual(response.headers.get('content-encoding'), contentCodings) |
| 29 | + strictEqual(response.headers.get('content-encoding'), contentEncoding) |
| 30 | + strictEqual(await response.text(), text) |
32 | 31 |
|
33 | 32 | await t.completed
|
| 33 | + server.close() |
34 | 34 | })
|
35 | 35 |
|
36 |
| -test('response decompression according to content-encoding should be handled in a correct order', async (t) => { |
37 |
| - const contentCodings = 'deflate, gzip' |
| 36 | +test('content-encoding header is case-iNsENsITIve', async (t) => { |
| 37 | + const { strictEqual } = tspl(t, { plan: 2 }) |
| 38 | + |
| 39 | + const contentEncoding = 'DeFlAtE, GzIp' |
38 | 40 | const text = 'Hello, World!'
|
39 | 41 | const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64')
|
40 | 42 |
|
41 |
| - const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
42 |
| - res.setHeader('Content-Encoding', contentCodings) |
43 |
| - res.setHeader('Content-Type', 'text/plain') |
44 |
| - res.write(gzipDeflateText) |
45 |
| - res.end() |
46 |
| - }).listen(0) |
| 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) |
| 51 | + }) |
47 | 52 |
|
48 |
| - t.after(closeServerAsPromise(server)) |
49 |
| - await once(server, 'listening') |
| 53 | + await once(server.listen(0), 'listening') |
50 | 54 |
|
51 | 55 | const response = await fetch(`http://localhost:${server.address().port}`)
|
52 | 56 |
|
53 |
| - assert.strictEqual(await response.text(), text) |
| 57 | + strictEqual(response.headers.get('content-encoding'), contentEncoding) |
| 58 | + strictEqual(await response.text(), text) |
54 | 59 |
|
55 | 60 | await t.completed
|
| 61 | + server.close() |
56 | 62 | })
|
57 | 63 |
|
58 | 64 | test('should decompress zstandard response',
|
59 |
| - { skip: typeof zlib.createZstdDecompress !== 'function' }, |
| 65 | + { skip: typeof require('node:zlib').createZstdDecompress !== 'function' }, |
60 | 66 | async (t) => {
|
61 |
| - const contentCodings = 'zstd' |
| 67 | + const { strictEqual } = tspl(t, { plan: 3 }) |
| 68 | + |
| 69 | + const contentEncoding = 'zstd' |
62 | 70 | const text = 'Hello, World!'
|
63 | 71 | const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64')
|
64 | 72 |
|
65 |
| - const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
66 |
| - res.setHeader('Content-Encoding', contentCodings) |
67 |
| - res.setHeader('Content-Type', 'text/plain') |
68 |
| - res.write(zstdText) |
69 |
| - res.end() |
70 |
| - } |
71 |
| - ).listen(0) |
72 |
| - t.after(closeServerAsPromise(server)) |
| 73 | + const server = createServer((req, res) => { |
| 74 | + res.writeHead(200, |
| 75 | + { |
| 76 | + 'Content-Encoding': contentEncoding, |
| 77 | + 'Content-Type': 'text/plain' |
| 78 | + }) |
| 79 | + .end(zstdText) |
| 80 | + }) |
| 81 | + |
| 82 | + await once(server.listen(0), 'listening') |
73 | 83 |
|
74 |
| - await once(server, 'listening') |
75 | 84 | const url = `http://localhost:${server.address().port}`
|
76 | 85 |
|
77 | 86 | const response = await fetch(url)
|
78 |
| - assert.strictEqual(await response.text(), text) |
79 |
| - assert.strictEqual(response.headers.get('content-encoding'), contentCodings) |
80 |
| - assert.strictEqual(response.headers.get('content-type'), 'text/plain') |
| 87 | + strictEqual(await response.text(), text) |
| 88 | + strictEqual(response.headers.get('content-encoding'), contentEncoding) |
| 89 | + strictEqual(response.headers.get('content-type'), 'text/plain') |
81 | 90 |
|
82 | 91 | await t.completed
|
| 92 | + server.close() |
83 | 93 | })
|
0 commit comments