diff --git a/README.md b/README.md index ec6b935..9bb2316 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,22 @@ The library expose three endpoints to deal with the id. http://localhost:3005/__scenario/reset \ ``` + For a specific request path: + + ```curl + curl -X GET \ + http://localhost:3005/__scenario/reset \ + -H 'content-type: application/json' \ + -d '{ + "path": "/some/path" + }' + ``` + + Or, to reset all custom scenarios: + + ```curl + curl http://localhost:3005/__scenario/reset-all + ``` ## Config file 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..4777b57 100644 --- a/lib/app/components/status/status.router.js +++ b/lib/app/components/status/status.router.js @@ -1,21 +1,40 @@ const express = require('express'); const router = new express.Router(); -let id; +let idMap, id; -router.get('/__scenario', (req, res) => { - res.json({ id: id }); +router.get('/__scenario', (_req, res) => { + res.json({ id, idMap }); }); -router.post('/__scenario', (req, res) => { - const bodyJson = req.body; +router.post('/__scenario', ({ body }, res) => { + if (body.path) { + if (!idMap) idMap = {}; - id = bodyJson.id; + idMap[body.path] = body.id; + } else { + id = body.id; + } res.sendStatus(200); }); -router.post('/__scenario/reset', (req, res) => { +router.post('/__scenario/reset', ({ body }, res) => { + if (body.path && idMap) { + delete idMap[body.path]; + + if (!Object.entries(idMap).length) { + idMap = undefined; + } + } else { + id = undefined; + } + + res.sendStatus(200); +}); + +router.get('/__scenario/reset-all', (_req, res) => { + idMap = undefined; id = undefined; res.sendStatus(200); @@ -23,5 +42,5 @@ router.post('/__scenario/reset', (req, res) => { module.exports = { router: router, - id: ()=> id + id: (path) => idMap ? idMap[path] || id : 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; diff --git a/test/e2e/same-req.test.js b/test/e2e/same-req.test.js index 29fcf4a..185160d 100644 --- a/test/e2e/same-req.test.js +++ b/test/e2e/same-req.test.js @@ -62,6 +62,60 @@ 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({}); + + response = await request(app) + .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({}); }); }); @@ -75,7 +129,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: {