Skip to content

Commit 89f9a52

Browse files
authored
test: another try to fix flaky macos and node 20 (#4490)
* test: fix flaky encoding test on macos and node 20 * ? * avoid after
1 parent 5ceaf61 commit 89f9a52

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

test/fetch/encoding.js

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,93 @@
11
'use strict'
22

3-
const { test } = require('node:test')
4-
const assert = require('node:assert')
5-
const { createServer } = require('node:http')
63
const { once } = require('node:events')
4+
const { createServer } = require('node:http')
5+
const { test } = require('node:test')
6+
const { tspl } = require('@matteo.collina/tspl')
77
const { fetch } = require('../..')
8-
const zlib = require('node:zlib')
9-
const { closeServerAsPromise } = require('../utils/node-http')
108

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 })
1211

13-
test('content-encoding header is case-iNsENsITIve', { skip }, async (t) => {
14-
const contentCodings = 'GZiP, bR'
12+
const contentEncoding = 'deflate, gzip'
1513
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')
2415

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')
2726

2827
const response = await fetch(`http://localhost:${server.address().port}`)
2928

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)
3231

3332
await t.completed
33+
server.close()
3434
})
3535

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'
3840
const text = 'Hello, World!'
3941
const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64')
4042

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+
})
4752

48-
t.after(closeServerAsPromise(server))
49-
await once(server, 'listening')
53+
await once(server.listen(0), 'listening')
5054

5155
const response = await fetch(`http://localhost:${server.address().port}`)
5256

53-
assert.strictEqual(await response.text(), text)
57+
strictEqual(response.headers.get('content-encoding'), contentEncoding)
58+
strictEqual(await response.text(), text)
5459

5560
await t.completed
61+
server.close()
5662
})
5763

5864
test('should decompress zstandard response',
59-
{ skip: typeof zlib.createZstdDecompress !== 'function' },
65+
{ skip: typeof require('node:zlib').createZstdDecompress !== 'function' },
6066
async (t) => {
61-
const contentCodings = 'zstd'
67+
const { strictEqual } = tspl(t, { plan: 3 })
68+
69+
const contentEncoding = 'zstd'
6270
const text = 'Hello, World!'
6371
const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64')
6472

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')
7383

74-
await once(server, 'listening')
7584
const url = `http://localhost:${server.address().port}`
7685

7786
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')
8190

8291
await t.completed
92+
server.close()
8393
})

0 commit comments

Comments
 (0)