Skip to content

Commit 31ea6fe

Browse files
committed
Merge pull request #10 from wheresrhys/status-number
added status response shorthand
2 parents 688db75 + 662d32e commit 31ea6fe

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22
Mock http requests made using fetch (or isomorphic-fetch)
33

44
*notes*
5-
- When using isomorphic-fetch or node-fetch fetch should be added as a global
6-
- fetch-mock doesn't declare fetch or Promise as dependencies; as you're testing `fetch` it's assumed you're already taking care of these globals
5+
- When using isomorphic-fetch or node-fetch `fetch` should be added as a global
6+
- fetch-mock doesn't declare `fetch` or `Promise` as dependencies; as you're testing `fetch` it's assumed you're already taking care of these globals
77
- If you prefer documentation by example skip to the bottom of this README
88

99
## API
1010

1111
`require('fetch-mock')` exports a singleton with the following methods
1212

1313
### `mock(config)`
14-
Replaces `fetch()` with a sinon stub which, in addition to the default sinon behaviour, records each of its calls and optionally returns a stub response or passes the call through to `fetch()`. `config` is an optional* object with the following properties.
14+
Replaces `fetch()` with a sinon stub which, in addition to the default sinon behaviour, records it's calls, grouped by route, and optionally returns a stub response or passes the call through to `fetch()`. `config` is an optional* object with the following properties.
1515

16-
* `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. Each route object must have the following properties. If multiple routes are specified the first matching route will be used to define the response
16+
* `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. If multiple routes are specified the first matching route will be used to define the response. Each route object must have the following properties.
1717
* `name`: A unique string naming the route
1818
* `matcher`: The rule for matching calls to `fetch()`. Accepts any of the following
1919
* `string`: Either an exact url to match e.g. 'http://www.site.com/page.html' or, if the string begins with a `^`, the string following the `^` must begin the url e.g. '^http://www.site.com' would match 'http://www.site.com' or 'http://www.site.com/page.html'
2020
* `RegExp`: A regular expression to test the url against
21-
* `Function(url, opts)`: A function that is passed the url and opts `fetch` is called with and that returns a Boolean
21+
* `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns a Boolean
2222
* `response`: Configures the response object returned by the mock. Can take any of the following values
23+
* `number`: creates a response with the number as the response status
2324
* `string`: creates a 200 response with the string as the response body
2425
* `object`: If the object contains any of the properties body, status, headers, throws; then these properties - all of them optional - are used to construct a response as follows
25-
* `body`: Retunred in the response body
26+
* `body`: Returned in the response body
2627
* `status`: Returned in the response status
2728
* `headers`: Returned in the response headers. They should be defined as an object literal (property names case-insensitive) which will be converted to a `Headers` instance
2829
* `throws`: If this property is present then a `Promise` rejected with the value of `throws` is returned
@@ -68,7 +69,7 @@ Returns a Boolean denoting whether any calls matched the given route
6869
Normally calling `mock()` twice without restoring inbetween will throw an error. `reMock()` calls `restore()` internally before calling `mock()` again. This allows you to put a generic call to `mock()` in a `beforeEach()` while retaining the flexibility to vary the responses for some tests
6970

7071
### `registerRoute(name, matcher, response)`
71-
Often your application/module will always need responses for some calls in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a lti-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute()` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.
72+
Often your application/module will need a mocked response for some http requests in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a multi-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute()` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.
7273

7374
`registerRoute()` takes either of the following parameters
7475
* `object`: An object similar to the route objects accepted by `mock()`
@@ -78,8 +79,6 @@ Often your application/module will always need responses for some calls in order
7879
### `unregisterRoute(name)`
7980
Unregisters one or more previously registered routes. Accepts either a string or an array of strings
8081

81-
82-
8382
## Example
8483
```javascript
8584

@@ -156,7 +155,6 @@ describe('content', function () {
156155
});
157156

158157
after(function () {
159-
// I wonder what this does??
160158
fetchMock.unregisterRoute('content');
161159
})
162160

src/fetch-mock.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ var theGlobal;
1010

1111
function mockResponse (url, config) {
1212
// allow just body to be passed in as this is the commonest use case
13-
if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
13+
if (typeof config === 'number') {
14+
config = {
15+
status: config
16+
};
17+
} else if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
1418
config = {
1519
body: config
1620
};

test/spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@ module.exports = function (fetchMock, theGlobal) {
333333
});
334334

335335
describe('responses', function () {
336+
337+
it('respond with a status', function (done) {
338+
fetchMock.mock({
339+
routes: {
340+
name: 'route',
341+
matcher: 'http://it.at.there',
342+
response: 300
343+
}
344+
});
345+
fetch('http://it.at.there')
346+
.then(function (res) {
347+
expect(res.status).to.equal(300);
348+
done();
349+
});
350+
});
351+
336352
it('respond with a string', function (done) {
337353
fetchMock.mock({
338354
routes: {

0 commit comments

Comments
 (0)