mockaton 12.5.0 → 12.5.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": "12.5.0",
5
+ "version": "12.5.1",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./index.js",
@@ -44,13 +44,17 @@ export function parseFilename(file) {
44
44
  }
45
45
  }
46
46
 
47
- function removeTrailingSlash(url = '') {
47
+ export function removeTrailingSlash(url = '') {
48
48
  return url
49
49
  .replace(/\/$/, '')
50
50
  .replace('/?', '?')
51
51
  .replace('/#', '#')
52
52
  }
53
53
 
54
+ export function removeQueryStringAndFragment(url = '') {
55
+ return new URL(url, 'http://_').pathname
56
+ }
57
+
54
58
  function responseStatusIsValid(status) {
55
59
  return Number.isInteger(status)
56
60
  && status >= 100
@@ -1,5 +1,5 @@
1
1
  import { DEFAULT_MOCK_COMMENT } from '../client/ApiConstants.js'
2
- import { parseFilename, includesComment, extractComments } from '../client/Filename.js'
2
+ import { parseFilename, includesComment, extractComments, removeQueryStringAndFragment } from '../client/Filename.js'
3
3
 
4
4
 
5
5
  /**
@@ -105,15 +105,11 @@ class UrlMatcher {
105
105
 
106
106
  #buildUrlRegex(file) {
107
107
  let { urlMask } = parseFilename(file)
108
- urlMask = this.#removeQueryStringAndFragment(urlMask)
108
+ urlMask = removeQueryStringAndFragment(urlMask)
109
109
  urlMask = this.#disregardVariables(urlMask)
110
110
  return new RegExp('^' + urlMask + '/*$')
111
111
  }
112
112
 
113
- #removeQueryStringAndFragment(str) {
114
- return str.replace(/[?#].*/, '')
115
- }
116
-
117
113
  #disregardVariables(str) { // Stars out all parts that are in square brackets
118
114
  return str.replace(/\[.*?]/g, '[^/]+')
119
115
  }
@@ -127,7 +123,7 @@ class UrlMatcher {
127
123
  // slashes. For instance, for routing api/foo/[id]?qs…
128
124
  urlMaskMatches = (url) => {
129
125
  let u = decodeURIComponent(url)
130
- u = this.#removeQueryStringAndFragment(u)
126
+ u = removeQueryStringAndFragment(u)
131
127
  u += '/'
132
128
  return this.#urlRegex.test(u)
133
129
  }
@@ -1,6 +1,7 @@
1
1
  import { relative } from 'node:path'
2
2
  import { config } from '../config.js'
3
- import { parseFilename } from '../../client/Filename.js'
3
+ import { decode } from './HttpIncomingMessage.js'
4
+ import { parseFilename, removeTrailingSlash, removeQueryStringAndFragment } from '../../client/Filename.js'
4
5
 
5
6
 
6
7
  export function parseQueryParams(url) {
@@ -11,14 +12,16 @@ export function parseSplats(url, filename) {
11
12
  const { urlMask } = parseFilename(relative(config.mocksDir, filename))
12
13
 
13
14
  const splats = []
14
- const pattern = urlMask
15
+ const pattern = removeQueryStringAndFragment(decode(urlMask))
15
16
  .replace(/\[(.+?)]/g, (_, name) => {
16
17
  splats.push(name)
17
18
  return '([^/]+)'
18
19
  })
19
- .replace(/\//g, '\\/')
20
20
 
21
- const match = url.match(new RegExp(`^${pattern}$`))
21
+ let u = removeQueryStringAndFragment(url)
22
+ u = removeTrailingSlash(u)
23
+
24
+ const match = u.match(new RegExp(`^${pattern}$`))
22
25
  if (!match)
23
26
  return {}
24
27
 
@@ -1,4 +1,4 @@
1
- import { test } from 'node:test'
1
+ import { test, describe } from 'node:test'
2
2
  import { deepEqual, equal } from 'node:assert/strict'
3
3
  import { parseSplats, parseQueryParams } from './UrlParsers.js'
4
4
  import { config } from '../config.js'
@@ -9,14 +9,46 @@ test('parseQueryParams', () => {
9
9
  })
10
10
 
11
11
 
12
- test('parseSplats', () => {
13
- const p = parseSplats(
14
- '/api/company/123/user/456',
15
- `${config.mocksDir}/api/company/[companyId]/user/[userId](comments).GET.200.js`
16
- )
17
- deepEqual(p, {
18
- companyId: '123',
19
- userId: '456',
12
+ describe('parseSplats', () => {
13
+ test('one splat', () => {
14
+ const splats = parseSplats(
15
+ '/api/company/123',
16
+ `${config.mocksDir}/api/company/[companyId].GET.200.js`
17
+ )
18
+ deepEqual(splats, {
19
+ companyId: '123'
20
+ })
21
+ })
22
+
23
+ test('one splat with trailing slash', () => {
24
+ const splats = parseSplats(
25
+ '/api/company/123/',
26
+ `${config.mocksDir}/api/company/[companyId].GET.200.js`
27
+ )
28
+ deepEqual(splats, {
29
+ companyId: '123'
30
+ })
31
+ })
32
+
33
+ test('two splats and comment', () => {
34
+ const splats = parseSplats(
35
+ '/api/company/123/user/456',
36
+ `${config.mocksDir}/api/company/[companyId]/user/[userId](comments).GET.200.js`
37
+ )
38
+ deepEqual(splats, {
39
+ companyId: '123',
40
+ userId: '456',
41
+ })
42
+ })
43
+
44
+ test('ignores query string', () => {
45
+ const splats = parseSplats(
46
+ '/api/company/123?foo=456',
47
+ `${config.mocksDir}/api/company/[companyId]?foo=[fooId].GET.200.js`
48
+ )
49
+ deepEqual(splats, {
50
+ companyId: '123'
51
+ })
20
52
  })
21
53
  })
22
54