mockaton 0.9.5 → 0.9.6
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/Api.js +1 -1
- package/ApiConstants.js +1 -3
- package/Dashboard.js +2 -10
- package/README.md +48 -43
- package/Route.js +1 -1
- package/Tests.js +0 -2
- package/_usage_example.js +2 -2
- package/package.json +1 -1
package/Api.js
CHANGED
|
@@ -97,7 +97,7 @@ async function bulkUpdateBrokersByCommentTag(req, response) {
|
|
|
97
97
|
async function updateBrokerTransform(req, response) {
|
|
98
98
|
try {
|
|
99
99
|
const body = await parseJSON(req)
|
|
100
|
-
const broker = mockBrokersCollection.
|
|
100
|
+
const broker = mockBrokersCollection.getBrokerByFilename(body[DF.file])
|
|
101
101
|
broker.updateTransform(body[DF.file])
|
|
102
102
|
sendOK(response)
|
|
103
103
|
}
|
package/ApiConstants.js
CHANGED
|
@@ -15,7 +15,5 @@ export const DF = { // Dashboard Fields (XHR)
|
|
|
15
15
|
delayed: 'delayed',
|
|
16
16
|
file: 'file',
|
|
17
17
|
currentCookieKey: 'current_cookie_key',
|
|
18
|
-
isForDashboard: 'mock_request_payload'
|
|
19
|
-
method: 'method',
|
|
20
|
-
urlMask: 'url_mask'
|
|
18
|
+
isForDashboard: 'mock_request_payload'
|
|
21
19
|
}
|
package/Dashboard.js
CHANGED
|
@@ -257,15 +257,13 @@ function Transforms({ brokersByMethod }) {
|
|
|
257
257
|
r('tr', null,
|
|
258
258
|
r('td', null, r(PreviewLink, { method: broker.method, urlMask })),
|
|
259
259
|
r('td', null, r(TransformSelector, {
|
|
260
|
-
urlMask,
|
|
261
|
-
method: broker.method,
|
|
262
260
|
items: ['', ...broker.transforms],
|
|
263
261
|
selected: broker.currentTransform
|
|
264
262
|
})))
|
|
265
263
|
)))
|
|
266
264
|
}
|
|
267
265
|
|
|
268
|
-
function TransformSelector({
|
|
266
|
+
function TransformSelector({ items, selected }) {
|
|
269
267
|
const className = defaultIsSelected => cssClass(
|
|
270
268
|
CSS.TransformSelector,
|
|
271
269
|
!defaultIsSelected && CSS.bold)
|
|
@@ -273,16 +271,10 @@ function TransformSelector({ method, urlMask, items, selected }) {
|
|
|
273
271
|
r('select', {
|
|
274
272
|
className: className(selected === items[0]),
|
|
275
273
|
autocomplete: 'off',
|
|
276
|
-
'data-urlMask': urlMask,
|
|
277
|
-
'data-method': method,
|
|
278
274
|
onChange() {
|
|
279
275
|
fetch(DP.transform, {
|
|
280
276
|
method: 'PATCH',
|
|
281
|
-
body: JSON.stringify({
|
|
282
|
-
[DF.file]: this.value,
|
|
283
|
-
[DF.urlMask]: this.getAttribute('data-urlMask'),
|
|
284
|
-
[DF.method]: this.getAttribute('data-method')
|
|
285
|
-
})
|
|
277
|
+
body: JSON.stringify({ [DF.file]: this.value })
|
|
286
278
|
}).then(() => {
|
|
287
279
|
this.closest('tr').querySelector('a').click()
|
|
288
280
|
this.className = className(this.value === this.options[0].value)
|
package/README.md
CHANGED
|
@@ -22,6 +22,8 @@ Each route can have many mocks, which could either be:
|
|
|
22
22
|
Those alternatives can be manually selected in the dashboard
|
|
23
23
|
UI, or programmatically, for instance, for setting up tests.
|
|
24
24
|
|
|
25
|
+
About the mock precedence, the first file in **alphabetical order** wins.
|
|
26
|
+
|
|
25
27
|
|
|
26
28
|
## Getting Started
|
|
27
29
|
The best way to learn _Mockaton_ is by checking out this repo and
|
|
@@ -102,7 +104,7 @@ Config.cookies = {
|
|
|
102
104
|
```
|
|
103
105
|
|
|
104
106
|
That `jwtCookie` has a hardcoded header and signature. In other
|
|
105
|
-
words, it’s useful iff you care about its payload in frontend.
|
|
107
|
+
words, it’s useful iff you care about its payload in the frontend.
|
|
106
108
|
|
|
107
109
|
---
|
|
108
110
|
|
|
@@ -153,28 +155,6 @@ api/foo/?bar=[bar].GET.200.json
|
|
|
153
155
|
api/foo/(my comment).GET.200.json
|
|
154
156
|
```
|
|
155
157
|
|
|
156
|
-
|
|
157
|
-
---
|
|
158
|
-
## Mock Precedence
|
|
159
|
-
The first file in **alphabetical order** wins when a particular route has many files.
|
|
160
|
-
|
|
161
|
-
### Why do we have many mocks per Route+Method?
|
|
162
|
-
Each route has mocks for many status codes, and also different
|
|
163
|
-
mocks (by having comments) for testing particular scenarios.
|
|
164
|
-
For example, different 422 validation error messages.
|
|
165
|
-
|
|
166
|
-
---
|
|
167
|
-
|
|
168
|
-
## Reset the Dashboard UI after insert or delete
|
|
169
|
-
When deleting the currently selected option, without refreshing the dashboard, the
|
|
170
|
-
served mock will be an alternative mock if it exists. That is, the dashboard won't show
|
|
171
|
-
a 404 after deleting the current mock if there’s another mock for that particular route.
|
|
172
|
-
|
|
173
|
-
Similarly, inserting a file that goes first in alphabetical order will
|
|
174
|
-
send a different mock from the one stated in the dashboard dropdown.
|
|
175
|
-
|
|
176
|
-
---
|
|
177
|
-
|
|
178
158
|
## Documenting Contracts (.md)
|
|
179
159
|
This is handy for documenting request payload parameters. The dashboard will
|
|
180
160
|
print the markdown document (as plain text) above the actual payload content.
|
|
@@ -183,15 +163,14 @@ Create a markdown file following the same filename convention.
|
|
|
183
163
|
The status code can be any number. For example,
|
|
184
164
|
```text
|
|
185
165
|
api/foo/[user-id].POST.201.md
|
|
166
|
+
api/foo/[user-id].POST.201.json
|
|
186
167
|
```
|
|
187
168
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
## Non-Deterministic Mocks (.mjs handlers)
|
|
169
|
+
## Transforms (.mjs)
|
|
191
170
|
Using the same filename convention, files ending
|
|
192
171
|
with `.mjs` will process the mock before serving it.
|
|
193
172
|
|
|
194
|
-
For example, this handler will
|
|
173
|
+
For example, this handler will capitalize the mock body and increment a counter.
|
|
195
174
|
```js
|
|
196
175
|
export default function capitalizeAllText(mockAsText, requestBody, Config) {
|
|
197
176
|
Config.database.myCount ??= 0
|
|
@@ -200,31 +179,21 @@ export default function capitalizeAllText(mockAsText, requestBody, Config) {
|
|
|
200
179
|
}
|
|
201
180
|
```
|
|
202
181
|
|
|
203
|
-
In demo mode, transforms tagged with the string `demo` within a filename
|
|
204
|
-
comment get activated. Mock sets tags e.g. `demo-a` have no effect. In
|
|
205
|
-
other words, only one transform per route is supported in demo mode.
|
|
206
|
-
|
|
207
182
|
---
|
|
208
183
|
|
|
209
184
|
## API
|
|
210
185
|
|
|
211
|
-
###
|
|
186
|
+
### `/mockaton/edit` Select a mock for a route
|
|
212
187
|
```
|
|
213
|
-
PATCH
|
|
188
|
+
PATCH /mockaton/edit
|
|
214
189
|
{
|
|
215
190
|
"file": "api/foo.200.GET.json"
|
|
216
191
|
"delayed": true // optional
|
|
217
192
|
}
|
|
218
193
|
```
|
|
194
|
+
---
|
|
219
195
|
|
|
220
|
-
###
|
|
221
|
-
```
|
|
222
|
-
PATCH http://localhost:2345/mockaton/bulk-select
|
|
223
|
-
{
|
|
224
|
-
"comment": "demo-a"
|
|
225
|
-
}
|
|
226
|
-
```
|
|
227
|
-
|
|
196
|
+
### `/mockaton/bulk-select` Select all mocks that have a particular comment
|
|
228
197
|
Many mocks can be changed at once. We do that by searching the
|
|
229
198
|
comments on the filename. For example, `api/foo(demo-a).GET.200.json`
|
|
230
199
|
|
|
@@ -235,8 +204,44 @@ particular API there is only `demo-a` and `demo-b`, changing to
|
|
|
235
204
|
Similarly, if there’s no demo mock at all for
|
|
236
205
|
a route, the first dev mock (a-z) will be served.
|
|
237
206
|
|
|
207
|
+
```
|
|
208
|
+
PATCH /mockaton/bulk-select
|
|
209
|
+
{
|
|
210
|
+
"comment": "demo-a"
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
### `/mockaton/reset` Reset
|
|
216
|
+
Re-Initialize the collection and its states (selected mocks and cookies, delays, etc.).
|
|
217
|
+
```
|
|
218
|
+
PATCH /mockaton/reset
|
|
219
|
+
```
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
### `/mockaton/cookies` Select a cookie
|
|
223
|
+
In `Config.cookies`, each key is a label used to change them.
|
|
224
|
+
```
|
|
225
|
+
PATCH /mockaton/cookies
|
|
226
|
+
{
|
|
227
|
+
"current_cookie_key": "My Normal User"
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### `/mockaton/cookies` List Cookies
|
|
232
|
+
Sends a list of the cookie labels (keys) and
|
|
233
|
+
along with a flag indicated if it’s the selected.
|
|
234
|
+
```
|
|
235
|
+
GET /mockaton/cookies
|
|
236
|
+
```
|
|
238
237
|
|
|
239
|
-
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
### `/mockaton/transform` Select a Transform
|
|
240
241
|
```
|
|
241
|
-
PATCH
|
|
242
|
+
PATCH /mockaton/transform
|
|
243
|
+
{
|
|
244
|
+
"file": "api/video/list(concat newly uploaded).GET.200.mjs"
|
|
245
|
+
}
|
|
242
246
|
```
|
|
247
|
+
---
|
package/Route.js
CHANGED
|
@@ -16,7 +16,7 @@ export class Route {
|
|
|
16
16
|
constructor(file) {
|
|
17
17
|
const { urlMask, method } = Route.parseFilename(file)
|
|
18
18
|
this.method = method
|
|
19
|
-
this.#urlRegex = new RegExp(
|
|
19
|
+
this.#urlRegex = new RegExp('^' + disregardVariables(removeQueryStringAndFragment(urlMask)) + '/*$')
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
urlMaskMatches(url) {
|
package/Tests.js
CHANGED
package/_usage_example.js
CHANGED
|
@@ -8,8 +8,8 @@ Mockaton({
|
|
|
8
8
|
mocksDir: resolve('sample-mocks'),
|
|
9
9
|
staticDir: resolve('sample-static'),
|
|
10
10
|
cookies: {
|
|
11
|
-
'Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
12
|
-
'Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
11
|
+
'My Admin User': 'my-cookie=1;Path=/;SameSite=strict',
|
|
12
|
+
'My Normal User': 'my-cookie=0;Path=/;SameSite=strict',
|
|
13
13
|
'My JWT': jwtCookie('my-cookie', {
|
|
14
14
|
email: 'john.doe@example.com',
|
|
15
15
|
picture: 'https://cdn.auth0.com/avatars/jd.png'
|
package/package.json
CHANGED