Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/interceptors/fetch/utils/followRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ export async function followFetchRedirect(
*/

requestInit.headers = request.headers
return fetch(new Request(locationUrl, requestInit))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that fetch doesn't set that property based on the location header. I suppose the reason is because it's the end response that's marked as redirected?

@erikshestopal did you by any chance have a reference to the spec that describes .redirected?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going off the undici implementation which sets the url list but for mockiing I opted for a simpler approach.

https://github.com/search?q=repo%3Anodejs%2Fundici+redirected&type=code

const finalResponse = await fetch(new Request(locationUrl, requestInit))
Object.defineProperty(finalResponse, 'redirected', {
value: true,
configurable: true,
})

return finalResponse
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/modules/fetch/compliance/fetch-follow-redirects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ it('follows a bypassed redirect response', async () => {
const response = await fetch(httpServer.http.url('/original'))

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

Expand All @@ -45,6 +46,7 @@ it('follows a mocked redirect to the original server', async () => {
const response = await fetch(httpServer.http.url('/original'))

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

Expand All @@ -60,6 +62,7 @@ it('follows a mocked relative redirect to the original server', async () => {
const response = await fetch(httpServer.http.url('/original'))

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

Expand All @@ -79,6 +82,7 @@ it('follows a mocked redirect to a mocked response', async () => {
const response = await fetch(httpServer.http.url('/original'))

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

Expand All @@ -96,6 +100,7 @@ it('returns the redirect response as-is for a request with "manual" redirect mod
})

expect(response.status).toBe(301)
expect(response.redirected).toBe(false)
expect(response.headers.get('location')).toBe(
httpServer.http.url('/redirected')
)
Expand Down