-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fetch): use native ref to parse request props (#186)
* fix(fetch): use native ref to parse request props * chore(fetch): rename internal ref to request * fix(fetch): use exposed request in pureFetch * test(fetch): add test for "fetch" requests using "Request" as input * test(fetch): add in-browser test for using "Request" as input Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
- Loading branch information
1 parent
8e4a70f
commit 77ce97a
Showing
3 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/modules/fetch/intercept/fetch.request.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
import { createServer, ServerApi } from '@open-draft/test-server' | ||
import { IsomorphicRequest } from '../../../../src/createInterceptor' | ||
import { extractRequestFromPage } from '../../../helpers' | ||
|
||
let httpServer: ServerApi | ||
|
||
beforeAll(async () => { | ||
httpServer = await createServer((app) => { | ||
app.post('/user', (_req, res) => { | ||
res.status(200).send('mocked') | ||
}) | ||
}) | ||
}) | ||
|
||
afterAll(async () => { | ||
httpServer.close() | ||
}) | ||
|
||
test('intercepts fetch requests constructed via a "Request" instance', async () => { | ||
const context = await pageWith({ | ||
example: path.resolve(__dirname, 'fetch.browser.runtime.js'), | ||
}) | ||
const url = httpServer.http.makeUrl('/user') | ||
|
||
const [request] = await Promise.all([ | ||
extractRequestFromPage(context.page), | ||
context.page.evaluate((url) => { | ||
const request = new Request(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'plain/text', | ||
'X-Origin': 'interceptors', | ||
}, | ||
body: 'hello world', | ||
}) | ||
|
||
return fetch(request) | ||
}, url), | ||
]) | ||
|
||
expect(request).toMatchObject<Partial<IsomorphicRequest>>({ | ||
method: 'POST', | ||
url: new URL(url), | ||
body: 'hello world', | ||
credentials: 'same-origin', | ||
}) | ||
expect(request.headers.all()).toMatchObject({ | ||
'content-type': 'plain/text', | ||
'x-origin': 'interceptors', | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import { Request } from 'node-fetch' | ||
import { createServer, ServerApi } from '@open-draft/test-server' | ||
import { createInterceptor, IsomorphicRequest, Resolver } from '../../../../src' | ||
import nodeInterceptors from '../../../../src/presets/node' | ||
import { fetch } from '../../../helpers' | ||
|
||
let httpServer: ServerApi | ||
|
||
const resolver = jest.fn<ReturnType<Resolver>, Parameters<Resolver>>() | ||
const interceptor = createInterceptor({ | ||
modules: nodeInterceptors, | ||
resolver, | ||
}) | ||
|
||
beforeAll(async () => { | ||
httpServer = await createServer((app) => { | ||
app.post('/user', (_req, res) => { | ||
res.status(200).send('mocked') | ||
}) | ||
}) | ||
|
||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.restore() | ||
await httpServer.close() | ||
}) | ||
|
||
test('intercepts fetch requests constructed via a "Request" instance', async () => { | ||
const request = new Request(httpServer.http.makeUrl('/user'), { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'plain/text', | ||
'User-Agent': 'interceptors', | ||
}, | ||
body: 'hello world', | ||
}) | ||
const { res } = await fetch(request) | ||
|
||
// There's no mocked response returned from the resolver | ||
// so this request must hit an actual (test) server. | ||
expect(res.status).toEqual(200) | ||
expect(await res.text()).toEqual('mocked') | ||
|
||
expect(resolver).toHaveBeenCalledTimes(1) | ||
const [capturedRequest] = resolver.mock.calls[0] | ||
|
||
expect(capturedRequest).toMatchObject<Partial<IsomorphicRequest>>({ | ||
method: 'POST', | ||
url: new URL(httpServer.http.makeUrl('/user')), | ||
body: 'hello world', | ||
}) | ||
expect(capturedRequest.headers.all()).toMatchObject({ | ||
'content-type': 'plain/text', | ||
'user-agent': 'interceptors', | ||
}) | ||
}) |