-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It exposes more options for mocking post requests.
- Loading branch information
Showing
4 changed files
with
50 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,63 @@ | ||
import { fetch } from 'whatwg-fetch'; | ||
|
||
let createMock = async function(path, method, statusCode, response) { | ||
let createMock = async function({ path, method, body, statusCode, response }) { | ||
return await fetch('/__mock-request', { | ||
method: 'post', | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
path, | ||
body, | ||
method, | ||
statusCode, | ||
response | ||
}), | ||
}); | ||
} | ||
|
||
export let mockServer = { | ||
async get(path, response, status = 200) { | ||
return createMock(path, "GET", status, response); | ||
}, | ||
|
||
async post(path, response, status = 200) { | ||
return createMock(path, "POST", status, response); | ||
}, | ||
|
||
async patch(path, response, status = 200) { | ||
return createMock(path, "PATCH", status, response); | ||
}, | ||
|
||
async put(path, response, status = 200) { | ||
return createMock(path, "PUT", status, response); | ||
let mockServer = { | ||
get(path, response, statusCode = 200) { | ||
// when needed we'll change this to use something like MockGet | ||
// see MockPost for the idea | ||
return createMock({ | ||
path, | ||
method: "GET", | ||
statusCode, | ||
response | ||
}); | ||
}, | ||
|
||
async delete(path, response, status = 200) { | ||
return createMock(path, "DELETE", status, response); | ||
post(path, body = {}) { | ||
return new MockPost({ path, body }); | ||
}, | ||
|
||
async cleanUp() { | ||
return fetch('/__cleanup-mocks'); | ||
} | ||
}; | ||
|
||
class MockPost { | ||
constructor({ path, body }) { | ||
this.path = path; | ||
this.body = body; | ||
this.status = 200; | ||
} | ||
|
||
statusCode(status) { | ||
this.status = status; | ||
return this; | ||
} | ||
|
||
reply(response) { | ||
return createMock({ | ||
path: this.path, | ||
body: this.body, | ||
method: "POST", | ||
statusCode: this.status, | ||
response | ||
}); | ||
} | ||
} | ||
|
||
export { mockServer }; |
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