mockaton 11.1.2 → 11.1.4
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/Makefile +21 -1
- package/README.md +124 -166
- package/package.json +9 -4
- package/src/Dashboard.css +3 -7
- package/src/Dashboard.js +5 -4
- package/src/MockDispatcher.js +1 -1
- package/src/StaticDispatcher.js +3 -2
- package/src/mockBrokersCollection.js +1 -1
package/Makefile
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
docker: docker-build docker-run
|
|
2
|
+
|
|
3
|
+
docker-build:
|
|
4
|
+
@docker build --tag mockaton $(PWD)
|
|
5
|
+
|
|
6
|
+
docker-run: docker-stop
|
|
7
|
+
@docker run --name mockaton \
|
|
8
|
+
--publish 127.0.0.1:2020:2020 \
|
|
9
|
+
--volume $(PWD)/mockaton.config.js:/app/mockaton.config.js \
|
|
10
|
+
--volume $(PWD)/mockaton-mocks:/app/mockaton-mocks \
|
|
11
|
+
--volume $(PWD)/mockaton-static-mocks:/app/mockaton-static-mocks \
|
|
12
|
+
mockaton
|
|
13
|
+
|
|
14
|
+
docker-stop:
|
|
15
|
+
@docker stop mockaton >/dev/null 2>&1 || true
|
|
16
|
+
@docker rm mockaton >/dev/null 2>&1 || true
|
|
17
|
+
|
|
18
|
+
|
|
1
19
|
start:
|
|
2
20
|
@node src/cli.js
|
|
3
21
|
|
|
@@ -10,7 +28,7 @@ test:
|
|
|
10
28
|
|
|
11
29
|
test-docker:
|
|
12
30
|
@docker run --rm --interactive --tty \
|
|
13
|
-
--volume
|
|
31
|
+
--volume $(PWD):/app \
|
|
14
32
|
--workdir /app \
|
|
15
33
|
node:24 \
|
|
16
34
|
make test
|
|
@@ -26,8 +44,10 @@ pixaton:
|
|
|
26
44
|
--import=./pixaton-tests/_setup.js \
|
|
27
45
|
'pixaton-tests/**/*.test.js'
|
|
28
46
|
|
|
47
|
+
|
|
29
48
|
outdated:
|
|
30
49
|
@npm outdated --parseable |\
|
|
31
50
|
awk -F: '{ printf "npm i %-30s ;# %s\n", $$4, $$2 }'
|
|
32
51
|
|
|
52
|
+
|
|
33
53
|
.PHONY: *
|
package/README.md
CHANGED
|
@@ -6,31 +6,50 @@
|
|
|
6
6
|
[](https://github.com/ericfortis/mockaton/actions/workflows/github-code-scanning/codeql)
|
|
7
7
|
[](https://codecov.io/github/ericfortis/mockaton)
|
|
8
8
|
|
|
9
|
-
|
|
10
9
|
An HTTP mock server for simulating APIs with minimal setup
|
|
11
|
-
— ideal for testing difficult to reproduce states.
|
|
10
|
+
— ideal for testing difficult to reproduce backend states.
|
|
12
11
|
|
|
13
12
|
## Overview
|
|
14
13
|
With Mockaton, you don’t need to write code for wiring up your
|
|
15
14
|
mocks. Instead, a given directory is scanned for filenames
|
|
16
15
|
following a convention similar to the URLs.
|
|
17
16
|
|
|
18
|
-
For example, for [/api/
|
|
17
|
+
For example, for [/api/company/123](#), the filename could be:
|
|
19
18
|
|
|
20
19
|
<pre>
|
|
21
|
-
<code>my-mocks-dir/<b>api/
|
|
20
|
+
<code>my-mocks-dir/<b>api/company</b>/[company-id].GET.200.json</code>
|
|
22
21
|
</pre>
|
|
23
22
|
|
|
24
23
|
<br/>
|
|
25
24
|
|
|
26
|
-
## Dashboard
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
## Quick Start (Docker)
|
|
27
|
+
This will spin up Mockaton with the mock directories included in this repo:
|
|
28
|
+
[mockaton-mocks/](./mockaton-mocks) and [mockaton-static-mocks/](./mockaton-static-mocks)
|
|
29
|
+
mounted on the container.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
git clone https://github.com/ericfortis/mockaton.git --depth 1
|
|
33
|
+
cd mockaton
|
|
34
|
+
make docker
|
|
35
|
+
```
|
|
36
|
+
Dashboard: http://localhost:2020/mockaton
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Test it:
|
|
40
|
+
```shell
|
|
41
|
+
curl localhost:2020/api/user
|
|
42
|
+
```
|
|
43
|
+
|
|
30
44
|
|
|
45
|
+
<br/>
|
|
46
|
+
|
|
47
|
+
## Dashboard
|
|
31
48
|
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
On the dashboard you can select a mock variant for a particular route,
|
|
50
|
+
🕓 delaying responses, or triggering an autogenerated `500` error, among
|
|
51
|
+
other features. Nonetheless, there’s a programmatic API, which is
|
|
52
|
+
handy for setting up tests (see **Commander API** section below).
|
|
34
53
|
|
|
35
54
|
<picture>
|
|
36
55
|
<source media="(prefers-color-scheme: light)" srcset="pixaton-tests/macos/pic-for-readme.vp761x720.light.gold.png">
|
|
@@ -54,6 +73,12 @@ api/login<b>(locked out user)</b>.POST.423.json
|
|
|
54
73
|
api/login<b>(invalid login attempt)</b>.POST.401.json
|
|
55
74
|
</pre>
|
|
56
75
|
|
|
76
|
+
You can **Bulk Select** mocks by comments to simulate the complete states
|
|
77
|
+
you want. For example:
|
|
78
|
+
|
|
79
|
+
<img src="docs/bulk-select.png" width="180px">
|
|
80
|
+
|
|
81
|
+
|
|
57
82
|
### Different response status code
|
|
58
83
|
For instance, you can use a `4xx` or `5xx` status code for triggering error
|
|
59
84
|
responses, or a `2xx` such as `204` for testing empty collections.
|
|
@@ -69,13 +94,13 @@ api/videos.GET.<b>500</b>.txt # Internal Server Error
|
|
|
69
94
|
|
|
70
95
|
## Scraping Mocks from your Backend
|
|
71
96
|
|
|
72
|
-
### Option 1: Browser
|
|
97
|
+
### Option 1: Browser extension
|
|
73
98
|
The companion Chrome [devtools
|
|
74
99
|
extension](https://github.com/ericfortis/download-http-requests-browser-ext)
|
|
75
100
|
lets you download all the HTTP responses, and they
|
|
76
101
|
get saved following Mockaton’s filename convention.
|
|
77
102
|
|
|
78
|
-
### Option 2: Fallback to
|
|
103
|
+
### Option 2: Fallback to your backend
|
|
79
104
|
<details>
|
|
80
105
|
<summary>Learn more…</summary>
|
|
81
106
|
|
|
@@ -97,138 +122,94 @@ They will be saved in your `config.mocksDir` following the filename convention.
|
|
|
97
122
|
|
|
98
123
|
|
|
99
124
|
## Motivation
|
|
100
|
-
<details>
|
|
101
|
-
<summary>Motivation…</summary>
|
|
102
|
-
|
|
103
|
-
**No API state should be too hard to test.**
|
|
104
|
-
With Mockaton, developers can achieve correctness and speed.
|
|
105
|
-
|
|
106
|
-
### Correctness
|
|
107
|
-
- Enables testing of complex scenarios that would otherwise be skipped. e.g.,
|
|
108
|
-
- Triggering errors on third-party APIs.
|
|
109
|
-
- Triggering errors on your project’s backend (if you are a frontend developer).
|
|
110
|
-
- Allows for deterministic, comprehensive, and consistent state.
|
|
111
|
-
- Spot inadvertent regressions during development.
|
|
112
|
-
- Use it to set up screenshot tests, e.g., with [pixaton](https://github.com/ericfortis/pixaton).
|
|
113
|
-
|
|
114
|
-
### Speed
|
|
115
|
-
- Works around unstable dev backends while developing UIs.
|
|
116
|
-
- Spinning up development infrastructure.
|
|
117
|
-
- Syncing database states.
|
|
118
|
-
- Prevents progress from being blocked by waiting for APIs.
|
|
119
|
-
- Time travel. If you commit the mocks to your repo,
|
|
120
|
-
you don’t have to downgrade backends for:
|
|
121
|
-
- checking out long-lived branches
|
|
122
|
-
- bisecting bugs
|
|
123
125
|
|
|
124
|
-
|
|
126
|
+
### Testing scenarios that would otherwise be skipped
|
|
127
|
+
- Simulate errors on third-party APIs, or on your project’s backend (if you are a frontend dev, or unfamiliar with that code)
|
|
128
|
+
- Trigger dynamic states on an API. You can do this by using comments on mock filenames, for example, for polled alerts or notifications.
|
|
129
|
+
- Trigger empty (no content) responses
|
|
130
|
+
- Sometimes, the ideal flow you need is just too difficult to reproduce from the actual backend
|
|
131
|
+
|
|
132
|
+
### Works around unstable dev backends while developing UIs
|
|
133
|
+
- Spinning up dev infrastructure
|
|
134
|
+
- Syncing the database
|
|
135
|
+
- Mitigates progress from being blocked by waiting for APIs
|
|
136
|
+
|
|
137
|
+
### Time travel
|
|
138
|
+
If you commit the mocks to your repo, you don’t have to downgrade backends when:
|
|
139
|
+
- Checking out long-lived branches
|
|
140
|
+
- Bisecting bugs
|
|
141
|
+
|
|
142
|
+
### Deterministic and comprehensive states
|
|
143
|
+
- Ideal for setting up screenshot tests, e.g., with [pixaton](https://github.com/ericfortis/pixaton)
|
|
144
|
+
- Spot inadvertent regressions during development. For example, the demo app in
|
|
145
|
+
this repo has a list of colors containing all of their possible states, such as
|
|
146
|
+
permutations for out-of-stock, new-arrival, and discontinued. This way you’ll
|
|
147
|
+
indirectly notice if something else broke.
|
|
125
148
|
|
|
149
|
+
<img src="./demo-app-vite/pixaton-tests/pic-for-readme.vp740x880.light.gold.png" alt="Mockaton Demo App Screenshot" width="740" />
|
|
126
150
|
|
|
127
151
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
### Testing Backend or Frontend
|
|
133
|
-
- Empty responses
|
|
134
|
-
- Errors such as _Bad Request_ and _Internal Server Error_
|
|
135
|
-
- Mocking third-party APIs
|
|
136
|
-
- Polled resources (for triggering their different states)
|
|
137
|
-
- alerts
|
|
138
|
-
- notifications
|
|
139
|
-
- slow to build resources
|
|
140
|
-
|
|
141
|
-
### Testing Frontend
|
|
142
|
-
- Spinners by delaying responses
|
|
143
|
-
- Setting up UI tests
|
|
144
|
-
|
|
145
|
-
### Demoing complex backend states
|
|
146
|
-
Sometimes, the ideal flow you need is too difficult to reproduce from the actual backend.
|
|
147
|
-
For this, you can **Bulk Select** mocks by comments to simulate the complete states
|
|
148
|
-
you want. For example, by adding `(demo-part1)`, `(demo-part2)` to the filenames.
|
|
149
|
-
|
|
150
|
-
Similarly, you can deploy a **Standalone Demo Server** by compiling the frontend app and
|
|
151
|
-
putting its built assets in `config.staticDir`. And simulate the flow by Bulk Selecting mocks.
|
|
152
|
-
The [aot-fetch-demo repo](https://github.com/ericfortis/aot-fetch-demo) has a working example.
|
|
152
|
+
### Standalone demo server (Docker)
|
|
153
|
+
You can demo your app by compiling the frontend and putting
|
|
154
|
+
its built assets in `config.staticDir`. For example, this
|
|
155
|
+
repo includes a demo which builds and runs a docker container.
|
|
153
156
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
+
```sh
|
|
158
|
+
git clone https://github.com/ericfortis/mockaton.git --depth 1
|
|
159
|
+
cd mockaton/demo-app-vite
|
|
160
|
+
make start-standalone-demo
|
|
161
|
+
```
|
|
162
|
+
- App: http://localhost:4040
|
|
163
|
+
- Dashboard: http://localhost:4040/mockaton
|
|
157
164
|
|
|
158
165
|
|
|
166
|
+
### Privacy and security
|
|
167
|
+
- Does not write to disk. Except when you select ✅ **Save Mocks** for scraping mocks from a backend
|
|
168
|
+
- Does not initiate network connections (no logs, no telemetry)
|
|
169
|
+
- Does not hijack your HTTP client
|
|
170
|
+
- Auditable, organized, and small. 4 KLoC (half is UI and tests)
|
|
171
|
+
- Zero dependencies. No runtime and no build packages.
|
|
159
172
|
|
|
160
173
|
<br/>
|
|
161
174
|
|
|
162
|
-
## Basic Usage
|
|
163
175
|
|
|
164
|
-
|
|
176
|
+
## Usage Without Docker
|
|
165
177
|
|
|
166
|
-
|
|
178
|
+
Requires Node.js. **v22.18+** support writing mocks in TypeScript.
|
|
179
|
+
|
|
180
|
+
1. Create a mock in the default mocks directory (`./mockaton-mocks`)
|
|
167
181
|
```sh
|
|
168
182
|
mkdir -p mockaton-mocks/api
|
|
169
183
|
echo "[1,2,3]" > mockaton-mocks/api/foo.GET.200.json
|
|
170
184
|
```
|
|
171
|
-
|
|
185
|
+
2. Run Mockaton (`npx` comes with Node, and installs Mockaton if needed)
|
|
172
186
|
```shell
|
|
173
|
-
npx mockaton --port
|
|
187
|
+
npx mockaton --port 4040
|
|
174
188
|
```
|
|
175
189
|
|
|
176
|
-
|
|
190
|
+
3. Test it
|
|
177
191
|
```shell
|
|
178
|
-
curl localhost:
|
|
192
|
+
curl localhost:4040/api/foo
|
|
179
193
|
```
|
|
180
194
|
|
|
181
|
-
5. Optionally, use a `mockaton.config.js`
|
|
182
|
-
```js
|
|
183
|
-
import { defineConfig } from 'mockaton'
|
|
184
195
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
})
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
### Alternative Installations
|
|
192
|
-
<details>
|
|
193
|
-
<summary>With NPM (package.json)…</summary>
|
|
194
|
-
|
|
195
|
-
```shell
|
|
196
|
+
## Or, on Node Projects
|
|
197
|
+
```sh
|
|
196
198
|
npm install mockaton --save-dev
|
|
197
199
|
```
|
|
198
200
|
|
|
199
|
-
|
|
201
|
+
In your `package.json`:
|
|
200
202
|
```json
|
|
201
|
-
{
|
|
202
|
-
"
|
|
203
|
-
"mockaton": "mockaton --port 2345"
|
|
204
|
-
}
|
|
203
|
+
"scripts": {
|
|
204
|
+
"mockaton": "mockaton --port 4040"
|
|
205
205
|
}
|
|
206
206
|
```
|
|
207
|
-
</details>
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
<details>
|
|
211
|
-
<summary>Without NPM…</summary>
|
|
212
|
-
|
|
213
|
-
Since Mockaton has no dependencies, you can create an executable
|
|
214
|
-
by linking to `src/cli.js`.
|
|
215
|
-
|
|
216
|
-
```shell
|
|
217
|
-
git clone https://github.com/ericfortis/mockaton.git --depth 1
|
|
218
|
-
ln -s `realpath mockaton/src/cli.js` ~/bin/mockaton # some dir in your $PATH
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
</details>
|
|
222
|
-
|
|
223
207
|
<br/>
|
|
224
208
|
|
|
225
209
|
|
|
226
210
|
## CLI Options
|
|
227
211
|
The CLI options override their counterparts in `mockaton.config.js`
|
|
228
212
|
|
|
229
|
-
<details>
|
|
230
|
-
<summary>CLI Options…</summary>
|
|
231
|
-
|
|
232
213
|
```txt
|
|
233
214
|
-c, --config <file> (default: ./mockaton.config.js)
|
|
234
215
|
|
|
@@ -241,10 +222,9 @@ The CLI options override their counterparts in `mockaton.config.js`
|
|
|
241
222
|
-q, --quiet Errors only
|
|
242
223
|
--no-open Don’t open dashboard in a browser (noops onReady callback)
|
|
243
224
|
|
|
244
|
-
-h, --help
|
|
245
|
-
-v, --version
|
|
225
|
+
-h, --help
|
|
226
|
+
-v, --version
|
|
246
227
|
```
|
|
247
|
-
</details>
|
|
248
228
|
|
|
249
229
|
|
|
250
230
|
## mockaton.config.js (Optional)
|
|
@@ -253,8 +233,6 @@ Mockaton looks for a file `mockaton.config.js` in its current working directory.
|
|
|
253
233
|
<details>
|
|
254
234
|
<summary>Defaults Overview… </summary>
|
|
255
235
|
|
|
256
|
-
As an overview, these are the defaults:
|
|
257
|
-
|
|
258
236
|
```js
|
|
259
237
|
import {
|
|
260
238
|
defineConfig,
|
|
@@ -271,9 +249,9 @@ export default defineConfig({
|
|
|
271
249
|
|
|
272
250
|
host: '127.0.0.1',
|
|
273
251
|
port: 0,
|
|
274
|
-
|
|
252
|
+
|
|
275
253
|
logLevel: 'normal',
|
|
276
|
-
|
|
254
|
+
|
|
277
255
|
delay: 1200,
|
|
278
256
|
delayJitter: 0,
|
|
279
257
|
|
|
@@ -292,8 +270,8 @@ export default defineConfig({
|
|
|
292
270
|
corsExposedHeaders: [],
|
|
293
271
|
corsCredentials: true,
|
|
294
272
|
corsMaxAge: 0,
|
|
295
|
-
|
|
296
|
-
|
|
273
|
+
|
|
274
|
+
|
|
297
275
|
plugins: [
|
|
298
276
|
[/\.(js|ts)$/, jsToJsonPlugin]
|
|
299
277
|
],
|
|
@@ -308,7 +286,7 @@ export default defineConfig({
|
|
|
308
286
|
<summary><b>Config Documentation…</b></summary>
|
|
309
287
|
|
|
310
288
|
### `mocksDir?: string`
|
|
311
|
-
Defaults to `'mockaton-mocks'`.
|
|
289
|
+
Defaults to `'mockaton-mocks'`.
|
|
312
290
|
|
|
313
291
|
### `staticDir?: string`
|
|
314
292
|
Defaults to `'mockaton-static-mocks'`.
|
|
@@ -338,7 +316,7 @@ The regex rule is tested against the basename (filename without directory path).
|
|
|
338
316
|
|
|
339
317
|
|
|
340
318
|
### `watcherEnabled?: boolean`
|
|
341
|
-
Defaults to `true`
|
|
319
|
+
Defaults to `true`
|
|
342
320
|
|
|
343
321
|
When `false`, if you **add**, **delete**, or **rename** you’ll need to click **"Reset"**
|
|
344
322
|
on the Dashboard, or call `commander.reset()` in order to re-initialize the collection.
|
|
@@ -497,7 +475,7 @@ config.plugins = [
|
|
|
497
475
|
// IOW, your plugins array overwrites the default list. This way you can remove it.
|
|
498
476
|
[/\.(js|ts)$/, jsToJsonPlugin],
|
|
499
477
|
|
|
500
|
-
|
|
478
|
+
|
|
501
479
|
[/\.yml$/, yamlToJsonPlugin],
|
|
502
480
|
|
|
503
481
|
|
|
@@ -555,7 +533,7 @@ At any rate, you can trigger any command besides opening a browser.
|
|
|
555
533
|
|
|
556
534
|
<br/>
|
|
557
535
|
|
|
558
|
-
### `logLevel?: 'quiet' | 'normal' | 'verbose'`
|
|
536
|
+
### `logLevel?: 'quiet' | 'normal' | 'verbose'`
|
|
559
537
|
Defaults to `'normal'`.
|
|
560
538
|
|
|
561
539
|
- `quiet`: only errors (stderr)
|
|
@@ -580,53 +558,39 @@ const server = await Mockaton(
|
|
|
580
558
|
</details>
|
|
581
559
|
|
|
582
560
|
|
|
583
|
-
|
|
584
561
|
<br/>
|
|
585
562
|
|
|
586
|
-
## Demo App (Vite + React)
|
|
587
|
-
|
|
588
|
-
```sh
|
|
589
|
-
git clone https://github.com/ericfortis/mockaton.git --depth 1
|
|
590
|
-
cd mockaton/demo-app-vite
|
|
591
|
-
npm install
|
|
592
|
-
|
|
593
|
-
npm run mockaton
|
|
594
|
-
npm run start # in another terminal
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
The demo app has a list of colors containing all of their possible states. For example,
|
|
598
|
-
permutations for out-of-stock, new-arrival, and discontinued.
|
|
599
|
-
|
|
600
|
-
<img src="./demo-app-vite/pixaton-tests/pic-for-readme.vp740x880.light.gold.png" alt="Mockaton Demo App Screenshot" width="740" />
|
|
601
|
-
|
|
602
|
-
<br/>
|
|
603
|
-
<br/>
|
|
604
|
-
|
|
605
|
-
|
|
606
563
|
|
|
607
564
|
## You can write JSON mocks in JavaScript or TypeScript
|
|
565
|
+
_TypeScript mocks need **Node 22.18+ or 23.6+**_
|
|
566
|
+
|
|
608
567
|
For example, `api/foo.GET.200.js`
|
|
609
568
|
|
|
610
|
-
|
|
569
|
+
### Option A: An Object, Array, or String is sent as JSON
|
|
611
570
|
|
|
612
571
|
```js
|
|
613
572
|
export default { foo: 'bar' }
|
|
614
573
|
```
|
|
615
574
|
|
|
616
|
-
|
|
575
|
+
### Option B: Function (async or sync)
|
|
617
576
|
|
|
618
|
-
Return a `string | Buffer | Uint8Array`, but don’t call `response.end()`
|
|
577
|
+
**Return** a `string | Buffer | Uint8Array`, but **don’t call** `response.end()`
|
|
619
578
|
|
|
620
579
|
```js
|
|
621
580
|
export default (request, response) =>
|
|
622
581
|
JSON.stringify({ foo: 'bar' })
|
|
623
582
|
```
|
|
624
583
|
|
|
625
|
-
|
|
626
|
-
|
|
584
|
+
#### About Custom HTTP Handlers
|
|
585
|
+
|
|
586
|
+
For example, you can intercept requests to write to a database. Or
|
|
587
|
+
act based on some query string value, etc. In summary, you get Node’s
|
|
588
|
+
`request`, `response` as arguments, so you can think of Mockaton as a
|
|
589
|
+
router, but in the handlers you return, instead of ending the response.
|
|
590
|
+
|
|
627
591
|
|
|
628
592
|
<details>
|
|
629
|
-
<summary><b>
|
|
593
|
+
<summary><b>Examples…</b></summary>
|
|
630
594
|
|
|
631
595
|
Imagine you have an initial list of colors, and
|
|
632
596
|
you want to concatenate newly added colors.
|
|
@@ -668,9 +632,12 @@ export default function listColors() {
|
|
|
668
632
|
**What if I need to serve a static .js or .ts?**
|
|
669
633
|
|
|
670
634
|
**Option A:** Put it in your `config.staticDir` without the `.GET.200.js` extension.
|
|
635
|
+
In other words, mocks in `staticDir` take precedence over `mocksDir/*`.
|
|
671
636
|
|
|
672
637
|
**Option B:** Read it and return it. For example:
|
|
673
638
|
```js
|
|
639
|
+
import { readFileSync } from 'node:fs'
|
|
640
|
+
|
|
674
641
|
export default function (_, response) {
|
|
675
642
|
response.setHeader('Content-Type', 'application/javascript')
|
|
676
643
|
return readFileSync('./some-dir/foo.js', 'utf8')
|
|
@@ -715,7 +682,7 @@ want a `Content-Type` header in the response.
|
|
|
715
682
|
<br/>
|
|
716
683
|
|
|
717
684
|
### Dynamic parameters
|
|
718
|
-
Anything within square brackets is always matched.
|
|
685
|
+
Anything within square brackets is always matched.
|
|
719
686
|
|
|
720
687
|
For example, for <a href="#">/api/company/<b>123</b>/user/<b>789</b></a>,
|
|
721
688
|
the filename could be:
|
|
@@ -786,7 +753,7 @@ All of its methods return their `fetch` response promise.
|
|
|
786
753
|
import { Commander } from 'mockaton'
|
|
787
754
|
|
|
788
755
|
|
|
789
|
-
const myMockatonAddr = 'http://localhost:
|
|
756
|
+
const myMockatonAddr = 'http://localhost:4040'
|
|
790
757
|
const mockaton = new Commander(myMockatonAddr)
|
|
791
758
|
```
|
|
792
759
|
|
|
@@ -872,7 +839,6 @@ await mockaton.setCorsAllowed(true)
|
|
|
872
839
|
<br/>
|
|
873
840
|
|
|
874
841
|
|
|
875
|
-
|
|
876
842
|
### Reset
|
|
877
843
|
Re-initialize the collection. The selected mocks, cookies, and delays go back to
|
|
878
844
|
default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not affected.
|
|
@@ -882,16 +848,6 @@ await mockaton.reset()
|
|
|
882
848
|
</details>
|
|
883
849
|
|
|
884
850
|
|
|
885
|
-
<br/>
|
|
886
|
-
|
|
887
|
-
## Privacy and Security
|
|
888
|
-
- Zero dependencies (no runtime and no build packages).
|
|
889
|
-
- Does not write to disk. Except when you select ✅ **Save Mocks** for scraping mocks from a backend.
|
|
890
|
-
- Does not initiate network connections (no logs, no telemetry).
|
|
891
|
-
- Does not hijack your HTTP client.
|
|
892
|
-
- Auditable. Organized and small — under 4 KLoC (half is UI and tests).
|
|
893
|
-
|
|
894
|
-
|
|
895
851
|
<br/>
|
|
896
852
|
|
|
897
853
|
## Alternatives worth learning as well
|
|
@@ -901,12 +857,13 @@ These are similar to Mockaton in the sense that you can modify the
|
|
|
901
857
|
mock response without loosing or risking your frontend code state. For
|
|
902
858
|
example, if you are polling, and you want to test the state change.
|
|
903
859
|
|
|
904
|
-
- Chrome DevTools allows for [overriding responses](https://developer.chrome.com/docs/devtools/overrides)
|
|
905
|
-
- Reverse Proxies such as [Burp](https://portswigger.net/burp) are also handy for overriding responses
|
|
860
|
+
- Chrome DevTools allows for [overriding responses](https://developer.chrome.com/docs/devtools/overrides).
|
|
861
|
+
- Reverse Proxies such as [Burp](https://portswigger.net/burp) are also handy for overriding responses. Not easy but
|
|
862
|
+
very powerful.
|
|
906
863
|
|
|
907
864
|
### Client Side
|
|
908
|
-
In contrast to Mockaton, which is an HTTP Server, these
|
|
909
|
-
hijack
|
|
865
|
+
In contrast to Mockaton, which is an HTTP Server, these
|
|
866
|
+
programs hijack your browser’s HTTP client (and Node’s).
|
|
910
867
|
|
|
911
868
|
- [Mock Server Worker (MSW)](https://mswjs.io)
|
|
912
869
|
- [Nock](https://github.com/nock/nock)
|
|
@@ -919,8 +876,9 @@ hijack the HTTP client in Node.js and browsers.
|
|
|
919
876
|
- [Mock](https://github.com/dhuan/mock)
|
|
920
877
|
- [Swagger](https://swagger.io/)
|
|
921
878
|
|
|
879
|
+
|
|
880
|
+
<br/>
|
|
922
881
|
<br/>
|
|
923
882
|
|
|
924
|
-
---
|
|
925
883
|
|
|
926
884
|

|
package/package.json
CHANGED
|
@@ -2,9 +2,14 @@
|
|
|
2
2
|
"name": "mockaton",
|
|
3
3
|
"description": "HTTP Mock Server",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "11.1.
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"version": "11.1.4",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./index.js",
|
|
10
|
+
"types": "./index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
8
13
|
"license": "MIT",
|
|
9
14
|
"repository": "https://github.com/ericfortis/mockaton",
|
|
10
15
|
"keywords": [
|
|
@@ -22,7 +27,7 @@
|
|
|
22
27
|
},
|
|
23
28
|
"devDependencies": {
|
|
24
29
|
"pixaton": "1.1.3",
|
|
25
|
-
"puppeteer": "24.
|
|
30
|
+
"puppeteer": "24.29.1"
|
|
26
31
|
},
|
|
27
32
|
"engines": {
|
|
28
33
|
"node": ">=22.18 <23 || >=23.6"
|
package/src/Dashboard.css
CHANGED
|
@@ -356,7 +356,6 @@ main {
|
|
|
356
356
|
border-color: var(--colorSecondaryActionBorder);
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
|
-
|
|
360
359
|
}
|
|
361
360
|
}
|
|
362
361
|
|
|
@@ -377,11 +376,11 @@ table {
|
|
|
377
376
|
tr.animIn {
|
|
378
377
|
opacity: 0;
|
|
379
378
|
transform: scaleY(0);
|
|
380
|
-
animation:
|
|
379
|
+
animation: _kfRowIn 180ms ease-in-out forwards;
|
|
381
380
|
}
|
|
382
381
|
}
|
|
383
382
|
|
|
384
|
-
@keyframes
|
|
383
|
+
@keyframes _kfRowIn {
|
|
385
384
|
to {
|
|
386
385
|
opacity: 1;
|
|
387
386
|
transform: scaleY(1);
|
|
@@ -611,10 +610,7 @@ table {
|
|
|
611
610
|
> code {
|
|
612
611
|
white-space: pre;
|
|
613
612
|
tab-size: 2;
|
|
614
|
-
|
|
615
|
-
> .json {
|
|
616
|
-
color: var(--colorSecondaryAction);
|
|
617
|
-
}
|
|
613
|
+
color: var(--colorSecondaryAction);
|
|
618
614
|
|
|
619
615
|
.syntaxPunc {
|
|
620
616
|
color: var(--colorSecondaryAction);
|
package/src/Dashboard.js
CHANGED
|
@@ -36,7 +36,6 @@ const CSS = {
|
|
|
36
36
|
rightSide: null,
|
|
37
37
|
status4xx: null,
|
|
38
38
|
|
|
39
|
-
json: null,
|
|
40
39
|
syntaxAttr: null,
|
|
41
40
|
syntaxAttrVal: null,
|
|
42
41
|
syntaxKey: null,
|
|
@@ -649,8 +648,8 @@ async function updatePayloadViewer(proxied, file, response) {
|
|
|
649
648
|
}))
|
|
650
649
|
else {
|
|
651
650
|
const body = await response.text() || t`/* Empty Response Body */`
|
|
652
|
-
if (mime === 'application/json')
|
|
653
|
-
payloadViewerCodeRef.elem.replaceChildren(
|
|
651
|
+
if (mime === 'application/json')
|
|
652
|
+
payloadViewerCodeRef.elem.replaceChildren(SyntaxJSON(body))
|
|
654
653
|
else if (isXML(mime))
|
|
655
654
|
payloadViewerCodeRef.elem.replaceChildren(SyntaxXML(body))
|
|
656
655
|
else
|
|
@@ -755,9 +754,11 @@ function initRealTimeUpdates() {
|
|
|
755
754
|
ErrorToast.close()
|
|
756
755
|
|
|
757
756
|
const version = await response.json()
|
|
757
|
+
const shouldSkip = oldVersion === undefined
|
|
758
758
|
if (oldVersion !== version) { // because it could be < or >
|
|
759
759
|
oldVersion = version
|
|
760
|
-
|
|
760
|
+
if (!shouldSkip)
|
|
761
|
+
store.fetchState()
|
|
761
762
|
}
|
|
762
763
|
longPoll()
|
|
763
764
|
}
|
package/src/MockDispatcher.js
CHANGED
|
@@ -31,7 +31,7 @@ export async function dispatchMock(req, response) {
|
|
|
31
31
|
if (cookie.getCurrent())
|
|
32
32
|
response.setHeader('Set-Cookie', cookie.getCurrent())
|
|
33
33
|
|
|
34
|
-
for (let i = 0; i < config.extraHeaders.length; i += 2)
|
|
34
|
+
for (let i = 0; i < config.extraHeaders.length; i += 2) // TESTME
|
|
35
35
|
response.setHeader(config.extraHeaders[i], config.extraHeaders[i + 1])
|
|
36
36
|
|
|
37
37
|
response.statusCode = broker.auto500 ? 500 : broker.status // TESTME plugins can change it
|
package/src/StaticDispatcher.js
CHANGED
|
@@ -9,18 +9,19 @@ import { config, calcDelay } from './config.js'
|
|
|
9
9
|
import { sendMockNotFound, sendPartialContent } from './utils/http-response.js'
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
// TODO HEAD
|
|
12
13
|
export async function dispatchStatic(req, response) {
|
|
13
14
|
const broker = brokerByRoute(req.url)
|
|
14
15
|
|
|
15
16
|
setTimeout(async () => {
|
|
16
|
-
if (!broker || broker.status === 404) {
|
|
17
|
+
if (!broker || broker.status === 404) {
|
|
17
18
|
sendMockNotFound(response)
|
|
18
19
|
return
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
const file = join(config.staticDir, broker.route)
|
|
22
23
|
if (!isFile(file)) {
|
|
23
|
-
sendMockNotFound(response)
|
|
24
|
+
sendMockNotFound(response)
|
|
24
25
|
return
|
|
25
26
|
}
|
|
26
27
|
|