mockaton 2.1.0 → 2.2.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 +36 -20
- package/Tests.js +4 -2
- package/index.d.ts +2 -1
- package/package.json +1 -1
- package/sample-mocks/api/user/likes.GET.200.js +3 -1
- package/sample-mocks/api/user/links.GET.200.js +8 -0
- package/src/Config.js +4 -2
- package/src/MockDispatcher.js +5 -4
- package/src/Mockaton.js +2 -0
- package/sample-mocks/api/user/loves.GET.200.js +0 -7
package/README-dashboard.png
CHANGED
|
Binary file
|
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ The first file in **alphabetical order** becomes the default mock.
|
|
|
28
28
|
### Optionally, you can write mocks in JavaScript
|
|
29
29
|
An Object, Array, or String is sent as JSON.
|
|
30
30
|
|
|
31
|
-
`api/
|
|
31
|
+
`api/foo.GET.200.js`
|
|
32
32
|
```js
|
|
33
33
|
export default [
|
|
34
34
|
{ id: 0 }
|
|
@@ -98,27 +98,9 @@ interface Config {
|
|
|
98
98
|
proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
|
|
99
99
|
allowedExt?: RegExp // /\.(json|txt|md|js)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
100
100
|
generate500?: boolean // autogenerates an Internal Server Error empty mock for routes that have no 500
|
|
101
|
+
extraHeaders?: []
|
|
101
102
|
}
|
|
102
103
|
```
|
|
103
|
-
|
|
104
|
-
## Cookies
|
|
105
|
-
```js
|
|
106
|
-
import { jwtCookie } from 'mockaton'
|
|
107
|
-
|
|
108
|
-
Config.cookies = {
|
|
109
|
-
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
110
|
-
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
111
|
-
'My JWT': jwtCookie('my-cookie', {
|
|
112
|
-
email: 'john.doe@example.com',
|
|
113
|
-
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
|
114
|
-
})
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
The key is just a label used for selecting a particular cookie in the dashboard.
|
|
118
|
-
|
|
119
|
-
`jwtCookie` has a hardcoded header and signature. In other
|
|
120
|
-
words, it’s useful if you only care about its payload.
|
|
121
|
-
|
|
122
104
|
---
|
|
123
105
|
|
|
124
106
|
## File Name Convention
|
|
@@ -171,6 +153,40 @@ api/foo/?bar=[bar].GET.200.json
|
|
|
171
153
|
api/foo/(my comment).GET.200.json
|
|
172
154
|
```
|
|
173
155
|
|
|
156
|
+
|
|
157
|
+
## `Config.cookies`
|
|
158
|
+
The selected cookie is sent in every response in the `Set-Cookie` header.
|
|
159
|
+
```js
|
|
160
|
+
import { jwtCookie } from 'mockaton'
|
|
161
|
+
|
|
162
|
+
Config.cookies = {
|
|
163
|
+
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
164
|
+
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
165
|
+
'My JWT': jwtCookie('my-cookie', {
|
|
166
|
+
email: 'john.doe@example.com',
|
|
167
|
+
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
The key is just a label used for selecting a particular cookie in the dashboard.
|
|
172
|
+
|
|
173
|
+
`jwtCookie` has a hardcoded header and signature. In other
|
|
174
|
+
words, it’s useful if you only care about its payload.
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
## `Config.extraHeaders`
|
|
178
|
+
They are applied last, right before ending the response. In
|
|
179
|
+
other words, they can overwrite the `Content-Type`. They are
|
|
180
|
+
not tuples, the header name goes in even-numbered indices.
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
Config.extraHeaders = [
|
|
184
|
+
'Server', 'Mockaton',
|
|
185
|
+
'Set-Cookie', 'another-cookie=FOO;Path=/;SameSite=strict',
|
|
186
|
+
'Set-Cookie', 'another-cookie=BAR;Path=/;SameSite=strict'
|
|
187
|
+
]
|
|
188
|
+
```
|
|
189
|
+
|
|
174
190
|
## Documenting Contracts (.md)
|
|
175
191
|
This is handy for documenting request payload parameters. The dashboard will
|
|
176
192
|
print the Markdown document (as plain text) above the actual payload content.
|
package/Tests.js
CHANGED
|
@@ -111,7 +111,8 @@ const server = Mockaton({
|
|
|
111
111
|
userA: 'CookieA',
|
|
112
112
|
userB: 'CookieB'
|
|
113
113
|
},
|
|
114
|
-
generate500: true
|
|
114
|
+
generate500: true,
|
|
115
|
+
extraHeaders: ['Server', 'MockatonTester']
|
|
115
116
|
})
|
|
116
117
|
server.on('listening', runTests)
|
|
117
118
|
|
|
@@ -158,7 +159,7 @@ async function runTests() {
|
|
|
158
159
|
await reset()
|
|
159
160
|
for (const [url, file, body] of fixtures)
|
|
160
161
|
await testMockDispatching(url, file, body)
|
|
161
|
-
|
|
162
|
+
|
|
162
163
|
await testMockDispatching('/api/object', 'api/object.GET.200.js', { JSON_FROM_JS: true }, mimeFor('.json'))
|
|
163
164
|
await testJsFunctionMocks()
|
|
164
165
|
|
|
@@ -205,6 +206,7 @@ async function testMockDispatching(url, file, expectedBody, forcedMime = void 0)
|
|
|
205
206
|
it('status: ' + status, () => equal(res.status, status))
|
|
206
207
|
it('cookie: ' + mime, () => equal(res.headers.get('set-cookie'), 'CookieA'))
|
|
207
208
|
it('delay is under 1 sec', () => equal((new Date()).getTime() - now.getTime() < 1000, true))
|
|
209
|
+
it('extra header', () => equal(res.headers.get('server'), 'MockatonTester'))
|
|
208
210
|
})
|
|
209
211
|
}
|
|
210
212
|
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/Config.js
CHANGED
|
@@ -12,7 +12,8 @@ export const Config = {
|
|
|
12
12
|
skipOpen: false,
|
|
13
13
|
proxyFallback: '', // e.g. http://localhost:9999
|
|
14
14
|
allowedExt: /\.(json|txt|md|js)$/, // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
15
|
-
generate500: false
|
|
15
|
+
generate500: false,
|
|
16
|
+
extraHeaders: []
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export function setup(options) {
|
|
@@ -27,7 +28,8 @@ export function setup(options) {
|
|
|
27
28
|
skipOpen: is(Boolean),
|
|
28
29
|
proxyFallback: is(String),
|
|
29
30
|
allowedExt: is(RegExp),
|
|
30
|
-
generate500: is(Boolean)
|
|
31
|
+
generate500: is(Boolean),
|
|
32
|
+
extraHeaders: Array.isArray
|
|
31
33
|
})
|
|
32
34
|
}
|
|
33
35
|
|
package/src/MockDispatcher.js
CHANGED
|
@@ -31,10 +31,6 @@ export async function dispatchMock(req, response) {
|
|
|
31
31
|
const { file, status, delay } = broker
|
|
32
32
|
console.log('\n', req.url, '→\n ', file)
|
|
33
33
|
|
|
34
|
-
response.statusCode = status
|
|
35
|
-
if (cookie.getCurrent())
|
|
36
|
-
response.setHeader('set-cookie', cookie.getCurrent())
|
|
37
|
-
|
|
38
34
|
let mockText
|
|
39
35
|
if (file.endsWith('.js')) {
|
|
40
36
|
response.setHeader('content-type', mimeFor('.json'))
|
|
@@ -47,6 +43,11 @@ export async function dispatchMock(req, response) {
|
|
|
47
43
|
response.setHeader('content-type', mimeFor(file))
|
|
48
44
|
mockText = readMock(file)
|
|
49
45
|
}
|
|
46
|
+
|
|
47
|
+
if (cookie.getCurrent())
|
|
48
|
+
response.setHeader('set-cookie', cookie.getCurrent())
|
|
49
|
+
|
|
50
|
+
response.writeHead(status, Config.extraHeaders)
|
|
50
51
|
setTimeout(() => response.end(mockText), delay)
|
|
51
52
|
}
|
|
52
53
|
catch (error) {
|
package/src/Mockaton.js
CHANGED
|
@@ -14,6 +14,8 @@ export function Mockaton(options) {
|
|
|
14
14
|
mockBrokerCollection.init()
|
|
15
15
|
|
|
16
16
|
return createServer(async (req, response) => {
|
|
17
|
+
response.setHeader('Server', 'Mockaton')
|
|
18
|
+
|
|
17
19
|
const { url, method } = req
|
|
18
20
|
if (method === 'GET' && apiGetRequests.has(url))
|
|
19
21
|
apiGetRequests.get(url)(req, response)
|