mockaton 8.23.2 → 8.23.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/README.md +60 -26
- package/package.json +2 -2
- package/src/Api.js +11 -19
- package/src/Dashboard.css +2 -1
- package/src/cli.js +1 -1
- package/src/utils/openInBrowser.js +3 -3
package/README.md
CHANGED
|
@@ -118,54 +118,86 @@ They will be saved in your `config.mocksDir` following the filename convention.
|
|
|
118
118
|
|
|
119
119
|
|
|
120
120
|
## Basic Usage
|
|
121
|
-
Mockaton is a Node.js program
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
The default `--mocks-dir` is **mockaton-mocks** in the current working directory.
|
|
121
|
+
Mockaton is a Node.js program available as an NPM module. After you have
|
|
122
|
+
Node.js, you can create a sample mock in the default mocks directory, which
|
|
123
|
+
is `./mockaton-mocks`. Then run it with `npx`, which installs it if needed.
|
|
125
124
|
```sh
|
|
126
125
|
mkdir -p mockaton-mocks/api/
|
|
127
126
|
echo "[1,2,3]" > mockaton-mocks/api/foo.GET.200.json
|
|
128
|
-
```
|
|
129
127
|
|
|
130
|
-
### Install and Run
|
|
131
|
-
```sh
|
|
132
|
-
npm install mockaton
|
|
133
128
|
npx mockaton --port 2345
|
|
134
129
|
```
|
|
135
130
|
|
|
136
131
|
## CLI Options
|
|
132
|
+
CLI options override their counterparts in `mockaton.config.js`
|
|
137
133
|
|
|
138
134
|
```txt
|
|
139
135
|
-c, --config <file> (default: ./mockaton.config.js)
|
|
140
136
|
|
|
141
|
-
-m, --mocks-dir <dir> (default: ./mockaton-mocks/)
|
|
142
|
-
-s, --static-dir <dir> (default: ./mockaton-static-mocks/)
|
|
143
|
-
|
|
144
137
|
-H, --host <host> (default: 127.0.0.1)
|
|
145
138
|
-p, --port <port> (default: 0) which means auto-assigned
|
|
139
|
+
|
|
140
|
+
-m, --mocks-dir <dir> (default: ./mockaton-mocks/)
|
|
141
|
+
-s, --static-dir <dir> (default: ./mockaton-static-mocks/)
|
|
146
142
|
```
|
|
147
143
|
|
|
148
144
|
|
|
149
|
-
## mockaton.config.js
|
|
150
|
-
|
|
145
|
+
## mockaton.config.js (Optional)
|
|
146
|
+
As an overview, these are the default options:
|
|
151
147
|
```js
|
|
152
|
-
import {
|
|
148
|
+
import {
|
|
149
|
+
defineConfig,
|
|
150
|
+
jsToJsonPlugin,
|
|
151
|
+
openInBrowser,
|
|
152
|
+
SUPPORTED_METHODS
|
|
153
|
+
} from 'mockaton'
|
|
154
|
+
|
|
153
155
|
|
|
154
156
|
export default defineConfig({
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
mocksDir: 'mockaton-mocks',
|
|
158
|
+
staticDir: 'mockaton-static-mocks',
|
|
159
|
+
ignore: /(\.DS_Store|~)$/,
|
|
160
|
+
|
|
161
|
+
host: '127.0.0.1',
|
|
162
|
+
port: 0,
|
|
163
|
+
|
|
164
|
+
proxyFallback: '',
|
|
165
|
+
collectProxied: false,
|
|
166
|
+
formatCollectedJSON: true,
|
|
167
|
+
|
|
168
|
+
delay: 1200,
|
|
169
|
+
delayJitter: 0,
|
|
170
|
+
|
|
171
|
+
cookies: {},
|
|
172
|
+
|
|
173
|
+
extraHeaders: [],
|
|
174
|
+
|
|
175
|
+
extraMimes: {},
|
|
176
|
+
|
|
177
|
+
plugins: [
|
|
178
|
+
[/\.(js|ts)$/, jsToJsonPlugin]
|
|
179
|
+
],
|
|
180
|
+
|
|
181
|
+
corsAllowed: true,
|
|
182
|
+
corsOrigins: ['*'],
|
|
183
|
+
corsMethods: SUPPORTED_METHODS,
|
|
184
|
+
corsHeaders: ['content-type', 'authorization'],
|
|
185
|
+
corsExposedHeaders: [],
|
|
186
|
+
corsCredentials: true,
|
|
187
|
+
corsMaxAge: 0,
|
|
188
|
+
|
|
189
|
+
onReady: await openInBrowser
|
|
190
|
+
})
|
|
160
191
|
```
|
|
161
192
|
|
|
162
193
|
<details>
|
|
163
|
-
<summary><b>See
|
|
194
|
+
<summary><b>See Config Documentation</b></summary>
|
|
164
195
|
|
|
165
|
-
### `mocksDir
|
|
166
|
-
|
|
196
|
+
### `mocksDir?: string`
|
|
197
|
+
Defaults to `'mockaton-mocks'`.
|
|
167
198
|
|
|
168
199
|
### `staticDir?: string`
|
|
200
|
+
Defaults to `'mockaton-static-mocks'`.
|
|
169
201
|
This option is not needed besides serving partial content (e.g., videos). But
|
|
170
202
|
it’s convenient for serving 200 GET requests without having to add the filename
|
|
171
203
|
extension convention. For example, for using Mockaton as a standalone demo server,
|
|
@@ -191,7 +223,7 @@ tested against the basename (filename without directory path).
|
|
|
191
223
|
|
|
192
224
|
|
|
193
225
|
### `host?: string`
|
|
194
|
-
Defaults to `'
|
|
226
|
+
Defaults to `'127.0.0.1'`
|
|
195
227
|
|
|
196
228
|
### `port?: number`
|
|
197
229
|
Defaults to `0`, which means auto-assigned
|
|
@@ -621,7 +653,7 @@ api/foo/bar.GET.200.json
|
|
|
621
653
|
<br/>
|
|
622
654
|
|
|
623
655
|
## Commander API
|
|
624
|
-
`Commander` is a client for Mockaton’s HTTP API.
|
|
656
|
+
`Commander` is a JavaScript client for Mockaton’s HTTP API.
|
|
625
657
|
All of its methods return their `fetch` response promise.
|
|
626
658
|
```js
|
|
627
659
|
import { Commander } from 'mockaton'
|
|
@@ -631,7 +663,8 @@ const myMockatonAddr = 'http://localhost:2345'
|
|
|
631
663
|
const mockaton = new Commander(myMockatonAddr)
|
|
632
664
|
```
|
|
633
665
|
|
|
634
|
-
<
|
|
666
|
+
<details>
|
|
667
|
+
<summary><b>See Commander Documentation</b></summary>
|
|
635
668
|
|
|
636
669
|
### Select a mock file for a route
|
|
637
670
|
```js
|
|
@@ -687,6 +720,7 @@ default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not aff
|
|
|
687
720
|
```js
|
|
688
721
|
await mockaton.reset()
|
|
689
722
|
```
|
|
723
|
+
</details>
|
|
690
724
|
|
|
691
725
|
|
|
692
726
|
<br/>
|
|
@@ -703,7 +737,7 @@ example, if you are polling, and you want to test the state change.
|
|
|
703
737
|
|
|
704
738
|
### Client Side
|
|
705
739
|
In contrast to Mockaton, which is an HTTP Server, these programs
|
|
706
|
-
hijack the client
|
|
740
|
+
hijack the HTTP client in Node.js and browsers.
|
|
707
741
|
|
|
708
742
|
- [Mock Server Worker (MSW)](https://mswjs.io)
|
|
709
743
|
- [Nock](https://github.com/nock/nock)
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "mockaton",
|
|
3
3
|
"description": "HTTP Mock Server",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.23.
|
|
5
|
+
"version": "8.23.4",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
8
8
|
"license": "MIT",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"pixaton": "1.1.2",
|
|
29
|
-
"puppeteer": "24.
|
|
29
|
+
"puppeteer": "24.19.0"
|
|
30
30
|
},
|
|
31
31
|
"optionalDependencies": {
|
|
32
32
|
"open": "10.2.0"
|
package/src/Api.js
CHANGED
|
@@ -14,18 +14,17 @@ import { DF, API, LONG_POLL_SERVER_TIMEOUT } from './ApiConstants.js'
|
|
|
14
14
|
import { sendOK, sendJSON, sendUnprocessableContent, sendFile } from './utils/http-response.js'
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
const dashboardAssets = [
|
|
18
|
-
'/ApiConstants.js',
|
|
19
|
-
'/ApiCommander.js',
|
|
20
|
-
'/Dashboard.css',
|
|
21
|
-
'/Dashboard.js',
|
|
22
|
-
'/Filename.js',
|
|
23
|
-
'/Logo.svg'
|
|
24
|
-
]
|
|
25
|
-
|
|
26
17
|
export const apiGetRequests = new Map([
|
|
27
|
-
[API.dashboard,
|
|
28
|
-
...
|
|
18
|
+
[API.dashboard, serveDashboardAsset('Dashboard.html')],
|
|
19
|
+
...[
|
|
20
|
+
'/ApiConstants.js',
|
|
21
|
+
'/ApiCommander.js',
|
|
22
|
+
'/Dashboard.css',
|
|
23
|
+
'/Dashboard.js',
|
|
24
|
+
'/Filename.js',
|
|
25
|
+
'/Logo.svg'
|
|
26
|
+
].map(f => [API.dashboard + f, serveDashboardAsset(f)]),
|
|
27
|
+
|
|
29
28
|
[API.cors, getIsCorsAllowed],
|
|
30
29
|
[API.static, listStaticFiles],
|
|
31
30
|
[API.mocks, listMockBrokers],
|
|
@@ -55,13 +54,8 @@ export const apiPatchRequests = new Map([
|
|
|
55
54
|
|
|
56
55
|
/** # GET */
|
|
57
56
|
|
|
58
|
-
function serveDashboard(_, response) {
|
|
59
|
-
sendFile(response, join(import.meta.dirname, 'Dashboard.html'))
|
|
60
|
-
}
|
|
61
|
-
|
|
62
57
|
function serveDashboardAsset(f) {
|
|
63
|
-
return (_, response) =>
|
|
64
|
-
sendFile(response, join(import.meta.dirname, f))
|
|
58
|
+
return (_, response) => sendFile(response, join(import.meta.dirname, f))
|
|
65
59
|
}
|
|
66
60
|
|
|
67
61
|
function listCookies(_, response) { sendJSON(response, cookie.list()) }
|
|
@@ -81,12 +75,10 @@ function longPollClientSyncVersion(req, response) {
|
|
|
81
75
|
sendJSON(response, uiSyncVersion.version)
|
|
82
76
|
return
|
|
83
77
|
}
|
|
84
|
-
|
|
85
78
|
function onAddOrRemoveMock() {
|
|
86
79
|
uiSyncVersion.unsubscribe(onAddOrRemoveMock)
|
|
87
80
|
sendJSON(response, uiSyncVersion.version)
|
|
88
81
|
}
|
|
89
|
-
|
|
90
82
|
response.setTimeout(LONG_POLL_SERVER_TIMEOUT, onAddOrRemoveMock)
|
|
91
83
|
req.on('error', () => {
|
|
92
84
|
uiSyncVersion.unsubscribe(onAddOrRemoveMock)
|
package/src/Dashboard.css
CHANGED
package/src/cli.js
CHANGED
|
@@ -45,7 +45,7 @@ Options:
|
|
|
45
45
|
|
|
46
46
|
Notes:
|
|
47
47
|
* mockaton.config.js supports more options, see:
|
|
48
|
-
https://github.com/ericfortis/mockaton?tab=readme-ov-file#
|
|
48
|
+
https://github.com/ericfortis/mockaton?tab=readme-ov-file#mockatonconfigjs-optional
|
|
49
49
|
* CLI options override their mockaton.config.js counterparts`)
|
|
50
50
|
|
|
51
51
|
else if (args.config && !isFile(args.config)) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
export const openInBrowser = (async () => {
|
|
@@ -13,10 +13,10 @@ export const openInBrowser = (async () => {
|
|
|
13
13
|
function _openInBrowser(address) {
|
|
14
14
|
switch (process.platform) {
|
|
15
15
|
case 'darwin':
|
|
16
|
-
|
|
16
|
+
execFileSync('open', [address])
|
|
17
17
|
break
|
|
18
18
|
case 'win32':
|
|
19
|
-
|
|
19
|
+
execFileSync('start', [address])
|
|
20
20
|
break
|
|
21
21
|
}
|
|
22
22
|
}
|