|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test, afterEach } = require('tap') |
| 4 | +const { createServer } = require('http') |
| 5 | +const { once } = require('events') |
| 6 | +const { |
| 7 | + getGlobalOrigin, |
| 8 | + setGlobalOrigin, |
| 9 | + Response, |
| 10 | + Request, |
| 11 | + fetch |
| 12 | +} = require('../..') |
| 13 | + |
| 14 | +afterEach(() => setGlobalOrigin(undefined)) |
| 15 | + |
| 16 | +test('setGlobalOrigin & getGlobalOrigin', (t) => { |
| 17 | + t.equal(getGlobalOrigin(), undefined) |
| 18 | + |
| 19 | + setGlobalOrigin('http://localhost:3000') |
| 20 | + t.same(getGlobalOrigin(), new URL('http://localhost:3000')) |
| 21 | + |
| 22 | + setGlobalOrigin(undefined) |
| 23 | + t.equal(getGlobalOrigin(), undefined) |
| 24 | + |
| 25 | + setGlobalOrigin(new URL('http://localhost:3000')) |
| 26 | + t.same(getGlobalOrigin(), new URL('http://localhost:3000')) |
| 27 | + |
| 28 | + t.throws(() => { |
| 29 | + setGlobalOrigin('invalid.url') |
| 30 | + }, TypeError) |
| 31 | + |
| 32 | + t.throws(() => { |
| 33 | + setGlobalOrigin('wss://invalid.protocol') |
| 34 | + }, TypeError) |
| 35 | + |
| 36 | + t.throws(() => setGlobalOrigin(true)) |
| 37 | + |
| 38 | + t.end() |
| 39 | +}) |
| 40 | + |
| 41 | +test('Response.redirect', (t) => { |
| 42 | + t.throws(() => { |
| 43 | + Response.redirect('/relative/path', 302) |
| 44 | + }, TypeError('Failed to parse URL from /relative/path')) |
| 45 | + |
| 46 | + t.doesNotThrow(() => { |
| 47 | + setGlobalOrigin('http://localhost:3000') |
| 48 | + Response.redirect('/relative/path', 302) |
| 49 | + }) |
| 50 | + |
| 51 | + setGlobalOrigin('http://localhost:3000') |
| 52 | + const response = Response.redirect('/relative/path', 302) |
| 53 | + // See step #7 of https://fetch.spec.whatwg.org/#dom-response-redirect |
| 54 | + t.equal(response.headers.get('location'), 'http://localhost:3000/relative/path') |
| 55 | + |
| 56 | + t.end() |
| 57 | +}) |
| 58 | + |
| 59 | +test('new Request', (t) => { |
| 60 | + t.throws( |
| 61 | + () => new Request('/relative/path'), |
| 62 | + TypeError('Failed to parse URL from /relative/path') |
| 63 | + ) |
| 64 | + |
| 65 | + t.doesNotThrow(() => { |
| 66 | + setGlobalOrigin('http://localhost:3000') |
| 67 | + // eslint-disable-next-line no-new |
| 68 | + new Request('/relative/path') |
| 69 | + }) |
| 70 | + |
| 71 | + setGlobalOrigin('http://localhost:3000') |
| 72 | + const request = new Request('/relative/path') |
| 73 | + t.equal(request.url, 'http://localhost:3000/relative/path') |
| 74 | + |
| 75 | + t.end() |
| 76 | +}) |
| 77 | + |
| 78 | +test('fetch', async (t) => { |
| 79 | + await t.rejects(async () => { |
| 80 | + await fetch('/relative/path') |
| 81 | + }, TypeError('Failed to parse URL from /relative/path')) |
| 82 | + |
| 83 | + t.test('Basic fetch', async (t) => { |
| 84 | + const server = createServer((req, res) => { |
| 85 | + t.equal(req.url, '/relative/path') |
| 86 | + res.end() |
| 87 | + }).listen(0) |
| 88 | + |
| 89 | + setGlobalOrigin(`http://localhost:${server.address().port}`) |
| 90 | + t.teardown(server.close.bind(server)) |
| 91 | + await once(server, 'listening') |
| 92 | + |
| 93 | + await t.resolves(fetch('/relative/path')) |
| 94 | + }) |
| 95 | + |
| 96 | + t.test('fetch return', async (t) => { |
| 97 | + const server = createServer((req, res) => { |
| 98 | + t.equal(req.url, '/relative/path') |
| 99 | + res.end() |
| 100 | + }).listen(0) |
| 101 | + |
| 102 | + setGlobalOrigin(`http://localhost:${server.address().port}`) |
| 103 | + t.teardown(server.close.bind(server)) |
| 104 | + await once(server, 'listening') |
| 105 | + |
| 106 | + const response = await fetch('/relative/path') |
| 107 | + |
| 108 | + t.equal(response.url, `http://localhost:${server.address().port}/relative/path`) |
| 109 | + }) |
| 110 | +}) |
0 commit comments