|
1 | 1 | import { setupServer } from 'msw/node'
|
2 |
| -import { headersToObject } from 'headers-polyfill' |
3 |
| -import { HttpResponse, http } from 'msw' |
4 |
| - |
5 |
| -export type Post = { |
6 |
| - id: string |
7 |
| - title: string |
8 |
| - body: string |
9 |
| -} |
10 |
| - |
11 |
| -export const posts: Record<string, Post> = { |
12 |
| - '1': { id: '1', title: 'hello', body: 'extra body!' }, |
13 |
| -} |
14 |
| - |
15 |
| -export const handlers = [ |
16 |
| - http.get( |
17 |
| - 'https://example.com/echo', |
18 |
| - async ({ request, params, cookies, requestId }) => { |
19 |
| - return HttpResponse.json( |
20 |
| - { |
21 |
| - ...request, |
22 |
| - params, |
23 |
| - cookies, |
24 |
| - requestId, |
25 |
| - url: new URL(request.url), |
26 |
| - headers: headersToObject(request.headers), |
27 |
| - }, |
28 |
| - { headers: request.headers } |
29 |
| - ) |
30 |
| - } |
31 |
| - ), |
32 |
| - http.post( |
33 |
| - 'https://example.com/echo', |
34 |
| - async ({ request, cookies, params, requestId }) => { |
35 |
| - const body = headersToObject(request.headers)['content-type'] === 'text/html' |
36 |
| - ? await request.text() |
37 |
| - : await request.json() |
38 |
| - |
39 |
| - return HttpResponse.json( |
40 |
| - { |
41 |
| - ...request, |
42 |
| - cookies, |
43 |
| - params, |
44 |
| - requestId, |
45 |
| - body, |
46 |
| - url: new URL(request.url), |
47 |
| - headers: request?.headers |
48 |
| - ? headersToObject(request.headers) |
49 |
| - : request?.headers, |
50 |
| - }, |
51 |
| - { headers: request.headers } |
52 |
| - ) |
53 |
| - } |
54 |
| - ), |
55 |
| - http.get('https://example.com/success', () => { |
56 |
| - return HttpResponse.json({ value: 'success' }) |
57 |
| - }), |
58 |
| - http.post('https://example.com/success', ({ request }) => { |
59 |
| - return HttpResponse.json({ value: 'success' }) |
60 |
| - }), |
61 |
| - http.get('https://example.com/empty', () => new HttpResponse('')), |
62 |
| - http.get('https://example.com/error', () => |
63 |
| - HttpResponse.json({ value: 'error' }, { status: 500 }) |
64 |
| - ), |
65 |
| - http.post('https://example.com/error', () => |
66 |
| - HttpResponse.json({ value: 'error' }, { status: 500 }) |
67 |
| - ), |
68 |
| - http.get('https://example.com/nonstandard-error', () => |
69 |
| - HttpResponse.json( |
70 |
| - { |
71 |
| - success: false, |
72 |
| - message: 'This returns a 200 but is really an error', |
73 |
| - }, |
74 |
| - { status: 200 } |
75 |
| - ) |
76 |
| - ), |
77 |
| - http.get('https://example.com/mirror', ({ params }) => |
78 |
| - HttpResponse.json(params) |
79 |
| - ), |
80 |
| - http.post('https://example.com/mirror', ({ params }) => |
81 |
| - HttpResponse.json(params) |
82 |
| - ), |
83 |
| - http.get('https://example.com/posts/random', () => { |
84 |
| - // just simulate an api that returned a random ID |
85 |
| - const { id } = posts[1] |
86 |
| - return HttpResponse.json({ id }) |
87 |
| - }), |
88 |
| - http.get<Post, any, Pick<Post, 'id'>>( |
89 |
| - 'https://example.com/post/:id', |
90 |
| - ({ params }) => HttpResponse.json(posts[params.id]) |
91 |
| - ), |
92 |
| -] |
93 |
| - |
| 2 | +import { handlers } from './handlers' |
94 | 3 |
|
95 | 4 | // This configures a request mocking server with the given request handlers.
|
96 | 5 | export const server = setupServer(...handlers)
|
0 commit comments