-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
writable.js
95 lines (85 loc) · 2.79 KB
/
writable.js
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
'use strict';
const assert = require('assert');
const Koa = require('../../');
const net = require('net');
describe('res.writable', () => {
describe('when continuous requests in one persistent connection', () => {
function requestTwice(server, done){
const port = server.address().port;
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
const client = net.connect(port);
const datas = [];
client
.on('error', done)
.on('data', data => datas.push(data))
.on('end', () => done(null, datas));
setImmediate(() => client.write(buf));
setImmediate(() => client.write(buf));
setTimeout(() => client.end(), 100);
}
it('should always be writable and respond to all requests', done => {
const app = new Koa();
let count = 0;
app.use(ctx => {
count++;
ctx.body = 'request ' + count + ', writable: ' + ctx.writable;
});
const server = app.listen();
requestTwice(server, (_, datas) => {
const responses = Buffer.concat(datas).toString();
assert.equal(/request 1, writable: true/.test(responses), true);
assert.equal(/request 2, writable: true/.test(responses), true);
done();
});
});
});
describe('when socket closed before response sent', () => {
function requestClosed(server){
const port = server.address().port;
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
const client = net.connect(port);
setImmediate(() => {
client.write(buf);
client.end();
});
}
it('should not be writable', done => {
const app = new Koa();
app.use(ctx => {
sleep(1000)
.then(() => {
if (ctx.writable) return done(new Error('ctx.writable should not be true'));
done();
});
});
const server = app.listen();
requestClosed(server);
});
});
describe('when response finished', () => {
function request(server){
const port = server.address().port;
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
const client = net.connect(port);
setImmediate(() => {
client.write(buf);
});
setTimeout(() => {
client.end();
}, 100);
}
it('should not be writable', done => {
const app = new Koa();
app.use(ctx => {
ctx.res.end();
if (ctx.writable) return done(new Error('ctx.writable should not be true'));
done();
});
const server = app.listen();
request(server);
});
});
});
function sleep(time){
return new Promise(resolve => setTimeout(resolve, time));
}