Skip to content

Commit

Permalink
feat: support query __datahub_scene to select custom scene (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and xudafeng committed Jun 11, 2019
1 parent 7c96866 commit 9d83251
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/controller/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,20 @@ class SceneController extends Controller {

const interfaceUniqId = (tagName && originInterfaceId) ? originInterfaceId : uniqId;

const res = await ctx.service.scene.querySceneByInterfaceUniqIdAndSceneName({
interfaceUniqId,
sceneName: currentScene,
});
let res;
// try to use custom scene by default
if (ctx.query.__datahub_scene) {
res = await ctx.service.scene.querySceneByInterfaceUniqIdAndSceneName({
interfaceUniqId,
sceneName: ctx.query.__datahub_scene,
});
}
if (!res) {
res = await ctx.service.scene.querySceneByInterfaceUniqIdAndSceneName({
interfaceUniqId,
sceneName: currentScene,
});
}

const {
contextConfig,
Expand Down
30 changes: 30 additions & 0 deletions test/controller/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ describe('test/app/controller/data.test.js', () => {
assert(body.text === '{\"success\":true}');
});

it('GET /data/baz/api/path support query.__datahub_scene', async () => {
const [{ uniqId: projectUniqId }] = await ctx.model.Project.bulkCreate([
{ projectName: 'baz', description: 'bazd' },
]);
const [{ uniqId: interfaceUniqId }] = await ctx.model.Interface.bulkCreate([
{ projectUniqId, pathname: 'api/path', method: 'ALL', description: 'description' },
]);
await app.httpRequest()
.post('/api/scene/')
.send({
interfaceUniqId,
sceneName: 'id_1',
contextConfig: {},
data: { id: 1 },
});
await app.httpRequest()
.post('/api/scene/')
.send({
interfaceUniqId,
sceneName: 'success',
contextConfig: {},
data: { success: true },
});
const body = await app.httpRequest()
.get('/data/baz/api/path?__datahub_scene=id_1');
assert(body.status === 200);
assert(body.req.method === 'GET');
assert(body.text === '{\"id\":1}');
});

it('GET /data/baz/api/path project with empty', async () => {
const [{ uniqId: projectUniqId }] = await ctx.model.Project.bulkCreate([
{ projectName: 'baz', description: 'bazd' },
Expand Down

0 comments on commit 9d83251

Please sign in to comment.