|
| 1 | +import { fetch } from 'whatwg-fetch'; |
| 2 | + |
| 3 | +let createMock = async function(path, method, statusCode, response) { |
| 4 | + return await fetch('/__mock-request', { |
| 5 | + method: 'post', |
| 6 | + headers: { |
| 7 | + "Content-Type": "application/json", |
| 8 | + }, |
| 9 | + body: JSON.stringify({ |
| 10 | + path, |
| 11 | + method, |
| 12 | + statusCode, |
| 13 | + response |
| 14 | + }), |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +export let mockServer = { |
| 19 | + async get(path, response, status = 200) { |
| 20 | + return createMock(path, "GET", status, response); |
| 21 | + }, |
| 22 | + |
| 23 | + async post(path, response, status = 200) { |
| 24 | + return createMock(path, "POST", status, response); |
| 25 | + }, |
| 26 | + |
| 27 | + async patch(path, response, status = 200) { |
| 28 | + return createMock(path, "PATCH", status, response); |
| 29 | + }, |
| 30 | + |
| 31 | + async put(path, response, status = 200) { |
| 32 | + return createMock(path, "PUT", status, response); |
| 33 | + }, |
| 34 | + |
| 35 | + async delete(path, response, status = 200) { |
| 36 | + return createMock(path, "DELETE", status, response); |
| 37 | + }, |
| 38 | + |
| 39 | + async cleanUp() { |
| 40 | + return fetch('/__cleanup-mocks'); |
| 41 | + } |
| 42 | +}; |
0 commit comments