diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
commit | 0b6210cd37b68b94252cb798598b12974a20e1c1 (patch) | |
tree | e371686554a877842d95aa94f100bee552ff2a8e /test/fetch/relative-url.js | |
parent | Initial commit. (diff) | |
download | node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.zip |
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/fetch/relative-url.js')
-rw-r--r-- | test/fetch/relative-url.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/test/fetch/relative-url.js b/test/fetch/relative-url.js new file mode 100644 index 0000000..1a4f819 --- /dev/null +++ b/test/fetch/relative-url.js @@ -0,0 +1,110 @@ +'use strict' + +const { test, afterEach } = require('tap') +const { createServer } = require('http') +const { once } = require('events') +const { + getGlobalOrigin, + setGlobalOrigin, + Response, + Request, + fetch +} = require('../..') + +afterEach(() => setGlobalOrigin(undefined)) + +test('setGlobalOrigin & getGlobalOrigin', (t) => { + t.equal(getGlobalOrigin(), undefined) + + setGlobalOrigin('http://localhost:3000') + t.same(getGlobalOrigin(), new URL('http://localhost:3000')) + + setGlobalOrigin(undefined) + t.equal(getGlobalOrigin(), undefined) + + setGlobalOrigin(new URL('http://localhost:3000')) + t.same(getGlobalOrigin(), new URL('http://localhost:3000')) + + t.throws(() => { + setGlobalOrigin('invalid.url') + }, TypeError) + + t.throws(() => { + setGlobalOrigin('wss://invalid.protocol') + }, TypeError) + + t.throws(() => setGlobalOrigin(true)) + + t.end() +}) + +test('Response.redirect', (t) => { + t.throws(() => { + Response.redirect('/relative/path', 302) + }, TypeError('Failed to parse URL from /relative/path')) + + t.doesNotThrow(() => { + setGlobalOrigin('http://localhost:3000') + Response.redirect('/relative/path', 302) + }) + + setGlobalOrigin('http://localhost:3000') + const response = Response.redirect('/relative/path', 302) + // See step #7 of https://fetch.spec.whatwg.org/#dom-response-redirect + t.equal(response.headers.get('location'), 'http://localhost:3000/relative/path') + + t.end() +}) + +test('new Request', (t) => { + t.throws( + () => new Request('/relative/path'), + TypeError('Failed to parse URL from /relative/path') + ) + + t.doesNotThrow(() => { + setGlobalOrigin('http://localhost:3000') + // eslint-disable-next-line no-new + new Request('/relative/path') + }) + + setGlobalOrigin('http://localhost:3000') + const request = new Request('/relative/path') + t.equal(request.url, 'http://localhost:3000/relative/path') + + t.end() +}) + +test('fetch', async (t) => { + await t.rejects(async () => { + await fetch('/relative/path') + }, TypeError('Failed to parse URL from /relative/path')) + + t.test('Basic fetch', async (t) => { + const server = createServer((req, res) => { + t.equal(req.url, '/relative/path') + res.end() + }).listen(0) + + setGlobalOrigin(`http://localhost:${server.address().port}`) + t.teardown(server.close.bind(server)) + await once(server, 'listening') + + await t.resolves(fetch('/relative/path')) + }) + + t.test('fetch return', async (t) => { + const server = createServer((req, res) => { + t.equal(req.url, '/relative/path') + res.end() + }).listen(0) + + setGlobalOrigin(`http://localhost:${server.address().port}`) + t.teardown(server.close.bind(server)) + await once(server, 'listening') + + const response = await fetch('/relative/path') + + t.equal(response.url, `http://localhost:${server.address().port}/relative/path`) + }) +}) |