Skip to content

Commit

Permalink
fix(fetch): rebase relative redirect URL against request.url (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Solomon authored Sep 10, 2024
1 parent d45e951 commit 1161c7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/interceptors/fetch/utils/followRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export async function followFetchRedirect(

let locationUrl: URL
try {
locationUrl = new URL(response.headers.get('location')!)
// If the location is a relative URL, use the request URL as the base URL.
locationUrl = new URL(response.headers.get('location')!, request.url)
} catch (error) {
return Promise.reject(createNetworkError(error))
}
Expand Down
15 changes: 15 additions & 0 deletions test/modules/fetch/compliance/fetch-follow-redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ it('follows a mocked redirect to the original server', async () => {
await expect(response.text()).resolves.toBe('redirected')
})

it('follows a mocked relative redirect to the original server', async () => {
interceptor.on('request', ({ request, controller }) => {
if (request.url.endsWith('/original')) {
return controller.respondWith(
new Response(null, { status: 302, headers: { location: '/redirected' } })
)
}
})

const response = await fetch(httpServer.http.url('/original'))

expect(response.status).toBe(200)
await expect(response.text()).resolves.toBe('redirected')
})

it('follows a mocked redirect to a mocked response', async () => {
interceptor.on('request', ({ request, controller }) => {
if (request.url.endsWith('/original')) {
Expand Down

0 comments on commit 1161c7b

Please sign in to comment.