mockaton 6.3.9 → 6.3.10
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/Tests.js +5 -4
- package/package.json +1 -1
- package/src/Filename.js +8 -1
- package/src/MockBroker.js +3 -1
- package/src/mockBrokersCollection.js +6 -14
- package/src/utils/fs.js +5 -1
package/Tests.js
CHANGED
|
@@ -17,6 +17,7 @@ import { API, DF, DEFAULT_500_COMMENT } from './src/ApiConstants.js'
|
|
|
17
17
|
|
|
18
18
|
const tmpDir = mkdtempSync(tmpdir()) + '/'
|
|
19
19
|
const staticTmpDir = mkdtempSync(tmpdir()) + '/'
|
|
20
|
+
console.log(tmpDir)
|
|
20
21
|
|
|
21
22
|
const fixtureCustomMime = [
|
|
22
23
|
'/api/custom-mime',
|
|
@@ -380,12 +381,12 @@ async function testInvalidFilenamesAreIgnored() {
|
|
|
380
381
|
consoleErrorSpy.mock.mockImplementation(() => {}) // so they don’t render in the test report
|
|
381
382
|
|
|
382
383
|
write('api/_INVALID_FILENAME_CONVENTION_.json', '')
|
|
383
|
-
write('api/bad-filename.
|
|
384
|
-
write('api/bad-filename.
|
|
384
|
+
write('api/bad-filename-method._INVALID_METHOD_.200.json', '')
|
|
385
|
+
write('api/bad-filename-status.GET._INVALID_STATUS_.json', '')
|
|
385
386
|
await reset()
|
|
386
387
|
equal(consoleErrorSpy.mock.calls[0].arguments[0], 'Invalid Filename Convention')
|
|
387
|
-
equal(consoleErrorSpy.mock.calls[1].arguments[0], '
|
|
388
|
-
equal(consoleErrorSpy.mock.calls[2].arguments[0], '
|
|
388
|
+
equal(consoleErrorSpy.mock.calls[1].arguments[0], 'Unrecognized HTTP Method: "_INVALID_METHOD_"')
|
|
389
|
+
equal(consoleErrorSpy.mock.calls[2].arguments[0], 'Invalid HTTP Response Status: "NaN"')
|
|
389
390
|
})
|
|
390
391
|
}
|
|
391
392
|
|
package/package.json
CHANGED
package/src/Filename.js
CHANGED
|
@@ -14,7 +14,14 @@ export const includesComment = (filename, search) =>
|
|
|
14
14
|
extractComments(filename).some(comment => comment.includes(search))
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
export function
|
|
17
|
+
export function filenameIsValid(file) {
|
|
18
|
+
const error = validateFilename(file)
|
|
19
|
+
if (error)
|
|
20
|
+
console.error(error, file)
|
|
21
|
+
return !error
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function validateFilename(file) {
|
|
18
25
|
const tokens = file.replace(reComments, '').split('.')
|
|
19
26
|
if (tokens.length < 4)
|
|
20
27
|
return 'Invalid Filename Convention'
|
package/src/MockBroker.js
CHANGED
|
@@ -41,7 +41,6 @@ export class MockBroker {
|
|
|
41
41
|
get file() { return this.currentMock.file }
|
|
42
42
|
get delay() { return this.currentMock.delay }
|
|
43
43
|
get status() { return parseFilename(this.file).status }
|
|
44
|
-
get isTemp500() { return includesComment(this.file, DEFAULT_500_COMMENT) }
|
|
45
44
|
|
|
46
45
|
updateFile(filename) {
|
|
47
46
|
this.currentMock.file = filename
|
|
@@ -78,6 +77,9 @@ export class MockBroker {
|
|
|
78
77
|
const file = urlMask.replace(/^\//, '') // Removes leading slash TESTME
|
|
79
78
|
this.register(`${file}${DEFAULT_500_COMMENT}.${method}.500.txt`)
|
|
80
79
|
}
|
|
80
|
+
get isTemp500() {
|
|
81
|
+
return includesComment(this.file, DEFAULT_500_COMMENT)
|
|
82
|
+
}
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
// Stars out (for regex) all the paths that are in square brackets
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { join } from 'node:path'
|
|
2
|
-
import { readdirSync as readDir } from 'node:fs'
|
|
3
|
-
|
|
4
1
|
import { Config } from './Config.js'
|
|
5
2
|
import { cookie } from './cookie.js'
|
|
6
|
-
import { isFile } from './utils/fs.js'
|
|
7
3
|
import { MockBroker } from './MockBroker.js'
|
|
8
|
-
import {
|
|
4
|
+
import { listFilesRecursively } from './utils/fs.js'
|
|
5
|
+
import { parseFilename, filenameIsValid } from './Filename.js'
|
|
9
6
|
|
|
10
7
|
|
|
11
8
|
/**
|
|
@@ -24,16 +21,11 @@ export function init() {
|
|
|
24
21
|
collection = {}
|
|
25
22
|
cookie.init(Config.cookies)
|
|
26
23
|
|
|
27
|
-
const files =
|
|
28
|
-
.filter(f => !Config.ignore.test(f) && isFile(join(Config.mocksDir, f)))
|
|
24
|
+
const files = listFilesRecursively(Config.mocksDir)
|
|
29
25
|
.sort()
|
|
26
|
+
.filter(f => !Config.ignore.test(f) && filenameIsValid(f))
|
|
30
27
|
|
|
31
28
|
for (const file of files) {
|
|
32
|
-
const error = validateFilename(file)
|
|
33
|
-
if (error) {
|
|
34
|
-
console.error(error, file)
|
|
35
|
-
continue
|
|
36
|
-
}
|
|
37
29
|
const { method, urlMask } = parseFilename(file)
|
|
38
30
|
collection[method] ??= {}
|
|
39
31
|
if (!collection[method][urlMask])
|
|
@@ -75,8 +67,8 @@ export function getBrokerForUrl(method, url) {
|
|
|
75
67
|
export function extractAllComments() {
|
|
76
68
|
const comments = new Set()
|
|
77
69
|
forEachBroker(broker => {
|
|
78
|
-
for (const
|
|
79
|
-
comments.add(
|
|
70
|
+
for (const c of broker.extractComments())
|
|
71
|
+
comments.add(c)
|
|
80
72
|
})
|
|
81
73
|
return Array.from(comments)
|
|
82
74
|
}
|
package/src/utils/fs.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { lstatSync, readFileSync, readdirSync } from 'node:fs'
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
export const isFile = path => lstatSync(path, { throwIfNoEntry: false })?.isFile()
|
|
5
6
|
export const isDirectory = path => lstatSync(path, { throwIfNoEntry: false })?.isDirectory()
|
|
6
7
|
|
|
7
8
|
export const read = path => readFileSync(path)
|
|
9
|
+
|
|
10
|
+
export const listFilesRecursively = dir => readdirSync(dir, { recursive: true })
|
|
11
|
+
.filter(f => isFile(join(dir, f)))
|