Skip to content

Commit

Permalink
Merge pull request #14 from mkbug-com/feature_add_headerapi
Browse files Browse the repository at this point in the history
<feat>: add header api get & set
  • Loading branch information
ryouaki authored Aug 9, 2020
2 parents 57a5486 + de19d23 commit e8c9619
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module.exports = {
ctx.params = req.params;
ctx.status = 200;
ctx.type = null;
ctx.get = req.get.bind(req);
ctx.set = res.set.bind(res);

return ctx;
},
Expand Down
12 changes: 12 additions & 0 deletions example/controller/HeaderApiTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { BaseController } = require('../../index');

module.exports = class HeaderApiTest extends BaseController {
getAction () {
return this.get('from-header');
}

getHeaderAction () {
this.set('from-header', 'from-header')
return ''
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mkbugjs",
"version": "1.4.2",
"version": "1.5.0",
"description": "An OOP style declare Nodejs Web framework base on Express.js",
"main": "index.js",
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions test/controller.header.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const request = require('superagent');

describe("Controller Header", () => {
it("Header from client", async () => {
let ret = null;
try {
ret = await request.get('http://localhost:3000/api/headerapitest')
.set('from-header', 'from-header')
} catch (err) {
ret = err;
} finally {
expect(ret.status).toBe(200);
expect(ret.text).toBe('from-header');
}
});

it("Header from server", async () => {
let ret = null;
try {
ret = await request.get('http://localhost:3000/api/headerapitest/header')
} catch (err) {
ret = err;
} finally {
expect(ret.status).toBe(200);
console.log(ret.header)
expect(ret.header['from-header']).toBe('from-header');
}
});
})

0 comments on commit e8c9619

Please sign in to comment.