Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/app/components/get/get.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 27 additions & 8 deletions lib/app/components/status/status.router.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
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);
});

module.exports = {
router: router,
id: ()=> id
id: (path) => idMap ? idMap[path] || id : id
};
2 changes: 1 addition & 1 deletion lib/app/shared/bases/scenario-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion lib/app/shared/bases/service-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 54 additions & 1 deletion test/e2e/same-req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
});
});

Expand All @@ -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({});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/data/scenario-provider.1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = db = {
'/customers/{id}': [
{
_req: {
id_: 'Nathan',
_id: 'Nathan',
_headers: []
},
_res: {
Expand All @@ -38,7 +38,7 @@ module.exports = db = {
},
{
_req: {
id_: 'mark',
_id: 'mark',
_headers: []
},
_res: {
Expand Down