Skip to content

Commit 6bfa4e1

Browse files
fix(fetch): resolve relative urls against location.href, not location.origin (#717)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent b722a5a commit 6bfa4e1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/interceptors/fetch/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
4545
typeof input === 'string' &&
4646
typeof location !== 'undefined' &&
4747
!canParseUrl(input)
48-
? new URL(input, location.origin)
48+
? new URL(
49+
input,
50+
location.href
51+
)
4952
: input
5053

5154
const request = new Request(resolvedInput, init)

test/modules/fetch/compliance/fetch-response-url.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,22 @@ it('returns the last response url in case of redirects', async () => {
8787
expect(response.url).toBe('http://localhost/destination')
8888
await expect(response.text()).resolves.toBe('hello world')
8989
})
90+
91+
it('resolves relative URLs against location', async () => {
92+
interceptor.on('request', ({ controller }) => {
93+
controller.respondWith(new Response('Hello world'))
94+
})
95+
96+
const originalLocation = global.location
97+
Object.defineProperty(global, 'location', {
98+
value: new URL('http://localhost/path/'),
99+
configurable: true,
100+
})
101+
102+
const response = await fetch('?x=y')
103+
expect(response.url).toBe('http://localhost/path/?x=y')
104+
Object.defineProperty(global, 'location', {
105+
value: originalLocation,
106+
configurable: true,
107+
})
108+
})

0 commit comments

Comments
 (0)