-
-
Notifications
You must be signed in to change notification settings - Fork 758
fetch: process content-encoding header only if relevant #4496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91ce345
perf: process content-encoding header only if relevant
Uzlopak 4987df5
patch test
Uzlopak 2f9feaa
Merge branch 'main' into improve-decoder-logic
Uzlopak ffd98e0
change order of tests and run fetch tests before the cache tests
Uzlopak 8272e82
disable keepAlive
Uzlopak 940f7f2
set keepalive to false
Uzlopak c96b6a2
what now?
Uzlopak d420a03
Merge branch 'main' into improve-decoder-logic
Uzlopak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,92 +2,102 @@ | |||||||
|
|
||||||||
| 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({ | ||||||||
| noDelay: true | ||||||||
| }, (req, res) => { | ||||||||
| res.socket.setNoDelay(true) | ||||||||
| if ( | ||||||||
| req.headers['accept-encoding'] === 'deflate, gzip' || | ||||||||
| req.headers['accept-encoding'] === 'DeFlAtE, GzIp' | ||||||||
|
Comment on lines
+20
to
+21
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| ) { | ||||||||
| res.writeHead(200, | ||||||||
| { | ||||||||
| 'Content-Encoding': 'deflate, gzip', | ||||||||
| 'Content-Type': 'text/plain' | ||||||||
| } | ||||||||
| ) | ||||||||
| res.flushHeaders() | ||||||||
| res.end(gzipDeflateText) | ||||||||
| } else if (req.headers['accept-encoding'] === 'zstd') { | ||||||||
| res.writeHead(200, | ||||||||
| { | ||||||||
| 'Content-Encoding': 'zstd', | ||||||||
| 'Content-Type': 'text/plain' | ||||||||
| } | ||||||||
| ) | ||||||||
| res.flushHeaders() | ||||||||
| res.end(zstdText) | ||||||||
| } else { | ||||||||
| res.writeHead(200, | ||||||||
| { | ||||||||
| 'Content-Type': 'text/plain' | ||||||||
| } | ||||||||
| ) | ||||||||
| res.flushHeaders() | ||||||||
| res.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}`, { | ||||||||
| keepalive: false, | ||||||||
| 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}`, { | ||||||||
| keepalive: false, | ||||||||
| 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}`, { | ||||||||
| keepalive: false, | ||||||||
| 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 | ||||||||
| }) | ||||||||
| }) | ||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this just moving
test:fetchearlier in the order? Does that really matter? i hope the tests are independantThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are, but it makes more sense to test the fetch stuff consequently.