mockaton 8.23.3 → 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 +59 -26
- package/package.json +1 -1
- package/src/Api.js +11 -19
- package/src/Dashboard.css +2 -1
- package/src/cli.js +1 -1
package/README.md
CHANGED
|
@@ -118,18 +118,13 @@ 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
|
|
|
@@ -139,34 +134,70 @@ CLI options override their counterparts in `mockaton.config.js`
|
|
|
139
134
|
```txt
|
|
140
135
|
-c, --config <file> (default: ./mockaton.config.js)
|
|
141
136
|
|
|
142
|
-
-m, --mocks-dir <dir> (default: ./mockaton-mocks/)
|
|
143
|
-
-s, --static-dir <dir> (default: ./mockaton-static-mocks/)
|
|
144
|
-
|
|
145
137
|
-H, --host <host> (default: 127.0.0.1)
|
|
146
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/)
|
|
147
142
|
```
|
|
148
143
|
|
|
149
144
|
|
|
150
|
-
## mockaton.config.js
|
|
151
|
-
|
|
145
|
+
## mockaton.config.js (Optional)
|
|
146
|
+
As an overview, these are the default options:
|
|
152
147
|
```js
|
|
153
|
-
import {
|
|
148
|
+
import {
|
|
149
|
+
defineConfig,
|
|
150
|
+
jsToJsonPlugin,
|
|
151
|
+
openInBrowser,
|
|
152
|
+
SUPPORTED_METHODS
|
|
153
|
+
} from 'mockaton'
|
|
154
|
+
|
|
154
155
|
|
|
155
156
|
export default defineConfig({
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
+
})
|
|
161
191
|
```
|
|
162
192
|
|
|
163
193
|
<details>
|
|
164
|
-
<summary><b>See
|
|
194
|
+
<summary><b>See Config Documentation</b></summary>
|
|
165
195
|
|
|
166
|
-
### `mocksDir
|
|
167
|
-
|
|
196
|
+
### `mocksDir?: string`
|
|
197
|
+
Defaults to `'mockaton-mocks'`.
|
|
168
198
|
|
|
169
199
|
### `staticDir?: string`
|
|
200
|
+
Defaults to `'mockaton-static-mocks'`.
|
|
170
201
|
This option is not needed besides serving partial content (e.g., videos). But
|
|
171
202
|
it’s convenient for serving 200 GET requests without having to add the filename
|
|
172
203
|
extension convention. For example, for using Mockaton as a standalone demo server,
|
|
@@ -192,7 +223,7 @@ tested against the basename (filename without directory path).
|
|
|
192
223
|
|
|
193
224
|
|
|
194
225
|
### `host?: string`
|
|
195
|
-
Defaults to `'
|
|
226
|
+
Defaults to `'127.0.0.1'`
|
|
196
227
|
|
|
197
228
|
### `port?: number`
|
|
198
229
|
Defaults to `0`, which means auto-assigned
|
|
@@ -622,7 +653,7 @@ api/foo/bar.GET.200.json
|
|
|
622
653
|
<br/>
|
|
623
654
|
|
|
624
655
|
## Commander API
|
|
625
|
-
`Commander` is a client for Mockaton’s HTTP API.
|
|
656
|
+
`Commander` is a JavaScript client for Mockaton’s HTTP API.
|
|
626
657
|
All of its methods return their `fetch` response promise.
|
|
627
658
|
```js
|
|
628
659
|
import { Commander } from 'mockaton'
|
|
@@ -632,7 +663,8 @@ const myMockatonAddr = 'http://localhost:2345'
|
|
|
632
663
|
const mockaton = new Commander(myMockatonAddr)
|
|
633
664
|
```
|
|
634
665
|
|
|
635
|
-
<
|
|
666
|
+
<details>
|
|
667
|
+
<summary><b>See Commander Documentation</b></summary>
|
|
636
668
|
|
|
637
669
|
### Select a mock file for a route
|
|
638
670
|
```js
|
|
@@ -688,6 +720,7 @@ default, but the `proxyFallback`, `colledProxied`, and `corsAllowed` are not aff
|
|
|
688
720
|
```js
|
|
689
721
|
await mockaton.reset()
|
|
690
722
|
```
|
|
723
|
+
</details>
|
|
691
724
|
|
|
692
725
|
|
|
693
726
|
<br/>
|
|
@@ -704,7 +737,7 @@ example, if you are polling, and you want to test the state change.
|
|
|
704
737
|
|
|
705
738
|
### Client Side
|
|
706
739
|
In contrast to Mockaton, which is an HTTP Server, these programs
|
|
707
|
-
hijack the client
|
|
740
|
+
hijack the HTTP client in Node.js and browsers.
|
|
708
741
|
|
|
709
742
|
- [Mock Server Worker (MSW)](https://mswjs.io)
|
|
710
743
|
- [Nock](https://github.com/nock/nock)
|
package/package.json
CHANGED
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)) {
|