Skip to content

Commit 63d310d

Browse files
committed
test(node-fetch): add test cases for zstandard decompression support
1 parent 1e623e9 commit 63d310d

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

test/node-fetch/main.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,57 @@ describe('node-fetch', () => {
670670
})
671671
})
672672

673+
it('should decompress zstandard response', function () {
674+
if (typeof zlib.createZstdDecompress !== 'function') {
675+
this.skip()
676+
}
677+
678+
const url = `${base}zstandard`
679+
return fetch(url).then(res => {
680+
assert.strictEqual(res.headers.get('content-type'), 'text/plain')
681+
assert.strictEqual(res.headers.get('content-encoding'), 'zstd')
682+
return res.text().then(result => {
683+
assert.strictEqual(typeof result, 'string')
684+
assert.strictEqual(result, 'hello world')
685+
})
686+
})
687+
})
688+
689+
it('should handle not modified response with zstandard encoding', function () {
690+
if (typeof zlib.createZstdDecompress !== 'function') {
691+
this.skip()
692+
}
693+
const url = `${base}not-modified/zstandard`
694+
return fetch(url).then(res => {
695+
assert.strictEqual(res.status, 304)
696+
assert.strictEqual(res.statusText, 'Not Modified')
697+
assert.strictEqual(res.headers.get('content-encoding'), 'zstd')
698+
assert.strictEqual(res.ok, false)
699+
return res.text().then(result => {
700+
assert.strictEqual(typeof result, 'string')
701+
assert.strictEqual(result, '')
702+
})
703+
})
704+
})
705+
706+
it('should handle no content response with zstandard encoding', function () {
707+
if (typeof zlib.createZstdDecompress !== 'function') {
708+
this.skip()
709+
}
710+
711+
const url = `${base}no-content/zstandard`
712+
return fetch(url).then(res => {
713+
assert.strictEqual(res.status, 204)
714+
assert.strictEqual(res.statusText, 'No Content')
715+
assert.strictEqual(res.headers.get('content-encoding'), 'zstd')
716+
assert.strictEqual(res.ok, true)
717+
return res.text().then(result => {
718+
assert.strictEqual(typeof result, 'string')
719+
assert.strictEqual(result, '')
720+
})
721+
})
722+
})
723+
673724
it('should handle no content response with brotli encoding', function () {
674725
if (typeof zlib.createBrotliDecompress !== 'function') {
675726
this.skip()

test/node-fetch/utils/server.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ module.exports = class TestServer {
164164
}
165165
}
166166

167+
if (p === '/zstandard') {
168+
res.statusCode = 200
169+
res.setHeader('Content-Type', 'text/plain')
170+
if (typeof zlib.createZstdDecompress === 'function') {
171+
res.setHeader('Content-Encoding', 'zstd')
172+
zlib.zstdCompress('hello world', (err, buffer) => {
173+
if (err) {
174+
throw err
175+
}
176+
res.end(buffer)
177+
})
178+
}
179+
}
180+
167181
if (p === '/multiunsupported') {
168182
res.statusCode = 200
169183
res.setHeader('Content-Type', 'text/plain')
@@ -406,6 +420,18 @@ module.exports = class TestServer {
406420
res.end()
407421
}
408422

423+
if (p === '/no-content/zstandard') {
424+
res.statusCode = 204
425+
res.setHeader('Content-Encoding', 'zstd')
426+
res.end()
427+
}
428+
429+
if (p === '/not-modified/zstandard') {
430+
res.statusCode = 304
431+
res.setHeader('Content-Encoding', 'zstd')
432+
res.end()
433+
}
434+
409435
if (p === '/not-modified') {
410436
res.statusCode = 304
411437
res.end()

0 commit comments

Comments
 (0)