Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 66 additions & 68 deletions test/fetch/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,90 @@

const { once } = require('node:events')
const { createServer } = require('node:http')
const { test } = require('node:test')
const { test, before, after, describe } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const { fetch } = require('../..')

test('content-encoding header', async (t) => {
const { strictEqual } = tspl(t, { plan: 2 })

const contentEncoding = 'deflate, gzip'
const text = 'Hello, World!'
describe('content-encoding handling', () => {
const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64')

const server = createServer((req, res) => {
res.writeHead(200,
{
'Content-Encoding': contentEncoding,
'Content-Type': 'text/plain'
const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64')

let server
before(async () => {
server = createServer((req, res) => {
if (req.headers['accept-encoding'].toLowerCase() === 'deflate, gzip') {
res.writeHead(200,
{
'Content-Encoding': 'deflate, gzip',
'Content-Type': 'text/plain'
}
)
.end(gzipDeflateText)
} else if (req.headers['accept-encoding'] === 'zstd') {
res.writeHead(200,
{
'Content-Encoding': 'zstd',
'Content-Type': 'text/plain'
}
)
.end(zstdText)
} else {
res.writeHead(200,
{
'Content-Type': 'text/plain'
}
)
.end('Hello, World!')
}
)
.end(gzipDeflateText)
})
await once(server.listen(0), 'listening')
})
await once(server.listen(0), 'listening')

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

strictEqual(response.headers.get('content-encoding'), contentEncoding)
strictEqual(await response.text(), text)

await t.completed
server.close()
})

test('content-encoding header is case-iNsENsITIve', async (t) => {
const { strictEqual } = tspl(t, { plan: 2 })

const contentEncoding = 'DeFlAtE, GzIp'
const text = 'Hello, World!'
const gzipDeflateText = Buffer.from('H4sIAAAAAAAAA6uY89nj7MmT1wM5zuuf8gxkYZCfx5IFACQ8u/wVAAAA', 'base64')

const server = createServer((req, res) => {
res.writeHead(200,
{
'Content-Encoding': contentEncoding,
'Content-Type': 'text/plain'
}
)
.end(gzipDeflateText)
after(() => {
server.close()
})

await once(server.listen(0), 'listening')
test('content-encoding header', async (t) => {
const { strictEqual } = tspl(t, { plan: 3 })

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

Check failure on line 51 in test/fetch/encoding.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

content-encoding header

TypeError [Error]: fetch failed at /Users/runner/work/undici/undici/index.js:124:13 at async TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/encoding.js:51:22) at async Test.run (node:internal/test_runner/test:797:9) at async Promise.all (index 0) at async Suite.run (node:internal/test_runner/test:1135:7) at async Test.processPendingSubtests (node:internal/test_runner/test:526:7) { [cause]: Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:218:20) { errno: -54, code: 'ECONNRESET', syscall: 'read' } }
headers: { 'accept-encoding': 'deflate, gzip' }
})

strictEqual(response.headers.get('content-encoding'), contentEncoding)
strictEqual(await response.text(), text)
strictEqual(response.headers.get('content-encoding'), 'deflate, gzip')
strictEqual(response.headers.get('content-type'), 'text/plain')
strictEqual(await response.text(), 'Hello, World!')

await t.completed
server.close()
})
await t.completed
})

test('should decompress zstandard response',
{ skip: typeof require('node:zlib').createZstdDecompress !== 'function' },
async (t) => {
test('content-encoding header is case-iNsENsITIve', async (t) => {
const { strictEqual } = tspl(t, { plan: 3 })

const contentEncoding = 'zstd'
const text = 'Hello, World!'
const zstdText = Buffer.from('KLUv/QBYaQAASGVsbG8sIFdvcmxkIQ==', 'base64')

const server = createServer((req, res) => {
res.writeHead(200,
{
'Content-Encoding': contentEncoding,
'Content-Type': 'text/plain'
})
.end(zstdText)
const response = await fetch(`http://localhost:${server.address().port}`, {
headers: { 'accept-encoding': 'DeFlAtE, GzIp' }
})

await once(server.listen(0), 'listening')

const url = `http://localhost:${server.address().port}`

const response = await fetch(url)
strictEqual(await response.text(), text)
strictEqual(response.headers.get('content-encoding'), contentEncoding)
strictEqual(response.headers.get('content-encoding'), 'deflate, gzip')
strictEqual(response.headers.get('content-type'), 'text/plain')
strictEqual(await response.text(), 'Hello, World!')

await t.completed
server.close()
})

test('should decompress zstandard response',
{ skip: typeof require('node:zlib').createZstdDecompress !== 'function' },
async (t) => {
const { strictEqual } = tspl(t, { plan: 3 })

const response = await fetch(`http://localhost:${server.address().port}`, {
headers: { 'accept-encoding': 'zstd' }
})

strictEqual(response.headers.get('content-encoding'), 'zstd')
strictEqual(response.headers.get('content-type'), 'text/plain')
strictEqual(await response.text(), 'Hello, World!')

await t.completed
})
})
2 changes: 1 addition & 1 deletion test/fetch/exiting.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { createServer } = require('node:http')
const { closeServerAsPromise } = require('../utils/node-http')
const tspl = require('@matteo.collina/tspl')

test('abort the request on the other side if the stream is canceled', async (t) => {
test('abort the request on the other side if the stream is canceled', { skip: true }, async (t) => {
const p = tspl(t, { plan: 1 })
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.writeHead(200)
Expand Down
Loading