aboutsummaryrefslogtreecommitdiff
path: root/node_modules/koa-connect/test/index.js
blob: 6a3d60657300e3c57730687fa45d6dd4451815d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';

const Koa = require('koa');
const supertest = require('supertest');
const c2k = require('..');

describe('koa-connect', () => {
  let app;

  beforeEach(() => {
    app = new Koa();
    app.use( function * (next) {
      this.status = 404;
      this.body = 'Original';
      yield next;
    })
  });

  it('works with a single noop Connect middleware', (done) => {
    app.use(c2k((req, res, next) => {
      next()
    }));
    supertest(app.callback())
      .get('/')
      .expect('Original')
      .end(done);
  });

  it('works with two noop Connect middleware', (done) => {
    app.use(c2k((req, res, next) => next()));
    app.use(c2k((req, res, next) => next()));
    supertest(app.callback())
      .get('/')
      .expect('Original')
      .end(done);
  });

  it('passes correctly to downstream Koa middlewares', (done) => {
    app.use(c2k((req, res, next) => next()));
    app.use(function * () { this.status = 200; });
    supertest(app.callback())
      .get('/')
      .expect(200)
      .end(done);
  });

  it('bubbles back to earlier middleware', (done) => {
    app.use(function * (next) {
      yield next;
      // Ensures that the following middleware is reached
      if ( this.status !== 200 ) {
        done(new Error('Never reached connect middleware'))
      }
      this.status = 201;
    });

    app.use(c2k((req, res) => res.statusCode = 200 ));

    supertest(app.callback())
      .get('/')
      .expect(201)
      .end(done);
  });

  it('receives errors from Connect middleware', (done) => {
    app.use(function * (next) {
      try {
        yield next;
      } catch (err) {
        this.status = 500;
      }
    })

    app.use(c2k((req, res, next) => {
      next(new Error('How Connect does error handling'));
    }));

    app.use(function * () {
      // Fail the test if this is reached
      done(new Error('Improper error handling'))
    })

    supertest(app.callback())
      .get('/')
      .expect(500)
      .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(function * (next) {
      yield next;
      if ( this.status !== 200 ) {
        done(new Error('Never reached connect middleware'));
      }
      // These calls won't end up doing anything
      // And will cause Koa to log a "Can't set headers after they are sent" error
      this.status = 500;
      this.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);
  });
})