forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add support for
integrity
option to Fetch (nodejs#1596)
* Test case for fetch() integrity option. * Implement matchRequestIntegrity * Add test to cover encoded body * Speed up integrity test completion * Fix trailing spaces
- Loading branch information
Showing
3 changed files
with
77 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { createServer } = require('http') | ||
const { createHash } = require('crypto') | ||
const { gzipSync } = require('zlib') | ||
const { fetch, setGlobalDispatcher, Agent } = require('../..') | ||
|
||
setGlobalDispatcher(new Agent({ | ||
keepAliveTimeout: 1, | ||
keepAliveMaxTimeout: 1 | ||
})) | ||
|
||
test('request with correct integrity checksum', (t) => { | ||
const body = 'Hello world!' | ||
const hash = createHash('sha256').update(body).digest('hex') | ||
|
||
const server = createServer((req, res) => { | ||
res.end(body) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
server.listen(0, async () => { | ||
const response = await fetch(`http://localhost:${server.address().port}`, { | ||
integrity: `sha256-${hash}` | ||
}) | ||
t.strictSame(body, await response.text()) | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('request with wrong integrity checksum', (t) => { | ||
const body = 'Hello world!' | ||
const hash = 'c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b' | ||
|
||
const server = createServer((req, res) => { | ||
res.end(body) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
server.listen(0, () => { | ||
fetch(`http://localhost:${server.address().port}`, { | ||
integrity: `sha256-${hash}` | ||
}).then(response => { | ||
t.fail('fetch did not fail') | ||
}).catch((err) => { | ||
t.equal(err.cause.message, 'integrity mismatch') | ||
}).finally(() => { | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
|
||
test('request with integrity checksum on encoded body', (t) => { | ||
const body = 'Hello world!' | ||
const hash = createHash('sha256').update(body).digest('hex') | ||
|
||
const server = createServer((req, res) => { | ||
res.setHeader('content-encoding', 'gzip') | ||
res.end(gzipSync(body)) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
server.listen(0, async () => { | ||
const response = await fetch(`http://localhost:${server.address().port}`, { | ||
integrity: `sha256-${hash}` | ||
}) | ||
t.strictSame(body, await response.text()) | ||
t.end() | ||
}) | ||
}) |