-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdca909
commit 172bc10
Showing
6 changed files
with
169 additions
and
18 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
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
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
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,12 @@ | ||
import { setupWorker, rest } from 'msw' | ||
|
||
const worker = setupWorker( | ||
rest.all('*/api/*', (req, res, ctx) => { | ||
return res(ctx.text('hello world')) | ||
}), | ||
rest.all('*', (req, res, ctx) => { | ||
return res(ctx.text('welcome to the jungle')) | ||
}), | ||
) | ||
|
||
worker.start() |
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,92 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import fetch, { Response } from 'node-fetch' | ||
import { createServer, ServerApi } from '@open-draft/test-server' | ||
import { RESTMethods, rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
let httpServer: ServerApi | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(async () => { | ||
httpServer = await createServer((app) => { | ||
// Responding with "204 No Content" because the "OPTIONS" | ||
// request returns 204 without an obvious way to override that. | ||
app.all('*', (req, res) => res.status(204).end()) | ||
}) | ||
server.listen({ | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(async () => { | ||
server.close() | ||
await httpServer.close() | ||
}) | ||
|
||
async function forEachMethod(callback: (method: RESTMethods) => unknown) { | ||
for (const method of Object.values(RESTMethods)) { | ||
await callback(method) | ||
} | ||
} | ||
|
||
test('matches all requests given no custom path', async () => { | ||
server.use( | ||
rest.all('*', (req, res, ctx) => { | ||
return res(ctx.text('welcome to the jungle')) | ||
}), | ||
) | ||
|
||
const responses = await Promise.all( | ||
Object.values(RESTMethods).reduce<Promise<Response>[]>((all, method) => { | ||
return all.concat( | ||
[ | ||
httpServer.http.makeUrl('/'), | ||
httpServer.http.makeUrl('/foo'), | ||
'https://example.com', | ||
].map((url) => fetch(url, { method })), | ||
) | ||
}, []), | ||
) | ||
|
||
for (const response of responses) { | ||
expect(response.status).toEqual(200) | ||
expect(await response.text()).toEqual('welcome to the jungle') | ||
} | ||
}) | ||
|
||
test('respects custom path when matching requests', async () => { | ||
server.use( | ||
rest.all(httpServer.http.makeUrl('/api/*'), (req, res, ctx) => { | ||
return res(ctx.text('hello world')) | ||
}), | ||
) | ||
|
||
// Root requests. | ||
await forEachMethod(async (method) => { | ||
const response = await fetch(httpServer.http.makeUrl('/api/'), { method }) | ||
expect(response.status).toEqual(200) | ||
expect(await response.text()).toEqual('hello world') | ||
}) | ||
|
||
// Nested requests. | ||
await forEachMethod(async (method) => { | ||
const response = await fetch(httpServer.http.makeUrl('/api/foo'), { | ||
method, | ||
}) | ||
expect(response.status).toEqual(200) | ||
expect(await response.text()).toEqual('hello world') | ||
}) | ||
|
||
// Mismatched requests. | ||
await forEachMethod(async (method) => { | ||
const response = await fetch(httpServer.http.makeUrl('/foo'), { method }) | ||
expect(response.status).toEqual(204) | ||
}) | ||
}) |
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,60 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import * as path from 'path' | ||
import { ScenarioApi, pageWith } from 'page-with' | ||
import { Response } from 'playwright' | ||
|
||
function createRuntime() { | ||
return pageWith({ | ||
example: path.resolve(__dirname, 'all.mocks.ts'), | ||
}) | ||
} | ||
|
||
function requestAllMethods( | ||
runtime: ScenarioApi, | ||
url: string, | ||
): Promise<Response[]> { | ||
return Promise.all( | ||
['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'].map((method) => | ||
runtime.request(url, { method }), | ||
), | ||
) | ||
} | ||
|
||
test('respects custom path when matching requests', async () => { | ||
const runtime = await createRuntime() | ||
|
||
// Root request. | ||
const rootResponses = await requestAllMethods( | ||
runtime, | ||
'http://localhost/api/', | ||
) | ||
|
||
for (const response of rootResponses) { | ||
expect(response.status()).toEqual(200) | ||
expect(await response.text()).toEqual('hello world') | ||
} | ||
|
||
// Nested request. | ||
const nestedResponses = await requestAllMethods( | ||
runtime, | ||
'http://localhost/api/user', | ||
) | ||
for (const response of nestedResponses) { | ||
expect(response.status()).toEqual(200) | ||
expect(await response.text()).toEqual('hello world') | ||
} | ||
|
||
// Mismatched request. | ||
// There's a fallback "rest.all()" in this test that acts | ||
// as a fallback request handler for all otherwise mismatched requests. | ||
const mismatchedResponses = await requestAllMethods( | ||
runtime, | ||
'http://localhost/foo', | ||
) | ||
for (const response of mismatchedResponses) { | ||
expect(response.status()).toEqual(200) | ||
expect(await response.text()).toEqual('welcome to the jungle') | ||
} | ||
}) |