mockaton 8.27.0 → 9.1.2
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/CHANGELOG.md +6 -0
- package/README.md +79 -22
- package/index.d.ts +23 -10
- package/index.js +0 -1
- package/lcov.info +2572 -0
- package/package.json +4 -3
- package/src/Api.js +21 -17
- package/src/ApiCommander.js +5 -43
- package/src/ApiConstants.js +3 -4
- package/src/Dashboard.css +19 -15
- package/src/Dashboard.js +55 -77
- package/src/Logo.svg +2 -2
- package/src/Mockaton.js +2 -19
- package/src/cli.js +36 -15
- package/src/config.js +14 -21
- package/src/utils/http-response.js +1 -1
- package/src/utils/mime.js +26 -13
- package/mockup.svg +0 -1037
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
## 9.0.0 (9/13/25)
|
|
5
|
+
- **Breaking change**: Commander GET APIs have been consolidated into `commander.getState()`. These were undocumented APIs, so likely you are not affected.
|
|
6
|
+
- `--no-open` new cli flag. Prevents opening dashboard in a browser. (No-ops `config.onReady`)
|
package/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|

|
|
5
5
|
[](https://github.com/ericfortis/mockaton/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/ericfortis/mockaton/actions/workflows/github-code-scanning/codeql)
|
|
7
|
+
[](https://codecov.io/github/ericfortis/mockaton)
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
An HTTP mock server for simulating APIs with minimal setup
|
|
9
11
|
— ideal for testing difficult to reproduce states.
|
|
@@ -92,9 +94,9 @@ api/videos.GET.<b>500</b>.txt # Internal Server Error
|
|
|
92
94
|
## Scraping Mocks from your Backend
|
|
93
95
|
|
|
94
96
|
### Option 1: Browser Extension
|
|
95
|
-
|
|
97
|
+
The companion Chrome [devtools
|
|
96
98
|
extension](https://github.com/ericfortis/download-http-requests-browser-ext)
|
|
97
|
-
you
|
|
99
|
+
lets you download all the HTTP responses, and they
|
|
98
100
|
get saved following Mockaton’s filename convention.
|
|
99
101
|
|
|
100
102
|
### Option 2: Fallback to Your Backend
|
|
@@ -121,26 +123,36 @@ They will be saved in your `config.mocksDir` following the filename convention.
|
|
|
121
123
|
- Does not write to disk. Except when you select ✅ **Save Mocks** for scraping mocks from a backend.
|
|
122
124
|
- Does not initiate network connections (no logs, no telemetry).
|
|
123
125
|
- Does not hijack your HTTP client.
|
|
124
|
-
- Auditable. Organized and small — under 4 KLoC (
|
|
126
|
+
- Auditable. Organized and small — under 4 KLoC (half is UI and tests).
|
|
125
127
|
|
|
126
128
|
|
|
127
129
|
## Basic Usage
|
|
128
|
-
|
|
130
|
+
1. Install Node.js, which comes with `npm` and `npx`
|
|
131
|
+
|
|
132
|
+
2. Create a sample mock in the default mocks directory (`./mockaton-mocks`)
|
|
129
133
|
```sh
|
|
130
|
-
mkdir -p
|
|
134
|
+
mkdir -p mockaton-mocks/api
|
|
131
135
|
echo "[1,2,3]" > mockaton-mocks/api/foo.GET.200.json
|
|
132
136
|
```
|
|
133
|
-
|
|
134
|
-
Mockaton is a Node.js program, so you install and run it with:
|
|
137
|
+
3. Run Mockaton (`npx` installs it if needed)
|
|
135
138
|
```shell
|
|
136
139
|
npx mockaton --port 2345
|
|
137
140
|
```
|
|
138
141
|
|
|
139
|
-
|
|
142
|
+
4. Test it
|
|
143
|
+
```shell
|
|
144
|
+
curl http://localhost:2345/api/foo
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Alternative Installations
|
|
148
|
+
<details>
|
|
149
|
+
<summary>With NPM…</summary>
|
|
150
|
+
|
|
140
151
|
```shell
|
|
141
152
|
npm install mockaton --save-dev
|
|
142
153
|
```
|
|
143
154
|
|
|
155
|
+
Then in your, `package.json`:
|
|
144
156
|
```json
|
|
145
157
|
{
|
|
146
158
|
"scripts": {
|
|
@@ -148,7 +160,23 @@ npm install mockaton --save-dev
|
|
|
148
160
|
}
|
|
149
161
|
}
|
|
150
162
|
```
|
|
163
|
+
</details>
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
<details>
|
|
167
|
+
<summary>Without NPM…</summary>
|
|
168
|
+
|
|
169
|
+
Since Mockaton has no dependencies, you can create an executable
|
|
170
|
+
by linking to `src/cli.js`.
|
|
171
|
+
|
|
172
|
+
```shell
|
|
173
|
+
git clone https://github.com/ericfortis/mockaton.git
|
|
174
|
+
ln -s `realpath mockaton/src/cli.js` ~/bin/mockaton # some dir in your $PATH
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
</details>
|
|
151
178
|
|
|
179
|
+
<br/>
|
|
152
180
|
|
|
153
181
|
|
|
154
182
|
## CLI Options
|
|
@@ -164,6 +192,8 @@ The CLI options override their counterparts in `mockaton.config.js`
|
|
|
164
192
|
-p, --port <port> (default: 0) which means auto-assigned
|
|
165
193
|
|
|
166
194
|
-q, --quiet Errors only
|
|
195
|
+
--no-open Don’t open dashboard in a browser (noops onReady callback)
|
|
196
|
+
|
|
167
197
|
-h, --help Show this help
|
|
168
198
|
-v, --version Show version
|
|
169
199
|
```
|
|
@@ -186,24 +216,20 @@ export default defineConfig({
|
|
|
186
216
|
|
|
187
217
|
host: '127.0.0.1',
|
|
188
218
|
port: 0,
|
|
219
|
+
|
|
220
|
+
logLevel: 'normal',
|
|
221
|
+
|
|
222
|
+
delay: 1200,
|
|
223
|
+
delayJitter: 0,
|
|
189
224
|
|
|
190
225
|
proxyFallback: '',
|
|
191
226
|
collectProxied: false,
|
|
192
227
|
formatCollectedJSON: true,
|
|
193
228
|
|
|
194
|
-
delay: 1200,
|
|
195
|
-
delayJitter: 0,
|
|
196
|
-
|
|
197
229
|
cookies: {},
|
|
198
|
-
|
|
199
230
|
extraHeaders: [],
|
|
200
|
-
|
|
201
231
|
extraMimes: {},
|
|
202
232
|
|
|
203
|
-
plugins: [
|
|
204
|
-
[/\.(js|ts)$/, jsToJsonPlugin]
|
|
205
|
-
],
|
|
206
|
-
|
|
207
233
|
corsAllowed: true,
|
|
208
234
|
corsOrigins: ['*'],
|
|
209
235
|
corsMethods: SUPPORTED_METHODS,
|
|
@@ -211,10 +237,13 @@ export default defineConfig({
|
|
|
211
237
|
corsExposedHeaders: [],
|
|
212
238
|
corsCredentials: true,
|
|
213
239
|
corsMaxAge: 0,
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
plugins: [
|
|
243
|
+
[/\.(js|ts)$/, jsToJsonPlugin]
|
|
244
|
+
],
|
|
214
245
|
|
|
215
246
|
onReady: await openInBrowser,
|
|
216
|
-
|
|
217
|
-
logLevel: 'normal'
|
|
218
247
|
})
|
|
219
248
|
```
|
|
220
249
|
|
|
@@ -354,7 +383,8 @@ config.extraHeaders = [
|
|
|
354
383
|
### `extraMimes?: { [fileExt: string]: string }`
|
|
355
384
|
```js
|
|
356
385
|
config.extraMimes = {
|
|
357
|
-
jpe: 'application/jpeg'
|
|
386
|
+
jpe: 'application/jpeg',
|
|
387
|
+
html: 'text/html; charset=utf-8' // overrides built-in
|
|
358
388
|
}
|
|
359
389
|
```
|
|
360
390
|
Those extra media types take precedence over the built-in
|
|
@@ -463,15 +493,17 @@ Defaults to `'normal'`.
|
|
|
463
493
|
<details>
|
|
464
494
|
<summary>Programmatic Launch (Optional)</summary>
|
|
465
495
|
|
|
496
|
+
|
|
466
497
|
```js
|
|
467
498
|
import { Mockaton } from 'mockaton'
|
|
468
499
|
import mockatonConfig from './mockaton.config.js'
|
|
469
500
|
|
|
470
|
-
Mockaton({
|
|
501
|
+
const server = Mockaton({
|
|
471
502
|
...mockatonConfig, // Not required, but it’s not read by default.
|
|
472
|
-
port: 3333, // etc.
|
|
473
503
|
})
|
|
474
504
|
```
|
|
505
|
+
See [src/cli.js](src/cli.js)
|
|
506
|
+
|
|
475
507
|
</details>
|
|
476
508
|
|
|
477
509
|
|
|
@@ -732,6 +764,14 @@ with many partial matches, their first mock in alphabetical order wins.
|
|
|
732
764
|
### Set route is delayed flag
|
|
733
765
|
```js
|
|
734
766
|
await mockaton.setRouteIsDelayed('GET', '/api/foo', true)
|
|
767
|
+
// or
|
|
768
|
+
await mockaton.setStaticRouteIsDelayed('/api/foo', true)
|
|
769
|
+
```
|
|
770
|
+
<br/>
|
|
771
|
+
|
|
772
|
+
### Set static route status
|
|
773
|
+
```js
|
|
774
|
+
await mockaton.setStaticRouteStatus('/api/foo', 404)
|
|
735
775
|
```
|
|
736
776
|
|
|
737
777
|
### Set route is proxied flag
|
|
@@ -762,6 +802,23 @@ await mockaton.setCollectProxied(true)
|
|
|
762
802
|
|
|
763
803
|
<br/>
|
|
764
804
|
|
|
805
|
+
### Set global delay value
|
|
806
|
+
```js
|
|
807
|
+
await mockaton.setGlobalDelsy(1200) // ms
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
<br/>
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
### Set CORS allowed
|
|
814
|
+
```js
|
|
815
|
+
await mockaton.setCorsAllowed(true)
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
<br/>
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
765
822
|
### Reset
|
|
766
823
|
Re-initialize the collection. The selected mocks, cookies, and delays go back to
|
|
767
824
|
default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not affected.
|
package/index.d.ts
CHANGED
|
@@ -16,22 +16,20 @@ interface Config {
|
|
|
16
16
|
|
|
17
17
|
host?: string,
|
|
18
18
|
port?: number
|
|
19
|
+
|
|
20
|
+
logLevel?: 'normal' | 'quiet'
|
|
21
|
+
|
|
22
|
+
delay?: number
|
|
23
|
+
delayJitter?: number
|
|
19
24
|
|
|
20
25
|
proxyFallback?: string
|
|
21
26
|
collectProxied?: boolean
|
|
22
27
|
formatCollectedJSON?: boolean
|
|
23
28
|
|
|
24
|
-
delay?: number
|
|
25
|
-
delayJitter?: number
|
|
26
|
-
|
|
27
29
|
cookies?: { [label: string]: string }
|
|
28
|
-
|
|
29
30
|
extraHeaders?: string[]
|
|
30
|
-
|
|
31
31
|
extraMimes?: { [fileExt: string]: string }
|
|
32
32
|
|
|
33
|
-
plugins?: [filenameTester: RegExp, plugin: Plugin][]
|
|
34
|
-
|
|
35
33
|
corsAllowed?: boolean,
|
|
36
34
|
corsOrigins?: string[]
|
|
37
35
|
corsMethods?: string[]
|
|
@@ -39,15 +37,16 @@ interface Config {
|
|
|
39
37
|
corsExposedHeaders?: string[]
|
|
40
38
|
corsCredentials?: boolean
|
|
41
39
|
corsMaxAge?: number
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
plugins?: [filenameTester: RegExp, plugin: Plugin][]
|
|
42
43
|
|
|
43
44
|
onReady?: (address: string) => void
|
|
44
|
-
|
|
45
|
-
logLevel?: 'normal' | 'quiet'
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
|
|
49
48
|
export function Mockaton(options: Partial<Config>): Server | undefined
|
|
50
|
-
export function defineConfig(options: Partial<Config>): Config
|
|
49
|
+
export function defineConfig(options: Partial<Config>): Partial<Config>
|
|
51
50
|
|
|
52
51
|
export const jsToJsonPlugin: Plugin
|
|
53
52
|
|
|
@@ -86,3 +85,17 @@ export type ClientStaticBrokers = {
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
|
|
88
|
+
export interface State {
|
|
89
|
+
brokersByMethod: ClientBrokersByMethod
|
|
90
|
+
staticBrokers: ClientStaticBrokers
|
|
91
|
+
|
|
92
|
+
cookies: [label:string, selected:boolean][]
|
|
93
|
+
comments: string[]
|
|
94
|
+
|
|
95
|
+
delay: number
|
|
96
|
+
|
|
97
|
+
collectProxied: boolean
|
|
98
|
+
proxyFallback: string
|
|
99
|
+
|
|
100
|
+
corsAllowed: boolean
|
|
101
|
+
}
|