diff options
Diffstat (limited to 'node_modules/koa-connect/tests.js')
| -rw-r--r-- | node_modules/koa-connect/tests.js | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/node_modules/koa-connect/tests.js b/node_modules/koa-connect/tests.js new file mode 100644 index 00000000..43cb904d --- /dev/null +++ b/node_modules/koa-connect/tests.js @@ -0,0 +1,121 @@ +'use strict' + +const Koa = require('koa') +const supertest = require('supertest') +const c2k = require('./index') +const assert = require('assert') + +describe('koa-connect', () => { + let app + + beforeEach(() => { + app = new Koa() + app.use((ctx, next) => { + ctx.status = 404 + ctx.body = 'Original' + return next() + }) + }) + + it('works with a single noop Connect middleware', (done) => { + function noop(req, res, next) { next() } + app.use(c2k(noop)) + supertest(app.callback()) + .get('/') + .expect('Original') + .end(done) + }) + + it('works with two noop Connect middleware', (done) => { + function noop(req, res, next) { next() } + app.use(c2k(noop)) + app.use(c2k(noop)) + supertest(app.callback()) + .get('/') + .expect('Original') + .end(done) + }) + + it('passes correctly to downstream Koa middlewares', (done) => { + function noop(req, res, next) { next() } + function goodStatusSetter(ctx) { ctx.status = 200 } + app.use(c2k(noop)) + app.use(goodStatusSetter) + supertest(app.callback()) + .get('/') + .expect(200) + .end(done) + }) + + it('bubbles back to earlier middleware', (done) => { + let callOne = false + let callTwo = false + app.use((ctx, next) => { + return next() + .then(() => { + callTwo = true + }) + }) + + app.use(c2k((req, res) => { + res.statusCode = 200 + callOne = true + })) + + supertest(app.callback()) + .get('/') + .expect(200) + .then(() => { + assert(callOne === true, 'Second middleware never called') + assert(callTwo === true, 'Never bubbled back to first middleware') + done() + }) + }) + + it('receives errors from Connect middleware', (done) => { + app.use((ctx, next) => { + next().catch((err) => ctx.status = 505) + }) + + app.use(c2k((req, res, next) => { + next(new Error('How Connect does error handling')) + })) + + app.use((ctx) => { + // Fail the test if this is reached + done(new Error('Improper error handling')) + }) + + supertest(app.callback()) + .get('/') + .expect(505) + .end(done) + }) + + it('Setting the body or status in Koa middlewares does not do anything if res.end was used in a Connect middleware', (done) => { + const message = 'The message that makes it' + app.use((ctx, next) => { + next() + .then(() => { + if ( ctx.status !== 200 ) { + done(new Error('Never reached connect middleware')) + } + // These calls won't end up doing anything + ctx.status = 500 + ctx.body = 'A story already written' + }) + }) + + app.use(c2k((req, res) => { + res.statusCode = 200 + res.setHeader('Content-Length', message.length) + res.end(message) + })) + + supertest(app.callback()) + .get('/') + .expect(200) + .expect(message) + .end(done) + }) +}) |
