-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Filter requests matched by a route handler (#189)
- Loading branch information
1 parent
e9678c4
commit 5d57c32
Showing
6 changed files
with
242 additions
and
76 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 |
---|---|---|
|
@@ -170,30 +170,37 @@ server.any('/session').passthrough(); | |
server.get('/session/1').passthrough(false); | ||
``` | ||
|
||
### recordingName | ||
### filter | ||
|
||
Override the recording name for the given route. This allows for grouping common | ||
requests to share a single recording which can drastically help de-clutter test | ||
recordings. | ||
Filter requests matched by the route handler with a predicate callback function. | ||
This can be useful when trying to match a request by a part of the url, a header, | ||
and/or parts of the request body. | ||
|
||
For example, if your tests always make a `/users` or `/session` call, instead of | ||
having each of those requests be recorded for every single test, you can use | ||
this to create a common recording file for them. | ||
The callback will receive the [Request](server/request) | ||
as an argument. Return `true` to match the request, `false` otherwise. | ||
|
||
| Param | Type | Description | | ||
| ------------- | -------- | ------------------------------------------------------------------------- | | ||
| recordingName | `String` | Name of the [recording](api#recordingName) to store the recordings under. | | ||
?> Multiple filters can be chained together. They must all return `true` for the route handler to match the given request. | ||
|
||
| Param | Type | Description | | ||
| -------- | ---------- | ----------------------------- | | ||
| callback | `Function` | The filter predicate function | | ||
|
||
**Example** | ||
|
||
```js | ||
server.any('/session').recordingName('User Session'); | ||
|
||
server.get('/users/:id').recordingName('User Data'); | ||
server | ||
.any() | ||
.filter(req => req.hasHeader('Authentication')); | ||
.on('request', req => { | ||
res.setHeader('Authentication', 'test123') | ||
}); | ||
|
||
server | ||
.get('/users/1') | ||
.recordingName(); /* Fallback to the polly instance's recording name */ | ||
.get('/users/:id') | ||
.filter(req => req.params.id === '1'); | ||
.intercept((req, res) => { | ||
res.status(200).json({ email: '[email protected]' }); | ||
}); | ||
``` | ||
|
||
### configure | ||
|
@@ -217,3 +224,29 @@ server.get('/users/:id').configure({ timing: Timing.relative(3.0) }); | |
|
||
server.get('/users/1').configure({ logging: true }); | ||
``` | ||
|
||
### recordingName | ||
|
||
Override the recording name for the given route. This allows for grouping common | ||
requests to share a single recording which can drastically help de-clutter test | ||
recordings. | ||
|
||
For example, if your tests always make a `/users` or `/session` call, instead of | ||
having each of those requests be recorded for every single test, you can use | ||
this to create a common recording file for them. | ||
|
||
| Param | Type | Description | | ||
| ------------- | -------- | ------------------------------------------------------------------------- | | ||
| recordingName | `String` | Name of the [recording](api#recordingName) to store the recordings under. | | ||
|
||
**Example** | ||
|
||
```js | ||
server.any('/session').recordingName('User Session'); | ||
|
||
server.get('/users/:id').recordingName('User Data'); | ||
|
||
server | ||
.get('/users/1') | ||
.recordingName(); /* Fallback to the polly instance's recording name */ | ||
``` |
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
Oops, something went wrong.