mockaton 2.0.0 → 2.1.0

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.
Binary file
package/README.md CHANGED
@@ -16,8 +16,7 @@ be used for downloading a TAR of your XHR requests following that convention.
16
16
  ### Mock Variants
17
17
  Each route can have many mocks, which could either be:
18
18
  - Different response __status code__.
19
- - e.g. for testing error responses. As a side note, an _Internal Server
20
- Error_ mock is autogenerated for routes that have no 500.
19
+ - e.g. for testing error responses.
21
20
  - __Comment__ on the filename, which is anything within parentheses.
22
21
  - e.g. `api/user(my-comment).POST.201.json`
23
22
 
@@ -56,7 +55,7 @@ The best way to learn _Mockaton_ is by checking out this repo and
56
55
  exploring its [sample-mocks/](./sample-mocks) directory. Then, run
57
56
  [`./_usage_example.js`](./_usage_example.js) and you’ll see this dashboard:
58
57
 
59
- ![](./README-dashboard.png)
58
+ <img src="./README-dashboard.png" style="max-width:890px"/>
60
59
 
61
60
 
62
61
  ## Delay 🕓
@@ -98,6 +97,7 @@ interface Config {
98
97
  skipOpen?: boolean // Prevents opening the dashboard in a browser
99
98
  proxyFallback?: string // e.g. http://localhost:9999 Target for relaying routes without mocks
100
99
  allowedExt?: RegExp // /\.(json|txt|md|js)$/ Just for excluding temporary editor files (e.g. JetBrains appends a ~)
100
+ generate500?: boolean // autogenerates an Internal Server Error empty mock for routes that have no 500
101
101
  }
102
102
  ```
103
103
 
@@ -127,10 +127,9 @@ words, it’s useful if you only care about its payload.
127
127
  ### Extension
128
128
  `.Method.HttpResponseStatusCode.FileExt`
129
129
 
130
- The **file extension** can anything, but `.md` and `.mjs` are reserved
131
- for documentation, and mock processors (more on that later).
130
+ The **file extension** can be anything, but `.md` is reserved for documentation.
132
131
 
133
- The `Config.allowedExt` regex defaults to: `/\.(json|txt|md|mjs)$/`
132
+ The `Config.allowedExt` regex defaults to: `/\.(json|txt|md|js)$/`
134
133
 
135
134
 
136
135
  ### Dynamic Parameters
package/Tests.js CHANGED
@@ -110,7 +110,8 @@ const server = Mockaton({
110
110
  cookies: {
111
111
  userA: 'CookieA',
112
112
  userB: 'CookieB'
113
- }
113
+ },
114
+ generate500: true
114
115
  })
115
116
  server.on('listening', runTests)
116
117
 
package/index.d.ts CHANGED
@@ -7,10 +7,10 @@ interface Config {
7
7
  port?: number
8
8
  delay?: number
9
9
  cookies?: object
10
- database?: object
11
10
  skipOpen?: boolean
12
11
  proxyFallback?: string
13
12
  allowedExt?: RegExp
13
+ generate500?: boolean
14
14
  }
15
15
 
16
16
  export function Mockaton(options: Config): Server
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mockaton",
3
3
  "description": "A deterministic server-side for developing and testing frontend clients",
4
4
  "type": "module",
5
- "version": "2.0.0",
5
+ "version": "2.1.0",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
@@ -0,0 +1,7 @@
1
+ This is a plain text response for (/api/friends).
2
+
3
+ In this case, it’s for mocking up a 500 - Internal Server Error.
4
+
5
+ This file could have been empty, or some JSON if it had a `.json` extension.
6
+
7
+ By the way, on initialization an 500 is auto-generated for routes that don’t have a 500.
package/src/Api.js CHANGED
@@ -60,7 +60,6 @@ async function selectCookie(req, response) {
60
60
  }
61
61
 
62
62
  function reinitialize(_, response) {
63
- Config.database = {}
64
63
  mockBrokersCollection.init()
65
64
  sendOK(response)
66
65
  }
package/src/Config.js CHANGED
@@ -9,10 +9,10 @@ export const Config = {
9
9
  port: 0, // auto-assigned
10
10
  delay: 1200, // milliseconds
11
11
  cookies: {}, // defaults to the first kv
12
- database: {},
13
12
  skipOpen: false,
14
13
  proxyFallback: '', // e.g. http://localhost:9999
15
- allowedExt: /\.(json|txt|md|js)$/ // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
14
+ allowedExt: /\.(json|txt|md|js)$/, // Just for excluding temporary editor files (e.g. JetBrains appends a ~)
15
+ generate500: false
16
16
  }
17
17
 
18
18
  export function setup(options) {
@@ -24,10 +24,10 @@ export function setup(options) {
24
24
  port: port => Number.isInteger(port) && port >= 0 && port < 2 ** 16,
25
25
  delay: ms => Number.isInteger(ms) && ms > 0,
26
26
  cookies: is(Object),
27
- database: is(Object),
28
27
  skipOpen: is(Boolean),
29
28
  proxyFallback: is(String),
30
- allowedExt: is(RegExp)
29
+ allowedExt: is(RegExp),
30
+ generate500: is(Boolean)
31
31
  })
32
32
  }
33
33
 
@@ -32,7 +32,6 @@ export async function dispatchMock(req, response) {
32
32
  console.log('\n', req.url, '→\n ', file)
33
33
 
34
34
  response.statusCode = status
35
- response.setHeader('content-type', mimeFor(file))
36
35
  if (cookie.getCurrent())
37
36
  response.setHeader('set-cookie', cookie.getCurrent())
38
37
 
@@ -44,8 +43,10 @@ export async function dispatchMock(req, response) {
44
43
  ? jsExport(req, response)
45
44
  : JSON.stringify(jsExport)
46
45
  }
47
- else
46
+ else {
47
+ response.setHeader('content-type', mimeFor(file))
48
48
  mockText = readMock(file)
49
+ }
49
50
  setTimeout(() => response.end(mockText), delay)
50
51
  }
51
52
  catch (error) {
@@ -39,7 +39,9 @@ export function init() {
39
39
  else
40
40
  collection[method][urlMask].register(file)
41
41
  }
42
- forEachBroker(broker => broker.ensureItHas500())
42
+
43
+ if (Config.generate500)
44
+ forEachBroker(broker => broker.ensureItHas500())
43
45
  }
44
46
 
45
47
  function forEachBroker(fn) {
@@ -1,7 +0,0 @@
1
- This is a plain text response for (/api/user).
2
-
3
- In this case, it’s for mocking up a 500 - Internal Server Error.
4
-
5
- This file could have been empty, or some JSON if it had a `.json` extension.
6
-
7
- By the way, on initialization an 500 is auto-generated for routes that don’t have a 500.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes