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