Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evheniy Bystrov
committed
Feb 20, 2017
1 parent
da0d7a4
commit 79564f5
Showing
5 changed files
with
187 additions
and
17 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 |
---|---|---|
@@ -1,6 +1,19 @@ | ||
const debug = require('debug')('yeps:wrapper'); | ||
|
||
module.exports = fn => async context => { | ||
debug(fn); | ||
context; | ||
debug('Wrapper created'); | ||
return new Promise((resolve, reject) => { | ||
context.res.on('finish', () => { | ||
debug('Response finished'); | ||
reject(); | ||
}); | ||
fn(context.req, context.res, error => { | ||
debug(error); | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,161 @@ | ||
const App = require('yeps'); | ||
const error = require('yeps-error'); | ||
const http = require('http'); | ||
const chai = require('chai'); | ||
const chaiHttp = require('chai-http'); | ||
const wrapper = require('..'); | ||
const expect = chai.expect; | ||
const bodyParser = require('body-parser'); | ||
const favicon = require('serve-favicon'); | ||
const path = require('path'); | ||
|
||
chai.use(chaiHttp); | ||
let app; | ||
|
||
describe('YEPS express wrapper test', () => { | ||
it('should test next()'); | ||
it('should test next(error)'); | ||
it('should test without next = res.end()'); | ||
it('bodyParser'); | ||
it('favicon'); | ||
|
||
beforeEach(() => { | ||
app = new App(); | ||
}); | ||
|
||
it('should test next()', async () => { | ||
let isTestFinished1 = false; | ||
let isTestFinished2 = false; | ||
let isTestFinished3 = false; | ||
|
||
app.then(wrapper((req, res, next) => { | ||
isTestFinished1 = true; | ||
next(); | ||
})); | ||
app.then(async ctx => { | ||
isTestFinished2 = true; | ||
ctx.res.writeHead(200); | ||
ctx.res.end('test'); | ||
}); | ||
|
||
await chai.request(http.createServer(app.resolve())) | ||
.get('/') | ||
.send() | ||
.then(res => { | ||
expect(res).to.have.status(200); | ||
expect(res.text).to.be.equal('test'); | ||
isTestFinished3 = true; | ||
}); | ||
|
||
expect(isTestFinished1).is.true; | ||
expect(isTestFinished2).is.true; | ||
expect(isTestFinished3).is.true; | ||
}); | ||
|
||
it('should test next(error)', async () => { | ||
let isTestFinished1 = false; | ||
let isTestFinished2 = false; | ||
let isTestFinished3 = false; | ||
|
||
app.all([ | ||
error(), | ||
]); | ||
|
||
app.then(wrapper((req, res, next) => { | ||
isTestFinished1 = true; | ||
next(new Error('error')); | ||
})); | ||
app.then(async ctx => { | ||
isTestFinished2 = true; | ||
ctx.res.writeHead(200); | ||
ctx.res.end('test'); | ||
}); | ||
|
||
await chai.request(http.createServer(app.resolve())) | ||
.get('/') | ||
.send() | ||
.catch(err => { | ||
expect(err).to.have.status(500); | ||
expect(err.message).to.be.equal('Internal Server Error'); | ||
isTestFinished3 = true; | ||
}); | ||
|
||
expect(isTestFinished1).is.true; | ||
expect(isTestFinished2).is.false; | ||
expect(isTestFinished3).is.true; | ||
}); | ||
|
||
it('should test without next = res.end()', async () => { | ||
let isTestFinished1 = false; | ||
let isTestFinished2 = false; | ||
let isTestFinished3 = false; | ||
|
||
app.then(wrapper((req, res) => { | ||
isTestFinished1 = true; | ||
res.writeHead(200); | ||
res.end('next'); | ||
})); | ||
app.then(async ctx => { | ||
isTestFinished2 = true; | ||
ctx.res.writeHead(200); | ||
ctx.res.end('test'); | ||
}); | ||
|
||
await chai.request(http.createServer(app.resolve())) | ||
.get('/') | ||
.send() | ||
.then(res => { | ||
expect(res).to.have.status(200); | ||
expect(res.text).to.be.equal('next'); | ||
isTestFinished3 = true; | ||
}); | ||
|
||
expect(isTestFinished1).is.true; | ||
expect(isTestFinished2).is.false; | ||
expect(isTestFinished3).is.true; | ||
}); | ||
|
||
it('body-parser', async () => { | ||
let isTestFinished1 = false; | ||
let isTestFinished2 = false; | ||
|
||
app.then(wrapper(bodyParser.json())); | ||
app.then(async ctx => { | ||
isTestFinished1 = true; | ||
ctx.res.writeHead(200, {'Content-Type': 'application/json'}); | ||
ctx.res.end(JSON.stringify(ctx.req.body)); | ||
}); | ||
|
||
await chai.request(http.createServer(app.resolve())) | ||
.get('/') | ||
.set('Content-Type', 'application/json') | ||
.send('{"user":"test"}') | ||
.then(res => { | ||
expect(res).to.have.status(200); | ||
expect(res.text).to.be.equal('{"user":"test"}'); | ||
expect(res.body).is.an('object'); | ||
expect(res.body).to.have.property('user'); | ||
expect(res.body.user).is.not.empty; | ||
expect(res.body.user).to.be.equal('test'); | ||
isTestFinished2 = true; | ||
}); | ||
|
||
expect(isTestFinished1).is.true; | ||
expect(isTestFinished2).is.true; | ||
}); | ||
|
||
it('serve-favicon', async () => { | ||
let isTestFinished = false; | ||
|
||
app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); | ||
|
||
await chai.request(http.createServer(app.resolve())) | ||
.get('/favicon.ico') | ||
.send() | ||
.then(res => { | ||
expect(res).to.have.status(200); | ||
expect(res.headers['content-type']).to.be.equal('image/x-icon'); | ||
expect(res.headers.etag).is.not.empty; | ||
expect(res.headers['cache-control']).is.not.empty; | ||
isTestFinished = true; | ||
}); | ||
|
||
expect(isTestFinished).is.true; | ||
}); | ||
|
||
}); |
Binary file not shown.