加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.js 13.72 KB
一键复制 编辑 原始数据 按行查看 历史
Yourtion 提交于 2017-12-18 16:33 . Fixed #9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
/* global it */
'use strict';
const assert = require('assert');
const expressCoroutine = require('./index')(require('express'));
const request = require('supertest');
const bodyParser = require('body-parser');
const async = require('async');
// eslint-disable-next-line no-unused-vars
const fn = function (req, res, next) {
return new Promise((resolve, reject) => {
if (req.params.user !== 'a') return reject(new Error('Bang!'));
resolve('a');
});
};
const middleware1 = function* (req, res, next) {
yield fn(req, res, next);
};
const middleware2 = function (req, res, next) {
if (req.params.user !== 'a') return next(new Error('Bang!'));
next();
};
const middleware3 = async function (req, res, next) {
await fn(req, res, next);
next();
};
// eslint-disable-next-line no-undef
describe('generator', function () {
it('accepts generator as middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/', function* (req, res) {
res.send('it works!');
});
request(app)
.get('/')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('does not call next middleware after res.send', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/', function* (req, res) {
res.send('one');
});
app.get('/', function* (req, res) {
res.send('two');
});
request(app)
.get('/')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'one');
done();
});
});
it('pass throwed exception to error handler', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/', function* () {
throw new Error('Bang!');
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
res.sendStatus(500);
next();
});
request(app).get('/').expect(500, done);
});
it('pass throwed exception to error handler with code', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/', function* () {
const error = new Error('Bang!');
error.code = -100;
throw error;
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
assert.equal(err.code, -100);
res.sendStatus(500);
next();
});
request(app).get('/').expect(500, done);
});
it('works with param method', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.param('user', function* (req, res, id) {
assert.equal(id, 42);
});
app.get('/:user', function* (req, res) {
res.send('meh');
});
request(app).get('/42').expect(200, done);
});
it('use multiple generator middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware1, function* (req, res) {
res.send('it works!');
});
request(app)
.get('/a')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('use function and generator middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware2, function* (req, res) {
res.send('it works!');
});
request(app)
.get('/a')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('use function and async middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware3, function (req, res) {
res.send('it works!');
});
request(app)
.get('/a')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('pass throwed exception in generator middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware1, function* (req, res) {
res.send('it works!');
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
res.sendStatus(500);
next();
});
request(app).get('/b').expect(500, done);
});
it('pass throwed exception in async middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware3, function* (req, res) {
res.send('it works!');
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
res.sendStatus(500);
next();
});
request(app).get('/b').expect(500, done);
});
it('pass throwed exception in function middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/:user', middleware2, function* (req, res) {
res.send('it works!');
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
res.sendStatus(500);
next();
});
request(app).get('/b').expect(500, done);
});
it('works with app.route call', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.route('/')
.get(function* (req, res) {
res.send('it works!');
});
request(app)
.get('/')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('works with route', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/', function* (req, res) {
res.send('it works!');
});
request(app)
.get('/')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('throwed when works with route and generator middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/:user', middleware1, function* (req, res) {
res.send('it works!');
});
app.use((err, req, res, next) => {
assert.equal(err.message, 'Bang!');
res.sendStatus(500);
next();
});
request(app).get('/b').expect(500, done);
});
it('works with route and generator middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/:user', middleware1, function* (req, res) {
res.send('it works!');
});
request(app)
.get('/a')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('accepts old function as middleware', done => {
const app = expressCoroutine();
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('it works!');
});
request(app)
.get('/')
.end((err, res) => {
assert.ifError(err);
assert.equal(res.text, 'it works!');
done();
});
});
it('has static method', () => {
assert.equal(typeof expressCoroutine.static, 'function');
});
it('has coroutine export', () => {
assert.equal(typeof require('./index').coroutine, 'function');
assert.deepEqual(require('./index').coroutine, require('lei-coroutine'));
});
});
// eslint-disable-next-line no-undef
describe('normal', function () {
it('get, post, put, delete', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/a', async function (req, res) {
res.json(req.query);
});
router.post('/b', async function (req, res) {
res.json(req.body);
});
router.put('/c', async function (req, res) {
res.json(req.body);
});
router.delete('/d', async function (req, res) {
res.json(req.body);
});
async.series([
function (next) {
request(app)
.get('/a')
.query({ a: 123 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 123 });
next();
});
},
function (next) {
request(app)
.post('/b')
.send({ a: 456 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 456 });
next();
});
},
function (next) {
request(app)
.put('/c')
.send({ a: 123, b: 456 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 123, b: 456 });
next();
});
},
function (next) {
request(app)
.delete('/d')
.send({ a: 'abcd' })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 'abcd' });
next();
});
},
], done);
});
it('all', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.all('/test', async function (req, res) {
res.json({ method: req.method, data: Object.assign(req.query, req.body) });
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 111, b: 222 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { method: 'GET', data: { a: 111, b: 222 }});
next();
});
},
], done);
});
it('use', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.use(async function (req, res, next) {
req.query.c = 789;
next();
});
router.get('/test', async function (req, res, next) {
req.query.d = 888;
next();
}, async function (req, res) {
res.json({ method: req.method, data: Object.assign(req.query, req.body) });
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 111, b: 222 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { method: 'GET', data: { a: 111, b: 222, c: 789, d: 888 }});
next();
});
},
], done);
});
it('param', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.param('id', async function (req, res, next, id) {
req.query.id = id;
next();
});
router.get('/:id', async function (req, res) {
res.json({ method: req.method, data: Object.assign(req.query, req.body) });
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 111, b: 222 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { method: 'GET', data: { a: 111, b: 222, id: 'test' }});
next();
});
},
], done);
});
it('route', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router
.route('/test')
.get(async function (req, res) {
res.json(req.query);
})
.post(bodyParser.json(), async function (req, res) {
res.json(req.body);
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 123 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 123 });
next();
});
},
function (next) {
request(app)
.post('/test')
.send({ a: 456 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { a: 456 });
next();
});
},
], done);
});
});
// eslint-disable-next-line no-undef
describe('error', function () {
it('throw error', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/test', async function (_req, _res) {
throw new Error('abcd');
});
app.use(function (err, req, res, _next) {
res.json({ error: err.message });
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 111, b: 222 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { error: 'abcd' });
next();
});
},
], done);
});
it('error handler', function (done) {
const app = expressCoroutine();
app.use(bodyParser.json());
const router = new expressCoroutine.Router();
app.use(router);
router.get('/test', async function (_req, _res) {
throw new Error('abcd');
});
router.use(async function (err, req, res, _next) {
res.json({ error: err.message });
});
async.series([
function (next) {
request(app)
.get('/test')
.query({ a: 111, b: 222 })
.end((err, res) => {
if (err) return next(err);
assert.deepEqual(res.body, { error: 'abcd' });
next();
});
},
], done);
});
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化