mockaton 2.3.0 → 2.3.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Mockaton
2
2
  _Mockaton_ is a mock server for developing and testing frontends.
3
3
 
4
- It scans `Config.mocksDir` for files following a specific
4
+ It scans a given directory for files following a specific
5
5
  file name convention, which is similar to the URL paths. For
6
6
  example, the following file will be served for `/api/user/1234`
7
7
  ```
@@ -13,60 +13,16 @@ extension](https://github.com/ericfortis/devtools-ext-tar-http-requests) can
13
13
  be used for downloading a TAR of your XHR requests following that convention.
14
14
 
15
15
 
16
- ### Mock Variants
17
- Each route can have many mocks, which could either be:
18
- - Different response __status code__.
19
- - e.g. for testing error responses.
20
- - __Comment__ on the filename, which is anything within parentheses.
21
- - e.g. `api/user(my-comment).POST.201.json`
22
-
23
- Those alternatives can be manually selected in the dashboard
24
- UI, or programmatically, for instance, for setting up tests.
25
-
26
- The first file in **alphabetical order** becomes the default mock.
27
-
28
- ### Optionally, you can write mocks in JavaScript
29
- An Object, Array, or String is sent as JSON.
30
-
31
- `api/foo.GET.200.js`
32
- ```js
33
- export default [
34
- { id: 0 }
35
- ]
36
- ```
37
-
38
- Or, export default a function. There, you
39
- can override the response status and the default JSON content
40
- type. But don’t call `response.end()`, just return a string.
41
- ```js
42
- export default function (req, response) {
43
- return JSON.stringify({ a: 1 })
44
- }
45
- ```
46
-
47
-
48
- ### Proxying Routes
49
- `Config.proxyFallback` lets you specify a target
50
- server for serving routes you don’t have mocks for.
51
-
52
-
53
16
  ## Getting Started
54
17
  The best way to learn _Mockaton_ is by checking out this repo and
55
18
  exploring its [sample-mocks/](./sample-mocks) directory. Then, run
56
19
  [`./_usage_example.js`](./_usage_example.js) and you’ll see this dashboard:
57
20
 
58
- <img src="./README-dashboard.png" style="max-width:890px"/>
59
-
60
-
61
- ## Delay 🕓
62
- The clock icon next to the mock selector is a checkbox for delaying a
63
- particular response. They are handy for testing spinners.
64
21
 
65
- The delay is globally configurable via `Config.delay = 1200` (milliseconds).
22
+ <img src="./README-dashboard.png" style="max-width:820px"/>
66
23
 
67
- ---
68
24
 
69
- ## Basic Usage (see [_usage_example.js](./_usage_example.js))
25
+ ## Basic Usage
70
26
  ```
71
27
  npm install mockaton
72
28
  ```
@@ -101,8 +57,54 @@ interface Config {
101
57
  extraHeaders?: []
102
58
  }
103
59
  ```
60
+
104
61
  ---
105
62
 
63
+ ## Mock Variants
64
+ Each route can have many mocks, which could either be:
65
+ - Different response __status code__.
66
+ - e.g. for testing error responses.
67
+ - __Comment__ on the filename, which is anything within parentheses.
68
+ - e.g. `api/user(my-comment).POST.201.json`
69
+
70
+ Those alternatives can be manually selected in the dashboard
71
+ UI, or programmatically, for instance, for setting up tests.
72
+
73
+ The first file in **alphabetical order** becomes the default mock.
74
+
75
+ ## You can write JSON mocks in JavaScript
76
+ An Object, Array, or String is sent as JSON.
77
+
78
+ `api/foo.GET.200.js`
79
+ ```js
80
+ export default [
81
+ { id: 0 }
82
+ ]
83
+ ```
84
+
85
+ Or, export default a function. There, you
86
+ can override the response status and the default JSON content
87
+ type. But don’t call `response.end()`, just return a string.
88
+ ```js
89
+ export default function (req, response) {
90
+ return JSON.stringify({ a: 1 })
91
+ }
92
+ ```
93
+
94
+
95
+ ## Proxying Routes
96
+ `Config.proxyFallback` lets you specify a target
97
+ server for serving routes you don’t have mocks for.
98
+
99
+
100
+
101
+ ## Delay 🕓
102
+ The clock icon next to the mock selector is a checkbox for delaying a
103
+ particular response. They are handy for testing spinners.
104
+
105
+ The delay is globally configurable via `Config.delay = 1200` (milliseconds).
106
+
107
+
106
108
  ## File Name Convention
107
109
 
108
110
 
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.3.0",
5
+ "version": "2.3.1",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Api.js CHANGED
@@ -9,7 +9,7 @@ import { Config } from './Config.js'
9
9
  import { DF, API } from './ApiConstants.js'
