2014-10-01 12:42:29 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2016-07-23 17:20:29 +00:00
|
|
|
const Koa = require('../../');
|
|
|
|
const net = require('net');
|
2014-10-01 12:42:29 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('res.writable', () => {
|
2016-07-23 17:20:29 +00:00
|
|
|
describe('when continuous requests in one persistent connection', () => {
|
|
|
|
function requestTwice(server, done){
|
|
|
|
const port = server.address().port;
|
2019-04-16 10:05:18 +00:00
|
|
|
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
|
2016-07-23 17:20:29 +00:00
|
|
|
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 writable and response 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();
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(/request 1, writable: true/.test(responses), true);
|
|
|
|
assert.equal(/request 2, writable: true/.test(responses), true);
|
2016-07-23 17:20:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:42:29 +00:00
|
|
|
|
2016-07-23 17:20:29 +00:00
|
|
|
describe('when socket closed before response sent', () => {
|
2017-04-29 02:30:24 +00:00
|
|
|
function requestClosed(server){
|
2016-07-23 17:20:29 +00:00
|
|
|
const port = server.address().port;
|
2017-03-20 06:48:37 +00:00
|
|
|
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
|
2016-07-23 17:20:29 +00:00
|
|
|
const client = net.connect(port);
|
|
|
|
setImmediate(() => {
|
|
|
|
client.write(buf);
|
|
|
|
client.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not writable', done => {
|
|
|
|
const app = new Koa();
|
|
|
|
app.use(ctx => {
|
|
|
|
sleep(1000)
|
2019-06-26 03:15:22 +00:00
|
|
|
.then(() => {
|
|
|
|
if (ctx.writable) return done(new Error('ctx.writable should not be true'));
|
|
|
|
done();
|
|
|
|
});
|
2016-07-23 17:20:29 +00:00
|
|
|
});
|
|
|
|
const server = app.listen();
|
2017-04-29 02:30:24 +00:00
|
|
|
requestClosed(server);
|
2016-07-23 17:20:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when response finished', () => {
|
|
|
|
function request(server){
|
|
|
|
const port = server.address().port;
|
2017-03-20 06:48:37 +00:00
|
|
|
const buf = Buffer.from('GET / HTTP/1.1\r\nHost: localhost:' + port + '\r\nConnection: keep-alive\r\n\r\n');
|
2016-07-23 17:20:29 +00:00
|
|
|
const client = net.connect(port);
|
|
|
|
setImmediate(() => {
|
|
|
|
client.write(buf);
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
client.end();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should not 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);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-07-23 17:20:29 +00:00
|
|
|
|
|
|
|
function sleep(time){
|
|
|
|
return new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
}
|