mockaton 8.2.10 → 8.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
<img src="src/mockaton-logo.svg" alt="Mockaton Logo" width="210" style="margin-top: 30px"/>
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
|
|
3
7
|
_Mockaton_ is an HTTP mock server for improving the frontend
|
|
4
8
|
development and testing experience.
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
Mockaton scans a given directory for filenames following a convention similar to
|
|
8
|
-
|
|
10
|
+
In contrast to other solutions, you don’t need to write code for wiring your mocks.
|
|
11
|
+
Instead, Mockaton scans a given directory for filenames following a convention similar to the
|
|
12
|
+
URL paths. For example, the following file will be served on `/api/user/1234`
|
|
9
13
|
```
|
|
10
14
|
my-mocks-dir/api/user/[user-id].GET.200.json
|
|
11
15
|
```
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
Also, you don’t need to mock all your APIs. It can request from your backend
|
|
17
|
+
the routes you don’t have mocks for. See `Config.proxyFallback` below.
|
|
15
18
|
|
|
16
19
|
By the way, [this browser
|
|
17
|
-
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
18
|
-
create a TAR of your requests following that convention.
|
|
20
|
+
extension](https://github.com/ericfortis/devtools-ext-tar-http-requests)
|
|
21
|
+
can create a TAR of your requests following that convention.
|
|
19
22
|
|
|
20
23
|
## Dashboard UI
|
|
21
24
|
|
|
25
|
+
In the dashboard, you can manually select which mock variant to serve for a particular
|
|
26
|
+
route. This is useful for testing different scenarios without changing code or the database state.
|
|
27
|
+
|
|
22
28
|
<picture>
|
|
23
29
|
<source media="(prefers-color-scheme: light)" srcset="./README-dashboard-light.png">
|
|
24
30
|
<source media="(prefers-color-scheme: dark)" srcset="./README-dashboard-dark.png">
|
|
@@ -37,8 +43,7 @@ Create a `my-mockaton.js` file
|
|
|
37
43
|
import { resolve } from 'node:path'
|
|
38
44
|
import { Mockaton } from 'mockaton'
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
// See the Config section below for more options
|
|
46
|
+
// See the Config section for more options
|
|
42
47
|
Mockaton({
|
|
43
48
|
mocksDir: resolve('my-mocks-dir'),
|
|
44
49
|
port: 2345
|
|
@@ -60,7 +65,7 @@ This demo uses the [sample-mocks/](./sample-mocks) directory of this repository.
|
|
|
60
65
|
|
|
61
66
|
Experiment with the Dashboard:
|
|
62
67
|
|
|
63
|
-
- Pick a mock variant from the
|
|
68
|
+
- Pick a mock variant from the _Mock dropdown_ (we’ll discuss them later)
|
|
64
69
|
- Toggle the 🕓 _Delay Responses_ button, (e.g. for testing spinners)
|
|
65
70
|
- Toggle the _500_ button, which sends and _Internal Server Error_ on that endpoint
|
|
66
71
|
|
|
@@ -90,8 +95,7 @@ simulate from the actual backend. In this case, compile your frontend app and
|
|
|
90
95
|
put its built assets in `Config.staticDir`. Then, from the Mockaton dashboard
|
|
91
96
|
you can "Bulk Select" mocks to simulate the complete states you want to demo.
|
|
92
97
|
For bulk-selecting, you just need to add a comment to the mock
|
|
93
|
-
filename
|
|
94
|
-
"Comments" section under the "Filename Convention" for details.
|
|
98
|
+
filename, such as `(demo-part1)`, `(demo-part2)`.
|
|
95
99
|
|
|
96
100
|
|
|
97
101
|
## Motivation
|
|
@@ -147,11 +151,11 @@ Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
|
147
151
|
export default (request, response) => JSON.stringify({ foo: 'bar' })
|
|
148
152
|
```
|
|
149
153
|
|
|
150
|
-
Think of these functions as HTTP handlers
|
|
151
|
-
|
|
154
|
+
Think of these functions as HTTP handlers, so you can
|
|
155
|
+
intercept requests. For example, for writing to a database.
|
|
152
156
|
|
|
153
157
|
<details>
|
|
154
|
-
<summary><b>See
|
|
158
|
+
<summary><b>See Intercepting Requests Examples</b></summary>
|
|
155
159
|
|
|
156
160
|
Imagine you have an initial list of colors, and
|
|
157
161
|
you want to concatenate newly added colors.
|
|
@@ -206,9 +210,10 @@ api/user.GET.200.json
|
|
|
206
210
|
|
|
207
211
|
|
|
208
212
|
### Dynamic Parameters
|
|
209
|
-
Anything within square brackets. For example
|
|
213
|
+
Anything within square brackets is always matched. For example, for this route
|
|
214
|
+
`/api/company/1234/user/5678`
|
|
210
215
|
<pre>
|
|
211
|
-
api/
|
|
216
|
+
api/company/<b>[id]</b>/user/<b>[uid]</b>.GET.200.json
|
|
212
217
|
</pre>
|
|
213
218
|
|
|
214
219
|
### Comments
|
|
@@ -221,18 +226,18 @@ api/foo.GET.200.json
|
|
|
221
226
|
</pre>
|
|
222
227
|
|
|
223
228
|
### Query String Params
|
|
229
|
+
The query string is ignored when routing to it. In other words, it’s only used for
|
|
230
|
+
documenting the URL contract.
|
|
224
231
|
<pre>
|
|
225
232
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
226
233
|
</pre>
|
|
227
234
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
[not permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
231
|
-
but since that’s part of the query string, it’s ignored anyway.
|
|
235
|
+
Speaking of which, in Windows, filenames containing "?" are [not
|
|
236
|
+
permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file), but since that’s part of the query string, it’s ignored anyway.
|
|
232
237
|
|
|
233
238
|
|
|
234
239
|
### Index-like route
|
|
235
|
-
For instance,
|
|
240
|
+
For instance, if you have `api/foo/bar` and
|
|
236
241
|
`api/foo`. For the latter you have two options:
|
|
237
242
|
|
|
238
243
|
**Option A.** Place it outside the directory:
|
|
@@ -275,18 +280,18 @@ For example, `Config.proxyFallback = 'http://example.com:8080'`
|
|
|
275
280
|
|
|
276
281
|
|
|
277
282
|
### `staticDir?: string`
|
|
283
|
+
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
284
|
+
- Use Case 2: For a standalone demo server. For example,
|
|
285
|
+
build your frontend bundle, and serve it from Mockaton.
|
|
286
|
+
|
|
278
287
|
Files under `Config.staticDir` don’t use the filename convention.
|
|
279
|
-
|
|
288
|
+
They take precedence over the `GET` mocks in `Config.mocksDir`.
|
|
280
289
|
For example, if you have two files for `GET /foo/bar.jpg`
|
|
281
290
|
```
|
|
282
291
|
my-static-dir/foo/bar.jpg
|
|
283
292
|
my-mocks-dir/foo/bar.jpg.GET.200.jpg // Unreacheable
|
|
284
293
|
```
|
|
285
294
|
|
|
286
|
-
- Use Case 1: If you have a bunch of static assets you don’t want to add `.GET.200.ext`
|
|
287
|
-
- Use Case 2: For a standalone demo server. For example,
|
|
288
|
-
build your frontend bundle, and serve it from Mockaton.
|
|
289
|
-
|
|
290
295
|
|
|
291
296
|
### `cookies?: { [label: string]: string }`
|
|
292
297
|
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
@@ -347,7 +352,7 @@ Plugins are for processing mocks before sending them.
|
|
|
347
352
|
Note: don’t call `response.end()`
|
|
348
353
|
|
|
349
354
|
<details>
|
|
350
|
-
<summary><b> Plugin Examples </b></summary>
|
|
355
|
+
<summary><b> See Plugin Examples </b></summary>
|
|
351
356
|
|
|
352
357
|
```shell
|
|
353
358
|
npm install yaml
|
|
@@ -381,9 +386,7 @@ function capitalizePlugin(filePath) {
|
|
|
381
386
|
|
|
382
387
|
|
|
383
388
|
### `corsAllowed?: boolean`
|
|
384
|
-
Defaults to `corsAllowed = false`
|
|
385
|
-
|
|
386
|
-
When `Config.corsAllowed === true`, these are the default options:
|
|
389
|
+
Defaults to `corsAllowed = false`. When `Config.corsAllowed === true`, these are the default options:
|
|
387
390
|
```js
|
|
388
391
|
Config.corsOrigins = ['*']
|
|
389
392
|
Config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
|
|
@@ -395,20 +398,19 @@ Config.corsExposedHeaders = [] // headers you need to access in client-side JS
|
|
|
395
398
|
|
|
396
399
|
|
|
397
400
|
### `onReady?: (dashboardUrl: string) => void`
|
|
398
|
-
This defaults to trying to open the dashboard in your default browser in
|
|
399
|
-
|
|
400
|
-
|
|
401
|
+
This defaults to trying to open the dashboard in your default browser in macOS and
|
|
402
|
+
Windows. For a more cross-platform utility, you could `npm install open` and pass it.
|
|
401
403
|
```js
|
|
402
|
-
|
|
404
|
+
import open from 'open'
|
|
405
|
+
Config.onReady = open
|
|
403
406
|
```
|
|
404
407
|
|
|
405
|
-
|
|
408
|
+
If you don’t want to open a browser, pass a noop:
|
|
406
409
|
```js
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
Config.onReady = open
|
|
410
|
+
Config.onReady = () => {}
|
|
410
411
|
```
|
|
411
412
|
|
|
413
|
+
|
|
412
414
|
---
|
|
413
415
|
|
|
414
416
|
## Commander API
|
package/package.json
CHANGED