10
10
  import { parseJSON } from './utils/http-request.js'
11
11
  import * as mockBrokersCollection from './mockBrokersCollection.js'
12
- import { sendOK, sendBadRequest, sendJSON, sendFile } from './utils/http-response.js'
12
+ import { sendOK, sendBadRequest, sendJSON, sendFile, sendUnprocessableContent } from './utils/http-response.js'
13
13
 
14
14
 
15
15
  export const apiGetRequests = new Map([
@@ -24,11 +24,11 @@ export const apiGetRequests = new Map([
24
24
  ])
25
25
 
26
26
  export const apiPatchRequests = new Map([
27
- [API.bulkSelect, bulkUpdateBrokersByCommentTag],
28
27
  [API.edit, updateBroker],
29
28
  [API.reset, reinitialize],
30
29
  [API.cookies, selectCookie],
31
- [API.fallback, updateProxyFallback]
30
+ [API.fallback, updateProxyFallback],
31
+ [API.bulkSelect, bulkUpdateBrokersByCommentTag]
32
32
  ])
33
33
 
34
34
  function serveDashboard(_, response) {
@@ -48,6 +48,11 @@ function listMockBrokers(_, response) {
48
48
  }
49
49
 
50
50
 
51
+ function reinitialize(_, response) {
52
+ mockBrokersCollection.init()
53
+ sendOK(response)
54
+ }
55
+
51
56
  async function selectCookie(req, response) {
52
57
  try {
53
58
  cookie.setCurrent(await parseJSON(req))
@@ -59,15 +64,14 @@ async function selectCookie(req, response) {
59
64
  }
60
65
  }
61
66
 
62
- function reinitialize(_, response) {
63
- mockBrokersCollection.init()
64
- sendOK(response)
65
- }
66
-
67
67
  async function updateBroker(req, response) {
68
68
  try {
69
69
  const body = await parseJSON(req)
70
70
  const broker = mockBrokersCollection.getBrokerByFilename(body[DF.file])
71
+ if (!broker) {
72
+ sendUnprocessableContent(response)
73
+ return
74
+ }
71
75
  if (DF.delayed in body)
72
76
  broker.updateDelay(body[DF.delayed])
73
77
  broker.updateFile(body[DF.file])
@@ -79,9 +83,9 @@ async function updateBroker(req, response) {
79
83
  }
80
84
  }
81
85
 
82
- async function bulkUpdateBrokersByCommentTag(req, response) {
86
+ async function updateProxyFallback(req, response) {
83
87
  try {
84
- mockBrokersCollection.setMocksMatchingComment(await parseJSON(req))
88
+ Config.proxyFallback = await parseJSON(req)
85
89
  sendOK(response)
86
90
  }
87
91
  catch (error) {
@@ -90,9 +94,9 @@ async function bulkUpdateBrokersByCommentTag(req, response) {
90
94
  }
91
95
  }
92
96
 
93
- async function updateProxyFallback(req, response) {
97
+ async function bulkUpdateBrokersByCommentTag(req, response) {
94
98
  try {
95
- Config.proxyFallback = await parseJSON(req)
99
+ mockBrokersCollection.setMocksMatchingComment(await parseJSON(req))
96
100
  sendOK(response)
97
101
  }
98
102
  catch (error) {
package/src/Config.js CHANGED
@@ -26,7 +26,7 @@ export function setup(options) {
26
26
  delay: ms => Number.isInteger(ms) && ms > 0,
27
27
  cookies: is(Object),
28
28
  skipOpen: is(Boolean),
29
- proxyFallback: is(String),
29
+ proxyFallback: optional(URL.canParse),
30
30
  allowedExt: is(RegExp),
31
31
  generate500: is(Boolean),
32
32
  extraHeaders: Array.isArray
@@ -54,7 +54,8 @@ export const getAll = () => collection
54
54
 
55
55
  export const getBrokerByFilename = file => {
56
56
  const { method, urlMask } = Route.parseFilename(file)
57
- return collection[method][urlMask]
57
+ if (collection[method])
58
+ return collection[method][urlMask]
58
59
  }
59
60
 
60
61
  // Searching the routes in reverse order so dynamic params (e.g.
@@ -54,6 +54,11 @@ export function sendNotFound(response) {
54
54
  response.end()
55
55
  }
56
56
 
57
+ export function sendUnprocessableContent(response) {
58
+ response.statusCode = 422
59
+ response.end()
60
+ }
61
+
57
62
  export function sendInternalServerError(response) {
58
63
  response.statusCode = 500
59
64
  response.end()