mockaton 0.10.1 → 0.10.3
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 +17 -17
- package/package.json +1 -1
- package/src/Dashboard.js +1 -3
- package/src/MockDispatcher.js +2 -2
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ be used for downloading a TAR of your XHR requests following that convention.
|
|
|
16
16
|
### Mock Variants
|
|
17
17
|
Each route can have many mocks, which could either be:
|
|
18
18
|
- Different response __status code__.
|
|
19
|
-
- e.g. for testing error responses.
|
|
19
|
+
- e.g. for testing error responses. As a side note, an _Internal Server
|
|
20
20
|
Error_ mock is autogenerated for routes that have no 500.
|
|
21
21
|
- __Comment__ on the filename, which is anything within parentheses.
|
|
22
22
|
- e.g. `api/user(my-comment).POST.201.json`
|
|
@@ -24,7 +24,7 @@ Each route can have many mocks, which could either be:
|
|
|
24
24
|
Those alternatives can be manually selected in the dashboard
|
|
25
25
|
UI, or programmatically, for instance, for setting up tests.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
The first file in **alphabetical order** becomes the default mock.
|
|
28
28
|
|
|
29
29
|
### Proxying Routes
|
|
30
30
|
`Config.proxyFallback` lets you specify a target
|
|
@@ -43,7 +43,7 @@ exploring its [sample-mocks/](./sample-mocks) directory. Then, run
|
|
|
43
43
|
The clock icon next to the mock selector is a checkbox for delaying a
|
|
44
44
|
particular response. They are handy for testing spinners.
|
|
45
45
|
|
|
46
|
-
The
|
|
46
|
+
The delay is globally configurable via `Config.delay = 1200` (milliseconds).
|
|
47
47
|
|
|
48
48
|
---
|
|
49
49
|
|
|
@@ -57,8 +57,8 @@ import { resolve } from 'node:path'
|
|
|
57
57
|
import { Mockaton } from 'mockaton'
|
|
58
58
|
|
|
59
59
|
Mockaton({
|
|
60
|
-
mocksDir: resolve('my-mocks-dir'),
|
|
61
|
-
|
|
60
|
+
mocksDir: resolve('my-mocks-dir'),
|
|
61
|
+
port: 2345
|
|
62
62
|
})
|
|
63
63
|
```
|
|
64
64
|
|
|
@@ -71,9 +71,9 @@ node my-mockaton.js
|
|
|
71
71
|
interface Config {
|
|
72
72
|
mocksDir: string
|
|
73
73
|
staticDir?: string
|
|
74
|
-
host?: string, // 'localhost'
|
|
75
|
-
port?: number // 0 auto-assigned
|
|
76
|
-
delay?: number // 1200 ms
|
|
74
|
+
host?: string, // defaults to 'localhost'
|
|
75
|
+
port?: number // defaults to 0, which means auto-assigned
|
|
76
|
+
delay?: number // defaults to 1200 (ms)
|
|
77
77
|
cookies?: object
|
|
78
78
|
database?: object // for "Transforms"
|
|
79
79
|
skipOpen?: boolean // Prevents opening the dashboard in a browser
|
|
@@ -84,7 +84,7 @@ interface Config {
|
|
|
84
84
|
|
|
85
85
|
## Cookies
|
|
86
86
|
```js
|
|
87
|
-
import { jwtCookie } from '
|
|
87
|
+
import { jwtCookie } from 'mockaton'
|
|
88
88
|
|
|
89
89
|
Config.cookies = {
|
|
90
90
|
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
@@ -95,10 +95,10 @@ Config.cookies = {
|
|
|
95
95
|
})
|
|
96
96
|
}
|
|
97
97
|
```
|
|
98
|
-
The key is just a label used
|
|
98
|
+
The key is just a label used for selecting a particular cookie in the dashboard.
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
words, it’s useful
|
|
100
|
+
`jwtCookie` has a hardcoded header and signature. In other
|
|
101
|
+
words, it’s useful if you only care about its payload.
|
|
102
102
|
|
|
103
103
|
---
|
|
104
104
|
|
|
@@ -115,7 +115,7 @@ The `Config.allowedExt` regex defaults to: `/\.(json|txt|md|mjs)$/`
|
|
|
115
115
|
|
|
116
116
|
|
|
117
117
|
### Dynamic Parameters
|
|
118
|
-
Anything within square brackets. For example
|
|
118
|
+
Anything within square brackets. For example:
|
|
119
119
|
<pre>
|
|
120
120
|
api/user/<b>[id]</b>/<b>[age]</b>.GET.200.json
|
|
121
121
|
</pre>
|
|
@@ -134,10 +134,10 @@ api/foo.GET.200.json
|
|
|
134
134
|
api/video<b>?limit=[limit]</b>.GET.200.json
|
|
135
135
|
</pre>
|
|
136
136
|
|
|
137
|
-
The query string is ignored when routing to it.
|
|
138
|
-
only used for documenting the URL
|
|
137
|
+
The query string is ignored when routing to it. In other words, it’s
|
|
138
|
+
only used for documenting the URL contract.
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
Speaking of which, in Windows, filenames containing "?" are [not
|
|
141
141
|
permitted](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file),
|
|
142
142
|
but since that’s part of the query string, it’s ignored anyway.
|
|
143
143
|
|
|
@@ -155,7 +155,7 @@ api/foo/(my comment).GET.200.json
|
|
|
155
155
|
|
|
156
156
|
## Documenting Contracts (.md)
|
|
157
157
|
This is handy for documenting request payload parameters. The dashboard will
|
|
158
|
-
print the
|
|
158
|
+
print the Markdown document (as plain text) above the actual payload content.
|
|
159
159
|
|
|
160
160
|
Create a markdown file following the same filename convention.
|
|
161
161
|
The status code can be any number. For example,
|
package/package.json
CHANGED
package/src/Dashboard.js
CHANGED
package/src/MockDispatcher.js
CHANGED
|
@@ -23,7 +23,7 @@ export async function dispatchMock(req, response) {
|
|
|
23
23
|
if (Config.proxyFallback)
|
|
24
24
|
await proxy(req, response)
|
|
25
25
|
else
|
|
26
|
-
sendNotFound(response) // TESTME
|
|
26
|
+
sendNotFound(response) // TODO unit TESTME
|
|
27
27
|
return
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -59,7 +59,7 @@ const nonSafeMethods = ['PATCH', 'POST', 'PUT', 'DELETE', 'CONNECT']
|
|
|
59
59
|
|
|
60
60
|
async function requestBodyForTransform(req, mockAsText) {
|
|
61
61
|
if (nonSafeMethods.includes(req.method))
|
|
62
|
-
return req.headers[DF.isForDashboard] // TESTME
|
|
62
|
+
return req.headers[DF.isForDashboard] // TODO unit TESTME
|
|
63
63
|
? JSON.parse(mockAsText)
|
|
64
64
|
: await parseJSON(req)
|
|
65
65
|
return ''
|