From ed62856935de84a78267e10a578ea1fe07e52e8f Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Tue, 19 Mar 2024 15:26:01 +0000 Subject: [PATCH 1/8] feat: id map --- lib/app/components/get/get.repository.js | 2 +- lib/app/components/status/status.router.js | 19 +++++++++++++------ lib/app/shared/bases/scenario-base.js | 2 +- lib/app/shared/bases/service-base.js | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/app/components/get/get.repository.js b/lib/app/components/get/get.repository.js index b5f823b..b589dcc 100644 --- a/lib/app/components/get/get.repository.js +++ b/lib/app/components/get/get.repository.js @@ -19,7 +19,7 @@ class GetRespository { const db = Data.db(); const data = db[method === 'HEAD' ? '_head' : '_get']; - const scenarioProvider = new ScenarioProvider(path, data, request.headers, request.cookies, id()); + const scenarioProvider = new ScenarioProvider(path, data, request.headers, request.cookies, id(request.path)); const endpoint = scenarioProvider.isInDB(); return endpoint ? scenarioProvider.getScenario(endpoint) : negaBodies.notFound; } diff --git a/lib/app/components/status/status.router.js b/lib/app/components/status/status.router.js index 8356047..1f1c989 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -1,27 +1,34 @@ const express = require('express'); const router = new express.Router(); +let idMap = new Map(); let id; router.get('/__scenario', (req, res) => { - res.json({ id: id }); + res.json({ id: idMap.get(req.body.path) || id }); }); router.post('/__scenario', (req, res) => { - const bodyJson = req.body; - - id = bodyJson.id; + if (req.body.path) { + idMap.set(req.body.path, req.body.id); + } else { + id = req.body.id; + } res.sendStatus(200); }); router.post('/__scenario/reset', (req, res) => { - id = undefined; + if (req.body.path) { + idMap.set(req.body.path, undefined); + } else { + id = undefined; + } res.sendStatus(200); }); module.exports = { router: router, - id: ()=> id + id: (path) => idMap.get(path) || id }; diff --git a/lib/app/shared/bases/scenario-base.js b/lib/app/shared/bases/scenario-base.js index d192e38..ebab3a8 100644 --- a/lib/app/shared/bases/scenario-base.js +++ b/lib/app/shared/bases/scenario-base.js @@ -9,7 +9,7 @@ module.exports = class ScenarioBase { * @return {object} Return an array of scenarios. */ provideScenarios(req, db) { - const scenarioProvider = new ScenarioProvider(req.url, db, req.headers, req.cookies, id()); + const scenarioProvider = new ScenarioProvider(req.url, db, req.headers, req.cookies, id(req.path)); const endpointScenario = scenarioProvider.isInDB(); const scenarios = scenarioProvider.getScenarios(endpointScenario); diff --git a/lib/app/shared/bases/service-base.js b/lib/app/shared/bases/service-base.js index e9a0605..f226540 100644 --- a/lib/app/shared/bases/service-base.js +++ b/lib/app/shared/bases/service-base.js @@ -15,7 +15,7 @@ module.exports = class ServiceBase { * @param {object} repository respository. */ handleRequest(req, res, repository) { - const scenario = repository.findData(req, id()); + const scenario = repository.findData(req, id(req.path)); let response; let responseOps = scenario._res; From c304f7cf0c573d5983f8a63e32c6e0f1cf91140b Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Tue, 19 Mar 2024 15:29:52 +0000 Subject: [PATCH 2/8] refactor: destructure --- lib/app/components/status/status.router.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/app/components/status/status.router.js b/lib/app/components/status/status.router.js index 1f1c989..d8c9640 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -4,23 +4,23 @@ const router = new express.Router(); let idMap = new Map(); let id; -router.get('/__scenario', (req, res) => { - res.json({ id: idMap.get(req.body.path) || id }); +router.get('/__scenario', ({ body }, res) => { + res.json({ id: idMap.get(body.path) || id }); }); -router.post('/__scenario', (req, res) => { - if (req.body.path) { - idMap.set(req.body.path, req.body.id); +router.post('/__scenario', ({ body }, res) => { + if (body.path) { + idMap.set(body.path, body.id); } else { - id = req.body.id; + id = body.id; } res.sendStatus(200); }); -router.post('/__scenario/reset', (req, res) => { - if (req.body.path) { - idMap.set(req.body.path, undefined); +router.post('/__scenario/reset', ({ body }, res) => { + if (body.path) { + idMap.set(body.path, undefined); } else { id = undefined; } From 5ec7f93101e4f0472158dda6c3c308284109e6e4 Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 10:59:30 +0000 Subject: [PATCH 3/8] docs: curl documentation for path specific custom responses --- README.md | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ec6b935..4a24d80 100644 --- a/README.md +++ b/README.md @@ -812,7 +812,7 @@ Basically, it requires to tell to the library which id to use before making the The library expose three endpoints to deal with the id. -- POST "/__scenario" body: ```{ id: 'CUSTOM_ID' }``` +- POST "/__scenario" body: ```{ id: 'CUSTOM_ID', path?: 'REQUEST_PATH' }``` This request sets the id in the library. For the example above, if we want to response the first scenario, we should make the next request before the mock is executed: ```curl @@ -821,18 +821,30 @@ The library expose three endpoints to deal with the id. -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{ - "id":"ID_1" + "id": "ID_1", + "path": "/some/path" }' ``` -- GET "/__scenario" response: ```{ id: 'CUSTOM_ID' }``` - If we want to know which id is set in the library in that moment, we need to make a a get request. Using the example above: +- GET "/__scenario" response: ```{ id: 'CUSTOM_ID', path?: 'REQUEST_PATH' }``` + If we want to know which id is set in the library in that moment, we need to make a a get request. Using the example below: ```curl curl -X GET \ http://localhost:3005/__scenario \ ``` + Or, for a specific request path: + + ```curl + curl -X GET \ + http://localhost:3005/__scenario \ + -H 'content-type: application/json' \ + -d '{ + "path": "/some/path" + }' + ``` + - POST "/__scenario/reset Reset the identifier in the library @@ -841,6 +853,17 @@ The library expose three endpoints to deal with the id. http://localhost:3005/__scenario/reset \ ``` + Or, for a specific request path: + + ```curl + curl -X GET \ + http://localhost:3005/__scenario/reset \ + -H 'content-type: application/json' \ + -d '{ + "path": "/some/path" + }' + ``` + ## Config file From 83dc07e591fffb6e7e5403eb375852ab3e299151 Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 11:51:55 +0000 Subject: [PATCH 4/8] test: add tests and fix logic to be backwards compatible --- lib/app/components/status/status.router.js | 21 +++++++++++++-------- test/e2e/same-req.test.js | 18 +++++++++++++++++- test/unit/data/scenario-provider.1.test.js | 4 ++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/app/components/status/status.router.js b/lib/app/components/status/status.router.js index d8c9640..df7b1ed 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -1,16 +1,17 @@ const express = require('express'); const router = new express.Router(); -let idMap = new Map(); -let id; +let idMap, id; -router.get('/__scenario', ({ body }, res) => { - res.json({ id: idMap.get(body.path) || id }); +router.get('/__scenario', (_req, res) => { + res.json({ id, idMap }); }); router.post('/__scenario', ({ body }, res) => { if (body.path) { - idMap.set(body.path, body.id); + if (!idMap) idMap = {}; + + idMap[body.path] = body.id; } else { id = body.id; } @@ -19,8 +20,12 @@ router.post('/__scenario', ({ body }, res) => { }); router.post('/__scenario/reset', ({ body }, res) => { - if (body.path) { - idMap.set(body.path, undefined); + if (body.path && idMap) { + idMap[body.path] = undefined; + + if (!Object.entries(idMap).filter(entry => entry === undefined).length) { + idMap = undefined; + } } else { id = undefined; } @@ -30,5 +35,5 @@ router.post('/__scenario/reset', ({ body }, res) => { module.exports = { router: router, - id: (path) => idMap.get(path) || id + id: (path) => idMap ? idMap[path] || id : id }; diff --git a/test/e2e/same-req.test.js b/test/e2e/same-req.test.js index 29fcf4a..6301f6c 100644 --- a/test/e2e/same-req.test.js +++ b/test/e2e/same-req.test.js @@ -62,6 +62,23 @@ describe('@E2E - same request', () => { .get('/__scenario'); expect(response.status).to.equal(200); expect(response.body).to.deep.equal({}); + + response = await request(app) + .post('/__scenario') + .send({ id: '2', path: '/two/common/scenarios' }); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); + + response = await request(app) + .get('/__scenario'); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({ idMap: { '/two/common/scenarios': '2' } }); + + response = await request(app) + .post('/__scenario/reset') + .send({ path: '/two/common/scenarios' }); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); }); }); @@ -75,7 +92,6 @@ describe('@E2E - same request', () => { response = await request(app) .post('/__scenario') .send({ id: '3' }); - expect(response.status).to.equal(200); expect(response.body).to.deep.equal({}); diff --git a/test/unit/data/scenario-provider.1.test.js b/test/unit/data/scenario-provider.1.test.js index a891c83..7695f96 100644 --- a/test/unit/data/scenario-provider.1.test.js +++ b/test/unit/data/scenario-provider.1.test.js @@ -27,7 +27,7 @@ module.exports = db = { '/customers/{id}': [ { _req: { - id_: 'Nathan', + _id: 'Nathan', _headers: [] }, _res: { @@ -38,7 +38,7 @@ module.exports = db = { }, { _req: { - id_: 'mark', + _id: 'mark', _headers: [] }, _res: { From 62fd71857264b7ba748ee4db7b1de51b8173b672 Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 11:54:59 +0000 Subject: [PATCH 5/8] docs: fix tab alignment --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4a24d80..9d8cd5e 100644 --- a/README.md +++ b/README.md @@ -822,7 +822,7 @@ The library expose three endpoints to deal with the id. -H 'content-type: application/json' \ -d '{ "id": "ID_1", - "path": "/some/path" + "path": "/some/path" }' ``` @@ -841,7 +841,7 @@ The library expose three endpoints to deal with the id. http://localhost:3005/__scenario \ -H 'content-type: application/json' \ -d '{ - "path": "/some/path" + "path": "/some/path" }' ``` @@ -860,7 +860,7 @@ The library expose three endpoints to deal with the id. http://localhost:3005/__scenario/reset \ -H 'content-type: application/json' \ -d '{ - "path": "/some/path" + "path": "/some/path" }' ``` From dfacb9ec4162663347fe8feb462261330d947861 Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 12:07:14 +0000 Subject: [PATCH 6/8] fix: cleaner logic --- lib/app/components/status/status.router.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/app/components/status/status.router.js b/lib/app/components/status/status.router.js index df7b1ed..9d00df8 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -21,9 +21,9 @@ router.post('/__scenario', ({ body }, res) => { router.post('/__scenario/reset', ({ body }, res) => { if (body.path && idMap) { - idMap[body.path] = undefined; + delete idMap[body.path]; - if (!Object.entries(idMap).filter(entry => entry === undefined).length) { + if (!Object.entries(idMap).length) { idMap = undefined; } } else { From d3101950133576bf010c7364708a90c72a42ea96 Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 14:09:33 +0000 Subject: [PATCH 7/8] feat: reset-all --- README.md | 7 ++++++- lib/app/components/status/status.router.js | 7 +++++++ test/e2e/same-req.test.js | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d8cd5e..9bb2316 100644 --- a/README.md +++ b/README.md @@ -853,7 +853,7 @@ The library expose three endpoints to deal with the id. http://localhost:3005/__scenario/reset \ ``` - Or, for a specific request path: + For a specific request path: ```curl curl -X GET \ @@ -864,6 +864,11 @@ The library expose three endpoints to deal with the id. }' ``` + Or, to reset all custom scenarios: + + ```curl + curl http://localhost:3005/__scenario/reset-all + ``` ## Config file diff --git a/lib/app/components/status/status.router.js b/lib/app/components/status/status.router.js index 9d00df8..4777b57 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -33,6 +33,13 @@ router.post('/__scenario/reset', ({ body }, res) => { res.sendStatus(200); }); +router.get('/__scenario/reset-all', (_req, res) => { + idMap = undefined; + id = undefined; + + res.sendStatus(200); +}); + module.exports = { router: router, id: (path) => idMap ? idMap[path] || id : id diff --git a/test/e2e/same-req.test.js b/test/e2e/same-req.test.js index 6301f6c..7c33119 100644 --- a/test/e2e/same-req.test.js +++ b/test/e2e/same-req.test.js @@ -79,6 +79,11 @@ describe('@E2E - same request', () => { .send({ path: '/two/common/scenarios' }); expect(response.status).to.equal(200); expect(response.body).to.deep.equal({}); + + response = await request(app) + .get('/__scenario'); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); }); }); From 9af75f7f5f2cc94fb1844eb7dcf1068b6c360f7c Mon Sep 17 00:00:00 2001 From: Tim Snow Date: Wed, 20 Mar 2024 14:12:12 +0000 Subject: [PATCH 8/8] test: reset-all --- test/e2e/same-req.test.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/e2e/same-req.test.js b/test/e2e/same-req.test.js index 7c33119..185160d 100644 --- a/test/e2e/same-req.test.js +++ b/test/e2e/same-req.test.js @@ -84,6 +84,38 @@ describe('@E2E - same request', () => { .get('/__scenario'); expect(response.status).to.equal(200); expect(response.body).to.deep.equal({}); + + response = await request(app) + .post('/__scenario') + .send({ id: '1', path: '/path/1' }); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); + + response = await request(app) + .post('/__scenario') + .send({ id: '2', path: '/path/2' }); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); + + response = await request(app) + .get('/__scenario'); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({ + 'idMap': { + '/path/1': '1', + '/path/2': '2' + } + }); + + response = await request(app) + .get('/__scenario/reset-all'); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); + + response = await request(app) + .get('/__scenario'); + expect(response.status).to.equal(200); + expect(response.body).to.deep.equal({}); }); });