mockaton 13.6.4 → 13.7.1

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 CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "HTTP Mock Server",
4
4
  "type": "module",
5
- "version": "13.6.4",
5
+ "version": "13.7.1",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./index.js",
@@ -37,5 +37,3 @@ export const DEFAULT_MOCK_COMMENT = '(default)'
37
37
 
38
38
  export const EXT_UNKNOWN_MIME = 'unknown'
39
39
  export const EXT_EMPTY = 'empty'
40
-
41
- export const FILENAME_HEADER = 'Mockaton-File'
@@ -113,13 +113,15 @@ async function updatePayloadViewer(proxied, file, response) {
113
113
  else if (mime.startsWith('video/'))
114
114
  codeRef.elem.replaceChildren(r('video', {
115
115
  src: store.chosenLink.urlMask,
116
- controls: true
116
+ controls: true,
117
+ autoPlay: true
117
118
  }))
118
119
 
119
120
  else if (mime.startsWith('audio/'))
120
121
  codeRef.elem.replaceChildren(r('audio', {
121
122
  src: store.chosenLink.urlMask,
122
- controls: true
123
+ controls: true,
124
+ autoPlay: true
123
125
  }))
124
126
 
125
127
  else if (['text/html', 'application/pdf'].includes(mime))
@@ -107,15 +107,20 @@ class UrlMatcher {
107
107
  this.#urlRegex = this.#buildUrlRegex(file)
108
108
  }
109
109
 
110
+ #escapeRegex(str) {
111
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
112
+ }
113
+
110
114
  #buildUrlRegex(file) {
111
115
  let { urlMask } = parseFilename(file)
112
116
  urlMask = removeQueryStringAndFragment(urlMask)
117
+ urlMask = this.#escapeRegex(urlMask)
113
118
  urlMask = this.#disregardVariables(urlMask)
114
119
  return new RegExp('^' + urlMask + '/*$')
115
120
  }
116
121
 
117
- #disregardVariables(str) { // Stars out all parts that are in square brackets
118
- return str.replace(/\[.*?]/g, '[^/]+')
122
+ #disregardVariables(str) {
123
+ return str.replace(/\\\[.*?\\]/g, '[^/]+')
119
124
  }
120
125
 
121
126
  // Appending a '/' so URLs ending with variables don't match
@@ -7,7 +7,6 @@ import { parseFilename } from '../client/Filename.js'
7
7
  import { echoFilePlugin } from './MockDispatcherPlugins.js'
8
8
  import { brokerByRoute } from './mockBrokersCollection.js'
9
9
  import { config, calcDelay } from './config.js'
10
- import { FILENAME_HEADER } from '../client/ApiConstants.js'
11
10
 
12
11
 
13
12
  export async function dispatchMock(req, response) {
@@ -29,7 +28,9 @@ export async function dispatchMock(req, response) {
29
28
  return
30
29
  }
31
30
 
32
- response.setHeader(FILENAME_HEADER, broker.file)
31
+ response.on('finish', () => {
32
+ logger.normal('MOCK', req.url, broker.file)
33
+ })
33
34
 
34
35
  if (cookie.getCurrent())
35
36
  response.setHeader('Set-Cookie', cookie.getCurrent())
@@ -8,8 +8,7 @@ import { ServerResponse } from './utils/HttpServerResponse.js'
8
8
  import { setCorsHeaders, isPreflight } from './utils/http-cors.js'
9
9
  import { IncomingMessage, BodyReaderError, hasControlChars } from './utils/HttpIncomingMessage.js'
10
10
 
11
- import { API, FILENAME_HEADER } from '../client/ApiConstants.js'
12
-
11
+ import { API } from '../client/ApiConstants.js'
13
12
  import { cookie } from './cookie.js'
14
13
  import { config, setup } from './config.js'
15
14
  import { apiPatchReqs, apiGetReqs, CLIENT_DIR } from './Api.js'
@@ -49,15 +48,12 @@ export function Mockaton(options) {
49
48
 
50
49
  async function onRequest(req, response) {
51
50
  response.setHeader('Server', `Mockaton ${pkgJSON.version}`)
52
-
53
51
  response.on('error', logger.warn)
54
52
 
53
+ let handledByMockDispatcher = false
55
54
  response.on('finish', () => {
56
- const f = response.getHeader(FILENAME_HEADER)
57
- if (f)
58
- logger.normal('MOCK', req.url, f)
59
- else
60
- logger.verbose('API', response)
55
+ if (!handledByMockDispatcher)
56
+ logger.verbose('APP', response)
61
57
  })
62
58
 
63
59
  const url = req.url || ''
@@ -87,8 +83,10 @@ async function onRequest(req, response) {
87
83
  else if (method === 'GET' && apiGetReqs.has(pathname))
88
84
  apiGetReqs.get(pathname)(req, response)
89
85
 
90
- else
86
+ else {
87
+ handledByMockDispatcher = true
91
88
  await dispatchMock(req, response)
89
+ }
92
90
  }
93
91
  catch (error) {
94
92
  if (error instanceof BodyReaderError)
@@ -940,6 +940,15 @@ describe('Dynamic Params', () => {
940
940
  })
941
941
  })
942
942
 
943
+ test('Dynamic Params on partial segments', async () => {
944
+ const fx = new Fixture('dynamic-params-partial-[id]/foo.GET.200.txt')
945
+ await makeDirInMocks('dynamic-params-partial-[id]')
946
+ await fx.write()
947
+ const r = await request('/dynamic-params-partial-999/foo')
948
+ equal(await r.text(), fx.body)
949
+ await fx.delete()
950
+ })
951
+
943
952
 
944
953
  describe('Query String', () => {
945
954
  const fx0 = new Fixture('query-string?foo=[foo]&bar=[bar].GET.200.json')