Skip to content

Commit

Permalink
test: response.url after redirect is set to target url
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 8, 2024
1 parent 7308f53 commit fac3c39
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/fetch/fetch-url-after-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const assert = require('node:assert')
const { test } = require('node:test')
const { createServer } = require('node:http')
const { fetch } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')
const { promisify } = require('node:util')

test('after redirecting the url of the response is set to the target url', async (t) => {
// redirect-1 -> redirect-2 -> target
const server = createServer((req, res) => {
switch (res.req.url) {
case '/redirect-1':
res.writeHead(302, undefined, { Location: '/redirect-2' })
res.end()
break
case '/redirect-2':
res.writeHead(302, undefined, { Location: '/redirect-3' })
res.end()
break
case '/redirect-3':
res.writeHead(302, undefined, { Location: '/target' })
res.end()
break
case '/target':
res.writeHead(200, 'dummy', { 'Content-Type': 'text/plain' })
res.end()
break
}
})
t.after(closeServerAsPromise(server))

const listenAsync = promisify(server.listen.bind(server))
await listenAsync(0)
const { port } = server.address()
const response = await fetch(`http://127.0.0.1:${port}/redirect-1`)

assert.strictEqual(response.url, `http://127.0.0.1:${port}/target`)
})

0 comments on commit fac3c39

Please sign in to comment.