-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtests.ts
138 lines (120 loc) · 3.63 KB
/
tests.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Koa, { Context } from 'koa';
import supertest from 'supertest';
import c2k from './index';
import assert from 'assert';
import { IncomingMessage, ServerResponse } from 'http';
import * as bodyParser from 'body-parser';
describe.skip('koa-connect', () => {
let app: Koa;
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: IncomingMessage, res: ServerResponse, next: () => void) {
next();
}
app.use(c2k(noop));
supertest(app.callback()).get('/').expect('Original').end(done);
});
it('works with two noop Connect middleware', (done) => {
function noop(req: IncomingMessage, res: ServerResponse, next: () => void) {
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: IncomingMessage, res: ServerResponse, next: () => void) {
next();
}
function goodStatusSetter(ctx: Context) {
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: IncomingMessage, res: ServerResponse) => {
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: IncomingMessage, res: ServerResponse) => {
res.statusCode = 200;
res.setHeader('Content-Length', message.length);
res.end(message);
})
);
supertest(app.callback()).get('/').expect(200).expect(message).end(done);
});
});
describe('integration tests', () => {
let app: Koa;
beforeEach(() => {
app = new Koa();
});
it('works with body-parser', (done) => {
const obj = { foo: '🦞' };
app.use(c2k(bodyParser.json()));
app.use((ctx, next) => {
// TODO fix types, remove need for any
const req = ctx.req as any;
assert(req.body.foo === obj.foo);
ctx.response.status = 200;
next();
});
supertest(app.callback()).post('/').send(obj).expect(200).end(done);
});
});