-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from htdangkhoa/feature/local-setting
feat: allow user set set key value as settings
- Loading branch information
Showing
4 changed files
with
92 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function set(key, value) { | ||
if (!this.locals) { | ||
this.locals = {}; | ||
} | ||
|
||
this.locals[key] = value; | ||
} | ||
|
||
function get(key) { | ||
if (!this.locals) { | ||
return undefined; | ||
} | ||
|
||
return this.locals[key]; | ||
} | ||
|
||
module.exports = { | ||
set, | ||
get, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const supertest = require('supertest'); | ||
const pureHttp = require('../..'); | ||
|
||
describe('configs', () => { | ||
it('should set and get configs', async () => { | ||
const app = pureHttp(); | ||
app.set('foo', 'bar'); | ||
app.set('lorem', 'ipsum'); | ||
|
||
app.use((req, res) => { | ||
const foo = req.app.get('foo'); | ||
const lorem = req.app.get('lorem'); | ||
res.send({ | ||
foo, | ||
lorem, | ||
}); | ||
}); | ||
|
||
const request = supertest(app); | ||
const res = await request.get('/'); | ||
|
||
const expected = { | ||
foo: 'bar', | ||
lorem: 'ipsum', | ||
}; | ||
|
||
expect(res.body).toEqual(expected); | ||
}); | ||
|
||
it('should return undefined if the config is not set', async () => { | ||
const app = pureHttp(); | ||
|
||
app.use((req, res) => { | ||
const foo = req.app.get('foo'); | ||
res.send(String(foo)); | ||
}); | ||
|
||
const request = supertest(app); | ||
await request.get('/').expect(200, 'undefined'); | ||
}); | ||
}); |