mockaton 2.0.0 → 2.1.0
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-dashboard.png +0 -0
- package/README.md +5 -6
- package/Tests.js +2 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/sample-mocks/api/user/friends.GET.500.txt +7 -0
- package/src/Api.js +0 -1
- package/src/Config.js +4 -4
- package/src/MockDispatcher.js +3 -2
- package/src/mockBrokersCollection.js +3 -1
- package/sample-mocks/api/user/.GET.500.txt +0 -7
- package/sample-mocks/api/user/edit-name.PATCH.500.txt +0 -0
- package/sample-mocks/api/user/likes.GET.500.txt +0 -0
- package/sample-mocks/api/user/logout.POST.500.txt +0 -0
- package/sample-mocks/api/user/loves.GET.500.txt +0 -0
- package/sample-mocks/api/user/videos.GET.500.txt +0 -0
- package/sample-mocks/api/video/[id].GET.500.txt +0 -0
- package/sample-mocks/api/video/stat/[stat-id]/[video-id].GET.500.txt +0 -0
- package/sample-mocks/api/video/stat/[stat-id]/all-videos?limit=[limit].GET.500.txt +0 -0
package/README-dashboard.png
CHANGED
|
Binary file
|
package/README.md
CHANGED
|
@@ -16,8 +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.
|
|
20
|
-
Error_ mock is autogenerated for routes that have no 500.
|
|
19
|
+
- e.g. for testing error responses.
|
|
21
20
|
- __Comment__ on the filename, which is anything within parentheses.
|
|
22
21
|
- e.g. `api/user(my-comment).POST.201.json`
|
|
23
22
|
|
|
@@ -56,7 +55,7 @@ The best way to learn _Mockaton_ is by checking out this repo and
|
|
|
56
55
|
exploring its [sample-mocks/](./sample-mocks) directory. Then, run
|
|
57
56
|
[`./_usage_example.js`](./_usage_example.js) and you’ll see this dashboard:
|
|
58
57
|
|
|
59
|
-
|
|
58
|
+
<img src="./README-dashboard.png" style="max-width:890px"/>
|
|
60
59
|
|
|
61
60
|
|
|
62
61
|
## Delay 🕓
|
|
@@ -98,6 +97,7 @@ interface Config {
|
|
|
98
97
|
skipOpen?: boolean // Prevents opening the dashboard in a browser
|
|
99
98
|
proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
|
|
100
99
|
allowedExt?: RegExp // /\.(json|txt|md|js)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
100
|
+
generate500?: boolean // autogenerates an Internal Server Error empty mock for routes that have no 500
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
@@ -127,10 +127,9 @@ words, it’s useful if you only care about its payload.
|
|
|
127
127
|
### Extension
|
|
128
128
|
`.Method.HttpResponseStatusCode.FileExt`
|
|
129
129
|
|
|
130
|
-
The **file extension** can anything, but `.md`
|
|
131
|
-
for documentation, and mock processors (more on that later).
|
|
130
|
+
The **file extension** can be anything, but `.md` is reserved for documentation.
|
|
132
131
|
|
|
133
|
-
The `Config.allowedExt` regex defaults to: `/\.(json|txt|md|
|
|
132
|
+
The `Config.allowedExt` regex defaults to: `/\.(json|txt|md|js)$/`
|
|
134
133
|
|
|
135
134
|
|
|
136
135
|
### Dynamic Parameters
|
package/Tests.js
CHANGED
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
This is a plain text response for (/api/friends).
|
|
2
|
+
|
|
3
|
+
In this case, it’s for mocking up a 500 - Internal Server Error.
|
|
4
|
+
|
|
5
|
+
This file could have been empty, or some JSON if it had a `.json` extension.
|
|
6
|
+
|
|
7
|
+
By the way, on initialization an 500 is auto-generated for routes that don’t have a 500.
|
package/src/Api.js
CHANGED
package/src/Config.js
CHANGED
|
@@ -9,10 +9,10 @@ export const Config = {
|
|
|
9
9
|
port: 0, // auto-assigned
|
|
10
10
|
delay: 1200, // milliseconds
|
|
11
11
|
cookies: {}, // defaults to the first kv
|
|
12
|
-
database: {},
|
|
13
12
|
skipOpen: false,
|
|
14
13
|
proxyFallback: '', // e.g. http://localhost:9999
|
|
15
|
-
allowedExt: /\.(json|txt|md|js)
|
|
14
|
+
allowedExt: /\.(json|txt|md|js)$/, // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
15
|
+
generate500: false
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function setup(options) {
|
|
@@ -24,10 +24,10 @@ export function setup(options) {
|
|
|
24
24
|
port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
|
|
25
25
|
delay: ms => Number.isInteger(ms) && ms > 0,
|
|
26
26
|
cookies: is(Object),
|
|
27
|
-
database: is(Object),
|
|
28
27
|
skipOpen: is(Boolean),
|
|
29
28
|
proxyFallback: is(String),
|
|
30
|
-
allowedExt: is(RegExp)
|
|
29
|
+
allowedExt: is(RegExp),
|
|
30
|
+
generate500: is(Boolean)
|
|
31
31
|
})
|
|
32
32
|
}
|
|
33
33
|
|
package/src/MockDispatcher.js
CHANGED
|
@@ -32,7 +32,6 @@ export async function dispatchMock(req, response) {
|
|
|
32
32
|
console.log('\n', req.url, '→\n ', file)
|
|
33
33
|
|
|
34
34
|
response.statusCode = status
|
|
35
|
-
response.setHeader('content-type', mimeFor(file))
|
|
36
35
|
if (cookie.getCurrent())
|
|
37
36
|
response.setHeader('set-cookie', cookie.getCurrent())
|
|
38
37
|
|
|
@@ -44,8 +43,10 @@ export async function dispatchMock(req, response) {
|
|
|
44
43
|
? jsExport(req, response)
|
|
45
44
|
: JSON.stringify(jsExport)
|
|
46
45
|
}
|
|
47
|
-
else
|
|
46
|
+
else {
|
|
47
|
+
response.setHeader('content-type', mimeFor(file))
|
|
48
48
|
mockText = readMock(file)
|
|
49
|
+
}
|
|
49
50
|
setTimeout(() => response.end(mockText), delay)
|
|
50
51
|
}
|
|
51
52
|
catch (error) {
|
|
@@ -39,7 +39,9 @@ export function init() {
|
|
|
39
39
|
else
|
|
40
40
|
collection[method][urlMask].register(file)
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
if (Config.generate500)
|
|
44
|
+
forEachBroker(broker => broker.ensureItHas500())
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
function forEachBroker(fn) {
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
This is a plain text response for (/api/user).
|
|
2
|
-
|
|
3
|
-
In this case, it’s for mocking up a 500 - Internal Server Error.
|
|
4
|
-
|
|
5
|
-
This file could have been empty, or some JSON if it had a `.json` extension.
|
|
6
|
-
|
|
7
|
-
By the way, on initialization an 500 is auto-generated for routes that don’t have a 500.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|