mockaton 6.3.5 → 6.3.7
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 +13 -5
- package/package.json +1 -1
- package/src/MockDispatcher.js +2 -2
- package/src/Route.js +8 -7
- package/src/utils/http-response.js +1 -1
- package/src/{mime.js → utils/mime.js} +1 -1
package/Tests.js
CHANGED
|
@@ -9,7 +9,8 @@ import { equal, deepEqual, match } from 'node:assert/strict'
|
|
|
9
9
|
import { writeFileSync, mkdtempSync, mkdirSync } from 'node:fs'
|
|
10
10
|
|
|
11
11
|
import { Route } from './src/Route.js'
|
|
12
|
-
import {
|
|
12
|
+
import { Config } from './src/Config.js'
|
|
13
|
+
import { mimeFor } from './src/utils/mime.js'
|
|
13
14
|
import { Mockaton } from './src/Mockaton.js'
|
|
14
15
|
import { API, DF, DEFAULT_500_COMMENT } from './src/ApiConstants.js'
|
|
15
16
|
|
|
@@ -51,6 +52,14 @@ const fixtures = [
|
|
|
51
52
|
'/api/alternative',
|
|
52
53
|
'api/alternative(comment-1).GET.200.json',
|
|
53
54
|
'With_Comment_1'
|
|
55
|
+
], [
|
|
56
|
+
'/api/dot.in.path',
|
|
57
|
+
'api/dot.in.path.GET.200.json',
|
|
58
|
+
'Dot_in_Path'
|
|
59
|
+
], [
|
|
60
|
+
'/api/space & colon:',
|
|
61
|
+
'api/space & colon:.GET.200.json',
|
|
62
|
+
'Decodes URI'
|
|
54
63
|
],
|
|
55
64
|
|
|
56
65
|
|
|
@@ -115,6 +124,7 @@ writeStatic('another-entry/index.html', '<h1>Another</h1>')
|
|
|
115
124
|
const server = Mockaton({
|
|
116
125
|
mocksDir: tmpDir,
|
|
117
126
|
staticDir: staticTmpDir,
|
|
127
|
+
delay: 40,
|
|
118
128
|
onReady: () => {},
|
|
119
129
|
cookies: {
|
|
120
130
|
userA: 'CookieA',
|
|
@@ -209,10 +219,9 @@ async function test404() {
|
|
|
209
219
|
})
|
|
210
220
|
}
|
|
211
221
|
|
|
212
|
-
async function testMockDispatching(url, file, expectedBody, forcedMime =
|
|
222
|
+
async function testMockDispatching(url, file, expectedBody, forcedMime = undefined) {
|
|
213
223
|
const { urlMask, method, status } = Route.parseFilename(file)
|
|
214
224
|
const mime = forcedMime || mimeFor(file)
|
|
215
|
-
const now = new Date()
|
|
216
225
|
const res = await request(url, { method })
|
|
217
226
|
const body = mime === 'application/json'
|
|
218
227
|
? await res.json()
|
|
@@ -222,7 +231,6 @@ async function testMockDispatching(url, file, expectedBody, forcedMime = void 0)
|
|
|
222
231
|
it('mime: ' + mime, () => equal(res.headers.get('content-type'), mime))
|
|
223
232
|
it('status: ' + status, () => equal(res.status, status))
|
|
224
233
|
it('cookie: ' + mime, () => equal(res.headers.get('set-cookie'), 'CookieA'))
|
|
225
|
-
it('delay is under 1 sec', () => equal((new Date()).getTime() - now.getTime() < 1000, true))
|
|
226
234
|
it('extra header', () => equal(res.headers.get('server'), 'MockatonTester'))
|
|
227
235
|
})
|
|
228
236
|
}
|
|
@@ -253,7 +261,7 @@ async function testItUpdatesDelayAndFile(url, file, expectedBody) {
|
|
|
253
261
|
const body = await res.text()
|
|
254
262
|
await describe('url: ' + url, () => {
|
|
255
263
|
it('body is: ' + expectedBody, () => equal(body, expectedBody))
|
|
256
|
-
it('delay
|
|
264
|
+
it('delay', () => equal((new Date()).getTime() - now.getTime() > Config.delay, true))
|
|
257
265
|
})
|
|
258
266
|
}
|
|
259
267
|
|
package/package.json
CHANGED
package/src/MockDispatcher.js
CHANGED
|
@@ -4,7 +4,7 @@ import { readFileSync } from 'node:fs'
|
|
|
4
4
|
import { proxy } from './ProxyRelay.js'
|
|
5
5
|
import { cookie } from './cookie.js'
|
|
6
6
|
import { Config } from './Config.js'
|
|
7
|
-
import { mimeFor } from './mime.js'
|
|
7
|
+
import { mimeFor } from './utils/mime.js'
|
|
8
8
|
import * as mockBrokerCollection from './mockBrokersCollection.js'
|
|
9
9
|
import { JsonBodyParserError } from './utils/http-request.js'
|
|
10
10
|
import { sendInternalServerError, sendNotFound, sendBadRequest } from './utils/http-response.js'
|
|
@@ -22,7 +22,7 @@ export async function dispatchMock(req, response) {
|
|
|
22
22
|
|
|
23
23
|
try {
|
|
24
24
|
const { file, status, delay } = broker
|
|
25
|
-
console.log(req.url, ' → ', file)
|
|
25
|
+
console.log(decodeURIComponent(req.url), ' → ', file)
|
|
26
26
|
|
|
27
27
|
let mockText
|
|
28
28
|
if (file.endsWith('.js')) {
|
package/src/Route.js
CHANGED
|
@@ -26,7 +26,7 @@ export class Route {
|
|
|
26
26
|
// api/foo/[route_id]/suffix => api/foo/.*/suffix
|
|
27
27
|
// By the same token, the regex handles many trailing
|
|
28
28
|
// slashes. For instance, for routing api/foo/[id]?qs…
|
|
29
|
-
return this.#urlRegex.test(removeQueryStringAndFragment(url) + '/')
|
|
29
|
+
return this.#urlRegex.test(removeQueryStringAndFragment(decodeURIComponent(url)) + '/')
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// Anything within parentheses in the filename is a comment, including the parentheses.
|
|
@@ -46,16 +46,17 @@ export class Route {
|
|
|
46
46
|
if (tokens.length < 4)
|
|
47
47
|
return { error: 'Invalid Filename Convention' }
|
|
48
48
|
|
|
49
|
+
const method = tokens.at(-3)
|
|
49
50
|
const status = Number(tokens.at(-2))
|
|
50
|
-
if (!responseStatusIsValid(status))
|
|
51
|
-
return { error: `Invalid HTTP Response Status: "${status}"` }
|
|
52
51
|
|
|
53
|
-
const method = tokens.at(-3)
|
|
54
52
|
if (!httpMethods.includes(method))
|
|
55
53
|
return { error: `Unrecognized HTTP Method: "${method}"` }
|
|
56
54
|
|
|
55
|
+
if (!responseStatusIsValid(status))
|
|
56
|
+
return { error: `Invalid HTTP Response Status: "${status}"` }
|
|
57
|
+
|
|
57
58
|
return {
|
|
58
|
-
urlMask: '/' + removeTrailingSlash(tokens.
|
|
59
|
+
urlMask: '/' + removeTrailingSlash(tokens.slice(0, -3).join('.')),
|
|
59
60
|
method,
|
|
60
61
|
status
|
|
61
62
|
}
|
|
@@ -73,10 +74,10 @@ function removeQueryStringAndFragment(urlMask) {
|
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
function removeTrailingSlash(url = '') {
|
|
76
|
-
return
|
|
77
|
+
return url
|
|
77
78
|
.replace(/\/$/, '')
|
|
78
79
|
.replace('/?', '?')
|
|
79
|
-
.replace('/#', '#')
|
|
80
|
+
.replace('/#', '#')
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
function responseStatusIsValid(status) {
|