mockaton 6.3.8 → 6.3.9
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/package.json +1 -1
- package/src/Api.js +4 -8
- package/src/Filename.js +12 -10
- package/src/MockDispatcher.js +2 -3
- package/src/mockBrokersCollection.js +3 -2
- package/src/utils/http-response.js +5 -4
- package/src/utils/openInBrowser.js +8 -2
package/package.json
CHANGED
package/src/Api.js
CHANGED
|
@@ -50,8 +50,7 @@ async function selectCookie(req, response) {
|
|
|
50
50
|
sendOK(response)
|
|
51
51
|
}
|
|
52
52
|
catch (error) {
|
|
53
|
-
|
|
54
|
-
sendBadRequest(response)
|
|
53
|
+
sendBadRequest(response, error)
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
|
|
@@ -69,8 +68,7 @@ async function updateBroker(req, response) {
|
|
|
69
68
|
sendOK(response)
|
|
70
69
|
}
|
|
71
70
|
catch (error) {
|
|
72
|
-
|
|
73
|
-
sendBadRequest(response)
|
|
71
|
+
sendBadRequest(response, error)
|
|
74
72
|
}
|
|
75
73
|
}
|
|
76
74
|
|
|
@@ -80,8 +78,7 @@ async function updateProxyFallback(req, response) {
|
|
|
80
78
|
sendOK(response)
|
|
81
79
|
}
|
|
82
80
|
catch (error) {
|
|
83
|
-
|
|
84
|
-
sendBadRequest(response)
|
|
81
|
+
sendBadRequest(response, error)
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
84
|
|
|
@@ -91,7 +88,6 @@ async function bulkUpdateBrokersByCommentTag(req, response) {
|
|
|
91
88
|
sendOK(response)
|
|
92
89
|
}
|
|
93
90
|
catch (error) {
|
|
94
|
-
|
|
95
|
-
sendBadRequest(response)
|
|
91
|
+
sendBadRequest(response, error)
|
|
96
92
|
}
|
|
97
93
|
}
|
package/src/Filename.js
CHANGED
|
@@ -14,24 +14,26 @@ export const includesComment = (filename, search) =>
|
|
|
14
14
|
extractComments(filename).some(comment => comment.includes(search))
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
export function
|
|
17
|
+
export function validateFilename(file) {
|
|
18
18
|
const tokens = file.replace(reComments, '').split('.')
|
|
19
19
|
if (tokens.length < 4)
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
const method = tokens.at(-3)
|
|
23
|
-
const status = Number(tokens.at(-2))
|
|
20
|
+
return 'Invalid Filename Convention'
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
return { error: `Unrecognized HTTP Method: "${method}"` }
|
|
22
|
+
const { status, method } = parseFilename(file)
|
|
27
23
|
|
|
28
24
|
if (!responseStatusIsValid(status))
|
|
29
|
-
return
|
|
25
|
+
return `Invalid HTTP Response Status: "${status}"`
|
|
26
|
+
|
|
27
|
+
if (!httpMethods.includes(method))
|
|
28
|
+
return `Unrecognized HTTP Method: "${method}"`
|
|
29
|
+
}
|
|
30
30
|
|
|
31
|
+
export function parseFilename(file) {
|
|
32
|
+
const tokens = file.replace(reComments, '').split('.')
|
|
31
33
|
return {
|
|
32
34
|
urlMask: '/' + removeTrailingSlash(tokens.slice(0, -3).join('.')),
|
|
33
|
-
method,
|
|
34
|
-
status
|
|
35
|
+
method: tokens.at(-3),
|
|
36
|
+
status: Number(tokens.at(-2))
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
|
package/src/MockDispatcher.js
CHANGED
|
@@ -41,13 +41,12 @@ export async function dispatchMock(req, response) {
|
|
|
41
41
|
setTimeout(() => response.end(mockText), delay)
|
|
42
42
|
}
|
|
43
43
|
catch (error) {
|
|
44
|
-
console.error(error)
|
|
45
44
|
if (error instanceof JsonBodyParserError)
|
|
46
|
-
sendBadRequest(response)
|
|
45
|
+
sendBadRequest(response, error)
|
|
47
46
|
else if (error.code === 'ENOENT')
|
|
48
47
|
sendNotFound(response) // file has been deleted
|
|
49
48
|
else
|
|
50
|
-
sendInternalServerError(response)
|
|
49
|
+
sendInternalServerError(response, error)
|
|
51
50
|
}
|
|
52
51
|
}
|
|
53
52
|
|
|
@@ -5,7 +5,7 @@ import { Config } from './Config.js'
|
|
|
5
5
|
import { cookie } from './cookie.js'
|
|
6
6
|
import { isFile } from './utils/fs.js'
|
|
7
7
|
import { MockBroker } from './MockBroker.js'
|
|
8
|
-
import { parseFilename } from './Filename.js'
|
|
8
|
+
import { parseFilename, validateFilename } from './Filename.js'
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -29,11 +29,12 @@ export function init() {
|
|
|
29
29
|
.sort()
|
|
30
30
|
|
|
31
31
|
for (const file of files) {
|
|
32
|
-
const
|
|
32
|
+
const error = validateFilename(file)
|
|
33
33
|
if (error) {
|
|
34
34
|
console.error(error, file)
|
|
35
35
|
continue
|
|
36
36
|
}
|
|
37
|
+
const { method, urlMask } = parseFilename(file)
|
|
37
38
|
collection[method] ??= {}
|
|
38
39
|
if (!collection[method][urlMask])
|
|
39
40
|
collection[method][urlMask] = new MockBroker(file)
|
|
@@ -42,14 +42,14 @@ export async function sendPartialContent(response, range, file) {
|
|
|
42
42
|
this.pipe(response)
|
|
43
43
|
})
|
|
44
44
|
reader.on('error', function (error) {
|
|
45
|
-
|
|
46
|
-
sendInternalServerError(response)
|
|
45
|
+
sendInternalServerError(response, error)
|
|
47
46
|
})
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
|
|
52
|
-
export function sendBadRequest(response) {
|
|
51
|
+
export function sendBadRequest(response, error) {
|
|
52
|
+
console.error(error);
|
|
53
53
|
response.statusCode = 400
|
|
54
54
|
response.end()
|
|
55
55
|
}
|
|
@@ -64,7 +64,8 @@ export function sendUnprocessableContent(response) {
|
|
|
64
64
|
response.end()
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
export function sendInternalServerError(response) {
|
|
67
|
+
export function sendInternalServerError(response, error) {
|
|
68
|
+
console.error(error);
|
|
68
69
|
response.statusCode = 500
|
|
69
70
|
response.end()
|
|
70
71
|
}
|
|
@@ -2,7 +2,13 @@ import { exec } from 'node:child_process'
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
export function openInBrowser(address) {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
switch (process.platform) {
|
|
6
|
+
case 'darwin':
|
|
7
|
+
exec(`open ${address}`)
|
|
8
|
+
break
|
|
9
|
+
case 'win32': // TESTME
|
|
10
|
+
exec(`start ${address}`)
|
|
11
|
+
break
|
|
12
|
+
}
|
|
7
13
|
}
|
|
8
14
|
|