|
1 | | - |
| 1 | +<p align="center"> |
| 2 | + <img alt="node-recorder logo" src="./logo.gif" width="50%"> |
| 3 | +</p> |
2 | 4 |
|
3 | | -> Simple recording & replaying of HTTP requests for predictable development & testing. |
| 5 | +- Spend less time writing mocks & fixtures. |
| 6 | +- Automatically record new HTTP(s) requests. |
| 7 | +- Replay fixtures when testing. |
| 8 | +- Works well with [supertest](https://github.com/visionmedia/supertest). |
| 9 | +- Predictable, deterministic filepaths that match the URL: |
4 | 10 |
|
5 | | -## Example |
| 11 | + 1. Call https://api.github.com/rate_limit. |
| 12 | + 1. `./__fixtures__/api.github.com/rate_limit/${hash}.json` |
6 | 13 |
|
7 | | -Given a GraphQL server: |
| 14 | + ```json |
| 15 | + { |
| 16 | + "request": { |
| 17 | + "method": "GET", |
| 18 | + "href": "https://api.github.com/rate_limit", |
| 19 | + "headers": {...}, |
| 20 | + "body": "" |
| 21 | + }, |
| 22 | + "response": { |
| 23 | + "statusCode": 200, |
| 24 | + "headers": {...}, |
| 25 | + "body": {...} |
| 26 | + } |
| 27 | + } |
| 28 | + ``` |
| 29 | + |
| 30 | +* Normalize the `request` & `response`. |
| 31 | +* Alias cookies & Oauth tokens to users, to avoid ambiguity. |
| 32 | +* Ignore requests you don't want to record. |
| 33 | + |
| 34 | +## Installation |
| 35 | + |
| 36 | +```shell |
| 37 | +$ yarn add node-recorder --dev |
| 38 | +# or |
| 39 | +$ npm install node-recorder --save-dev |
| 40 | +``` |
| 41 | + |
| 42 | +## Getting Started |
| 43 | + |
| 44 | +- By simply including `node-recorder`, **all HTTP(s) requests are intercepted**. |
| 45 | +- By default, `RECORD` mode records new fixtures, and replays existing fixures. |
| 46 | +- When in `NODE_ENV=test` or `CI=true`, `REPLAY` mode replays existing fixtures, and throws an error when one doesn't exist. |
| 47 | + _(So that local tests don't suddenly fail in CI)_ |
| 48 | + |
| 49 | +### Recorder Modes |
| 50 | + |
| 51 | +- `bypass` - All network requests bypass the recorder and respond as usual. |
| 52 | +- `record` - Record only new network requests (i.e. those without fixtures), while replaying existing fixtures. |
| 53 | +- `replay` - Replay all network requests using fixtures. **If a fixture is missing, an error is thrown**. |
| 54 | +- `rerecord` - Re-record all network requests. |
| 55 | + |
| 56 | +### Using `node --require` |
| 57 | + |
| 58 | +```shell |
| 59 | +$ node -r node-recorder path/to/server.js |
| 60 | +``` |
| 61 | + |
| 62 | +_(This also works with `mocha`!)_ |
| 63 | + |
| 64 | +### Setting the `mode` via `RECORDER=...` |
| 65 | + |
| 66 | +```shell |
| 67 | +$ RECORDER=ignore node -r node-recorder path/to/server.js |
| 68 | +``` |
| 69 | + |
| 70 | +### Using Jest |
| 71 | + |
| 72 | +Included is a `jest-preset` that will automatically include `node-recorder` and a custom plugin to make toggling modes easier. |
8 | 73 |
|
9 | 74 | ```js |
10 | | -import { recorder } from "back-to-the-fixture"; |
11 | | -import graphql from "express-graphql"; |
| 75 | +// jest.config.js |
| 76 | +module.exports = { |
| 77 | + preset: "node-recorder/jest-preset" |
| 78 | +}; |
| 79 | +``` |
| 80 | + |
| 81 | +Now, running `jest --watch` will add a new `r` option: |
12 | 82 |
|
13 | | -export default graphql((req: Request, res: Response) => { |
14 | | - // 👇 Pull ?mode=record or ?mode=replay |
15 | | - const { mode } = req.query; |
| 83 | +``` |
| 84 | +Watch Usage |
| 85 | + › Press a to run all tests. |
| 86 | + › Press f to run only failed tests. |
| 87 | + › Press p to filter by a filename regex pattern. |
| 88 | + › Press t to filter by a test name regex pattern. |
| 89 | + › Press q to quit watch mode. |
| 90 | + › Press r to change recording mode from "REPLAY". |
| 91 | + › Press Enter to trigger a test run. |
| 92 | +``` |
16 | 93 |
|
17 | | - // 👇 Create a recorder for this request |
18 | | - recorder.configure({ mode }); |
| 94 | +Pressing `r` will toggle between the various modes: |
19 | 95 |
|
20 | | - return { |
21 | | - graphiql: true, |
22 | | - pretty: true, |
23 | | - schema |
24 | | - }; |
25 | | -}); |
| 96 | +``` |
| 97 | + ╭─────────────────────────────╮ |
| 98 | + │ │ |
| 99 | + │ node-recorder: RECORD │ |
| 100 | + │ │ |
| 101 | + ╰─────────────────────────────╯ |
26 | 102 | ``` |
27 | 103 |
|
28 | | -- **Record** network calls – <http://localhost:3000/?mode=record> |
29 | | -- **Replay** network calls - <http://localhost:3000/?mode=replay> |
| 104 | +### Configuring `recorder.config.js` |
30 | 105 |
|
31 | | -Fixtures are stored based on their URL with the name `${hash}.${user}.json`: |
| 106 | +Within your project, you can create a `recorder.config.js` that exports: |
32 | 107 |
|
| 108 | +```js |
| 109 | +// recorder.conig.js |
| 110 | +module.exports = { |
| 111 | + identify(request, response) {...}, |
| 112 | + ignore(request) {...}, |
| 113 | + normalize(request, response) {...} |
| 114 | +} |
33 | 115 | ``` |
34 | | -. |
35 | | -└── __fixtures__ |
36 | | - └── api.github.com |
37 | | - └── rate_limit |
38 | | - └── 4280543676.all.json |
| 116 | + |
| 117 | +- `request` is the same as the fixture (e.g. `body`, `headers`, `href`, `method`), but |
| 118 | + with an additional `url` property from https://github.com/unshiftio/url-parse to simplify conditional logic. |
| 119 | +- `response` contains `body`, `headers`, & `statusCode`. |
| 120 | + |
| 121 | +#### `identify` a `request` or `response |
| 122 | + |
| 123 | +This is useful when network requests are stateful, in that they rely on an authorization call first, then they pass along a token/cookie to subsequent calls: |
| 124 | + |
| 125 | +1. Suppose you login by calling `/login?user=foo&password=bar`. |
| 126 | +2. The response contains `{ "token": "abc123" }3. Now, to get data, you call`/api?token=abc123`. |
| 127 | + |
| 128 | +When recording fixtures, the token `abc123` isn't clearly associated with the user `foo`. |
| 129 | + |
| 130 | +To address this, you can `identify` the `request` and `response`, so that the fixtures are aliased accordingly: |
| 131 | + |
| 132 | +```js |
| 133 | +identify(request, response) { |
| 134 | + const { user, token } = request.query |
| 135 | + |
| 136 | + if (request.href.endsWith("/login")) { |
| 137 | + // We know the user, but not the token yet |
| 138 | + if (!response) { |
| 139 | + return user |
| 140 | + } |
| 141 | + |
| 142 | + // Upon login, associate this `user` with the `token` |
| 143 | + return [user, response.body.token] |
| 144 | + } |
| 145 | + |
| 146 | + // API calls supply a `token`, which has been associated with a `user` |
| 147 | + if (request.href.endsWith("/api")) { |
| 148 | + return token |
| 149 | + } |
| 150 | +} |
39 | 151 | ``` |
40 | 152 |
|
41 | | -This way, similar requests for different users/logins in your testing can be |
42 | | -easily found. |
| 153 | +Now, when recorded fixtures will look like: |
43 | 154 |
|
44 | | -## Installation |
| 155 | +- `127.0.0.1/login/${hash}.${user}.json` |
| 156 | +- `127.0.0.1/api/${hash}.${user}.json` |
45 | 157 |
|
46 | | -```shell |
47 | | -yarn add --dev back-to-the-fixture |
| 158 | +This way, similar-looking network requests (e.g. login & GraphQL) can be differentiated and easily searched for. |
| 159 | + |
| 160 | +#### `ignore` a `request` |
| 161 | + |
| 162 | +Typically, you don't want to record fixtures for things like analytics or reporting. |
| 163 | + |
| 164 | +```js |
| 165 | +// recorder.conig.js |
| 166 | +module.exports = { |
| 167 | + ignore(request) { |
| 168 | + if (request.href.includes("www.google-analytics.com")) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + return false; |
| 173 | + } |
| 174 | +}; |
48 | 175 | ``` |
| 176 | + |
| 177 | +#### `normalize` a `request` or `response` |
| 178 | + |
| 179 | +Fixtures are meant to make development & testing _easier_, so modification is necessary. |
| 180 | + |
| 181 | +- **Changing `request` changes the filename `hash` of the fixture**. You may need to `record` again. |
| 182 | +- `normalize` is called **before** the network request and **after**. This means that `response` may be `undefined`! |
| 183 | +- You can **change `response` by hand, or via `normalize` without affecting the filename `hash` of the fixture**. |
| 184 | + |
| 185 | +```js |
| 186 | +module.exports = { |
| 187 | + normalize(request, response) { |
| 188 | + // Suppose you never care about `user-agent` |
| 189 | + delete request.headers["user-agent"]; |
| 190 | + |
| 191 | + // We may not have a response (yet) |
| 192 | + if (response) { |
| 193 | + // ...or the `date` |
| 194 | + delete response; |
| 195 | + } |
| 196 | + } |
| 197 | +}; |
| 198 | +``` |
| 199 | + |
| 200 | +## MIT License |
| 201 | + |
| 202 | +## Author |
| 203 | + |
| 204 | +- Eric Clemmons |
0 commit comments