mockaton 0.10.3 → 0.10.5

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/Tests.js CHANGED
@@ -10,7 +10,7 @@ import { writeFileSync, mkdtempSync, mkdirSync } from 'node:fs'
10
10
  import { Route } from './src/Route.js'
11
11
  import { mimeFor } from './src/utils/mime.js'
12
12
  import { Mockaton } from './src/Mockaton.js'
13
- import { DP, DF } from './src/ApiConstants.js'
13
+ import { API, DF } from './src/ApiConstants.js'
14
14
 
15
15
 
16
16
  const tmpDir = mkdtempSync(tmpdir()) + '/'
@@ -169,11 +169,11 @@ async function runTests() {
169
169
  }
170
170
 
171
171
  async function reset() {
172
- await request(DP.reset, { method: 'PATCH' })
172
+ await request(API.reset, { method: 'PATCH' })
173
173
  }
174
174
 
175
175
  async function testItRendersDashboard() {
176
- const res = await request(DP.dashboard)
176
+ const res = await request(API.dashboard)
177
177
  const body = await res.text()
178
178
  await describe('Dashboard', () =>
179
179
  it('Renders HTML', () => match(body, new RegExp('<!DOCTYPE html>'))))
@@ -197,7 +197,7 @@ async function testMockDispatching(url, file, expectedBody, reqBody = void 0) {
197
197
  }
198
198
 
199
199
  async function testItUpdatesTheCurrentSelectedMock(url, file, expectedStatus, expectedBody) {
200
- await request(DP.edit, {
200
+ await request(API.edit, {
201
201
  method: 'PATCH',
202
202
  body: JSON.stringify({ [DF.file]: file })
203
203
  })
@@ -210,7 +210,7 @@ async function testItUpdatesTheCurrentSelectedMock(url, file, expectedStatus, ex
210
210
  }
211
211
 
212
212
  async function testItUpdatesDelayAndFile(url, file, expectedBody) {
213
- await request(DP.edit, {
213
+ await request(API.edit, {
214
214
  method: 'PATCH',
215
215
  body: JSON.stringify({
216
216
  [DF.file]: file,
@@ -228,7 +228,7 @@ async function testItUpdatesDelayAndFile(url, file, expectedBody) {
228
228
 
229
229
 
230
230
  async function testAutogenerates500(url, file) {
231
- await request(DP.edit, {
231
+ await request(API.edit, {
232
232
  method: 'PATCH',
233
233
  body: JSON.stringify({ [DF.file]: file })
234
234
  })
@@ -241,7 +241,7 @@ async function testAutogenerates500(url, file) {
241
241
  }
242
242
 
243
243
  async function testPreservesExiting500(url, file, expectedBody) {
244
- await request(DP.edit, {
244
+ await request(API.edit, {
245
245
  method: 'PATCH',
246
246
  body: JSON.stringify({ [DF.file]: file })
247
247
  })
@@ -254,14 +254,14 @@ async function testPreservesExiting500(url, file, expectedBody) {
254
254
  }
255
255
 
256
256
  async function testExtractsAllComments(expected) {
257
- const res = await request(DP.comments)
257
+ const res = await request(API.comments)
258
258
  const body = await res.json()
259
259
  await it('Extracts all comments without duplicates', () =>
260
260
  deepEqual(body, expected))
261
261
  }
262
262
 
263
263
  async function testItBulkSelectsByComment(comment, tests) {
264
- await request(DP.bulkSelect, {
264
+ await request(API.bulkSelect, {
265
265
  method: 'PATCH',
266
266
  body: JSON.stringify({ [DF.comment]: comment })
267
267
  })
@@ -273,7 +273,7 @@ async function testItBulkSelectsByComment(comment, tests) {
273
273
  async function testItUpdatesUserRole() {
274
274
  await describe('Cookie', () => {
275
275
  it('Defaults to the first key:value', async () => {
276
- const res = await request(DP.cookies)
276
+ const res = await request(API.cookies)
277
277
  deepEqual(await res.json(), [
278
278
  ['userA', true],
279
279
  ['userB', false]
@@ -281,11 +281,11 @@ async function testItUpdatesUserRole() {
281
281
  })
282
282
 
283
283
  it('Update the selected cookie', async () => {
284
- await request(DP.cookies, {
284
+ await request(API.cookies, {
285
285
  method: 'PATCH',
286
286
  body: JSON.stringify({ [DF.currentCookieKey]: 'userB' })
287
287
  })
288
- const res = await request(DP.cookies)
288
+ const res = await request(API.cookies)
289
289
  deepEqual(await res.json(), [
290
290
  ['userA', false],
291
291
  ['userB', true]
@@ -305,7 +305,7 @@ export default function (mock, reqBody, config) {
305
305
  return JSON.stringify(body);
306
306
  }`)
307
307
  await reset() // for registering the files
308
- await request(DP.transform, {
308
+ await request(API.transform, {
309
309
  method: 'PATCH',
310
310
  body: JSON.stringify({ [DF.file]: 'api/transform.POST.200.mjs' })
311
311
  })
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": "0.10.3",
5
+ "version": "0.10.5",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
8
8
  "license": "MIT",
package/src/Api.js CHANGED
@@ -6,29 +6,29 @@
6
6
  import { join } from 'node:path'
7
7
  import { cookie } from './cookie.js'
8
8
  import { Config } from './Config.js'
9
- import { DF, DP } from './ApiConstants.js'
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
12
  import { sendOK, sendBadRequest, sendJSON, sendFile } from './utils/http-response.js'
13
13
 
14
14
 
15
15
  export const apiGetRequests = new Map([
16
- [DP.dashboard, serveDashboard],
16
+ [API.dashboard, serveDashboard],
17
17
  ['/Route.js', serveDashboardAsset],
18
18
  ['/Dashboard.js', serveDashboardAsset],
19
19
  ['/Dashboard.css', serveDashboardAsset],
20
20
  ['/ApiConstants.js', serveDashboardAsset],
21
- [DP.mocks, listMockBrokers],
22
- [DP.cookies, listCookies],
23
- [DP.comments, listComments]
21
+ [API.mocks, listMockBrokers],
22
+ [API.cookies, listCookies],
23
+ [API.comments, listComments]
24
24
  ])
25
25
 
26
26
  export const apiPatchRequests = new Map([
27
- [DP.bulkSelect, bulkUpdateBrokersByCommentTag],
28
- [DP.edit, updateBroker],
29
- [DP.reset, reinitialize],
30
- [DP.cookies, selectCookie],
31
- [DP.transform, updateBrokerTransform]
27
+ [API.bulkSelect, bulkUpdateBrokersByCommentTag],
28
+ [API.edit, updateBroker],
29
+ [API.reset, reinitialize],
30
+ [API.cookies, selectCookie],
31
+ [API.transform, updateBrokerTransform]
32
32
  ])
33
33
 
34
34
  function serveDashboard(_, response) {
@@ -1,5 +1,5 @@
1
1
  const MOUNT = '/mockaton'
2
- export const DP = { // Dashboard Paths
2
+ export const API = {
3
3
  dashboard: MOUNT,
4
4
  bulkSelect: MOUNT + '/bulk-select',
5
5
  comments: MOUNT + '/comments',
package/src/Mockaton.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { exec } from 'node:child_process'
2
2
  import { createServer } from 'node:http'
3
3
 
4
- import { DP } from './ApiConstants.js'
4
+ import { API } from './ApiConstants.js'
5
5
  import { Config, setup } from './Config.js'
6
6
  import { dispatchMock } from './MockDispatcher.js'
7
7
  import * as mockBrokerCollection from './mockBrokersCollection.js'
@@ -30,10 +30,11 @@ export function Mockaton(options) {
30
30
  .listen(Config.port, Config.host, function (error) {
31
31
  const { address, port } = this.address()
32
32
  const url = `http://${address}:${port}`
33
- console.log('Listening on', url)
33
+ console.log('Listening', url)
34
+ console.log('Dashboard', url + API.dashboard)
34
35
  if (error)
35
36
  console.error(error)
36
37
  else if (!Config.skipOpen)
37
- exec(`open ${url + DP.dashboard}`)
38
+ exec(`open ${url + API.dashboard}`)
38
39
  })
39
40
  }
@@ -1,13 +1,12 @@
1
1
  import { join } from 'node:path'
2
- import { existsSync, lstatSync } from 'node:fs'
2
+ import { existsSync as exists, lstatSync as lstat } from 'node:fs'
3
3
 
4
4
  import { Config } from './Config.js'
5
5
  import { sendFile, sendPartialContent } from './utils/http-response.js'
6
6
 
7
7
 
8
8
  export function isStatic(req) {
9
- return Config.staticDir &&
10
- existsSync(resolvePath(req))
9
+ return Config.staticDir && exists(resolvePath(req))
11
10
  }
12
11
 
13
12
  export async function dispatchStatic(req, response) {
@@ -20,8 +19,8 @@ export async function dispatchStatic(req, response) {
20
19
 
21
20
  function resolvePath(req) {
22
21
  const candidate = join(Config.staticDir, req.url)
23
- if (existsSync(candidate))
24
- return lstatSync(candidate).isDirectory()
22
+ if (exists(candidate))
23
+ return lstat(candidate).isDirectory()
25
24
  ? candidate + '/index.html'
26
25
  : candidate
27
26
  }
@@ -1,5 +1,5 @@
1
1
  import { join } from 'node:path'
2
- import { readdirSync, lstatSync } from 'node:fs'
2
+ import { readdirSync as readDir, lstatSync } from 'node:fs'
3
3
 
4
4
  import { Route } from './Route.js'
5
5
  import { Config } from './Config.js'
@@ -23,7 +23,7 @@ export function init() {
23
23
  collection = {}
24
24
  cookie.init(Config.cookies)
25
25
 
26
- const files = readdirSync(Config.mocksDir, { recursive: true })
26
+ const files = readDir(Config.mocksDir, { recursive: true })
27
27
  .filter(f => Config.allowedExt.test(f) && lstatSync(join(Config.mocksDir, f)).isFile())
28
28
  .sort()
29
29