mockaton 1.0.0 → 1.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.md
CHANGED
|
@@ -26,6 +26,17 @@ 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
|
+
### Optionally, you can write mocks in JavaScript
|
|
30
|
+
An Object, Array, or String is sent as JSON.
|
|
31
|
+
|
|
32
|
+
`api/user/likes.GET.200.js`
|
|
33
|
+
```js
|
|
34
|
+
export default [
|
|
35
|
+
{ id: 0 }
|
|
36
|
+
]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
29
40
|
### Proxying Routes
|
|
30
41
|
`Config.proxyFallback` lets you specify a target
|
|
31
42
|
server for serving routes you don’t have mocks for.
|
|
@@ -78,7 +89,7 @@ interface Config {
|
|
|
78
89
|
database?: object // for "Transforms"
|
|
79
90
|
skipOpen?: boolean // Prevents opening the dashboard in a browser
|
|
80
91
|
proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
|
|
81
|
-
allowedExt?: RegExp // /\.(json|txt|md|mjs)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
92
|
+
allowedExt?: RegExp // /\.(json|txt|md|js|mjs)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
82
93
|
}
|
|
83
94
|
```
|
|
84
95
|
|
package/Tests.js
CHANGED
|
@@ -96,6 +96,9 @@ write('api/.GET.500.txt', 'keeps non-autogenerated 500')
|
|
|
96
96
|
write('api/alternative(comment-2).GET.200.json', JSON.stringify({ comment: 2 }))
|
|
97
97
|
write('api/my-route(comment-2).GET.200.json', JSON.stringify({ comment: 2 }))
|
|
98
98
|
|
|
99
|
+
// JavaScript to JSON
|
|
100
|
+
write('/api/object.GET.200.js', 'export default { JSON_FROM_JS: true }')
|
|
101
|
+
|
|
99
102
|
writeStatic('index.html', '<h1>Static</h1>')
|
|
100
103
|
writeStatic('assets/app.js', 'const app = 1')
|
|
101
104
|
writeStatic('another-entry/index.html', '<h1>Another</h1>')
|
|
@@ -118,6 +121,8 @@ async function runTests() {
|
|
|
118
121
|
for (const [url, file, body] of fixtures)
|
|
119
122
|
await testMockDispatching(url, file, body)
|
|
120
123
|
|
|
124
|
+
await testMockDispatching('/api/object', 'api/object.GET.200.js', { JSON_FROM_JS: true }, undefined, mimeFor('.json'))
|
|
125
|
+
|
|
121
126
|
await testItUpdatesDelayAndFile(
|
|
122
127
|
'/api/alternative',
|
|
123
128
|
'api/alternative(comment-2).GET.200.json',
|
|
@@ -185,9 +190,9 @@ async function test404() {
|
|
|
185
190
|
})
|
|
186
191
|
}
|
|
187
192
|
|
|
188
|
-
async function testMockDispatching(url, file, expectedBody, reqBody = void 0) {
|
|
193
|
+
async function testMockDispatching(url, file, expectedBody, reqBody = void 0, forcedMime = void 0) {
|
|
189
194
|
const { urlMask, method, status } = Route.parseFilename(file)
|
|
190
|
-
const mime = mimeFor(file)
|
|
195
|
+
const mime = forcedMime || mimeFor(file)
|
|
191
196
|
const now = new Date()
|
|
192
197
|
const res = await request(url, { method, body: reqBody })
|
|
193
198
|
const body = mime === 'application/json'
|
package/package.json
CHANGED
|
File without changes
|
package/src/Config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, lstatSync } from 'node:fs'
|
|
1
|
+
import { existsSync as exists, lstatSync } from 'node:fs'
|
|
2
2
|
import { validate, is, optional } from './utils/validate.js'
|
|
3
3
|
|
|
4
4
|
|
|
@@ -12,7 +12,7 @@ export const Config = {
|
|
|
12
12
|
database: {},
|
|
13
13
|
skipOpen: false,
|
|
14
14
|
proxyFallback: '', // e.g. http://localhost:9999
|
|
15
|
-
allowedExt: /\.(json|txt|md|mjs)$/ // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
15
|
+
allowedExt: /\.(json|txt|md|js|mjs)$/ // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function setup(options) {
|
|
@@ -32,7 +32,7 @@ export function setup(options) {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
function isDirectory(dir) {
|
|
35
|
-
return
|
|
35
|
+
return exists(dir) && lstatSync(dir).isDirectory()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
|
package/src/MockDispatcher.js
CHANGED
|
@@ -31,15 +31,19 @@ export async function dispatchMock(req, response) {
|
|
|
31
31
|
const { file, status, delay, currentTransform } = broker
|
|
32
32
|
console.log('\n', req.url, '→\n ', file)
|
|
33
33
|
|
|
34
|
+
const shouldJavaScriptToJSON = file.endsWith('.js')
|
|
34
35
|
response.statusCode = status
|
|
35
|
-
response.setHeader('content-type', mimeFor(file))
|
|
36
|
+
response.setHeader('content-type', mimeFor(shouldJavaScriptToJSON ? '.json' : file))
|
|
36
37
|
if (cookie.getCurrent())
|
|
37
38
|
response.setHeader('set-cookie', cookie.getCurrent())
|
|
38
39
|
|
|
39
|
-
let mockAsText =
|
|
40
|
+
let mockAsText = shouldJavaScriptToJSON
|
|
41
|
+
? JSON.stringify(await importDefault(file))
|
|
42
|
+
: readMock(file)
|
|
43
|
+
|
|
40
44
|
if (broker.currentTransform) {
|
|
41
45
|
const body = await requestBodyForTransform(req, mockAsText)
|
|
42
|
-
const transformFunc = await
|
|
46
|
+
const transformFunc = await importDefault(currentTransform)
|
|
43
47
|
mockAsText = transformFunc(mockAsText, body, Config)
|
|
44
48
|
}
|
|
45
49
|
setTimeout(() => response.end(mockAsText), delay)
|
|
@@ -69,7 +73,7 @@ function readMock(file) {
|
|
|
69
73
|
return readFileSync(join(Config.mocksDir, file), 'utf8')
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
async function
|
|
76
|
+
async function importDefault(file) {
|
|
73
77
|
// The date param is just for cache busting
|
|
74
78
|
return (await import(join(Config.mocksDir, file) + '?' + Date.now())).default
|
|
75
79
|
}
|