koa-lite/test/response/flushHeaders.js

138 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-03-04 03:57:52 +00:00
'use strict';
const request = require('supertest');
const assert = require('assert');
const Koa = require('../..');
const http = require('http');
2016-03-04 03:57:52 +00:00
describe('ctx.flushHeaders()', () => {
2017-05-11 03:30:32 +00:00
it('should set headersSent', () => {
2016-03-04 03:57:52 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.body = 'Body';
ctx.status = 200;
ctx.flushHeaders();
2017-05-11 03:30:32 +00:00
assert.equal(ctx.res.headersSent, true);
2016-03-04 03:57:52 +00:00
});
const server = app.listen();
2017-05-11 03:30:32 +00:00
return request(server)
2016-03-04 03:57:52 +00:00
.get('/')
.expect(200)
2017-05-11 03:30:32 +00:00
.expect('Body');
2016-03-04 03:57:52 +00:00
});
2017-05-11 03:30:32 +00:00
it('should allow a response afterwards', () => {
2016-03-04 03:57:52 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.status = 200;
ctx.res.setHeader('Content-Type', 'text/plain');
ctx.flushHeaders();
ctx.body = 'Body';
});
const server = app.listen();
2017-05-11 03:30:32 +00:00
return request(server)
2016-03-04 03:57:52 +00:00
.get('/')
.expect(200)
.expect('Content-Type', 'text/plain')
2017-05-11 03:30:32 +00:00
.expect('Body');
2016-03-04 03:57:52 +00:00
});
2017-05-11 03:30:32 +00:00
it('should send the correct status code', () => {
2016-03-04 03:57:52 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.status = 401;
ctx.res.setHeader('Content-Type', 'text/plain');
ctx.flushHeaders();
ctx.body = 'Body';
});
const server = app.listen();
2017-05-11 03:30:32 +00:00
return request(server)
2016-03-04 03:57:52 +00:00
.get('/')
.expect(401)
.expect('Content-Type', 'text/plain')
2017-05-11 03:30:32 +00:00
.expect('Body');
2016-03-04 03:57:52 +00:00
});
2017-05-11 03:30:32 +00:00
it('should fail to set the headers after flushHeaders', async () => {
2016-03-04 03:57:52 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.status = 401;
ctx.res.setHeader('Content-Type', 'text/plain');
ctx.flushHeaders();
ctx.body = '';
try {
ctx.set('X-Shouldnt-Work', 'Value');
} catch (err) {
ctx.body += 'ctx.set fail ';
}
try {
ctx.status = 200;
} catch (err) {
ctx.body += 'ctx.status fail ';
}
try {
ctx.length = 10;
} catch (err) {
ctx.body += 'ctx.length fail';
}
});
const server = app.listen();
2017-05-11 03:30:32 +00:00
const res = await request(server)
2016-03-04 03:57:52 +00:00
.get('/')
.expect(401)
.expect('Content-Type', 'text/plain')
2017-05-11 03:30:32 +00:00
.expect('ctx.set fail ctx.status fail ctx.length fail');
assert.equal(res.headers['x-shouldnt-work'], undefined, 'header set after flushHeaders');
2016-03-04 03:57:52 +00:00
});
it('should flush headers first and delay to send data', done => {
const PassThrough = require('stream').PassThrough;
const app = new Koa();
app.use(ctx => {
ctx.type = 'json';
ctx.status = 200;
ctx.headers['Link'] = '</css/mycss.css>; as=style; rel=preload, <https://img.craftflair.com>; rel=preconnect; crossorigin';
const stream = ctx.body = new PassThrough();
ctx.flushHeaders();
setTimeout(() => {
stream.end(JSON.stringify({ message: 'hello!' }));
}, 10000);
});
app.listen(function(err){
if (err) return done(err);
const port = this.address().port;
http.request({
port
})
.on('response', res => {
const onData = () => done(new Error('boom'));
res.on('data', onData);
// shouldn't receive any data for a while
setTimeout(() => {
res.removeListener('data', onData);
done();
}, 1000);
})
.on('error', done)
.end();
});
});
2016-03-04 03:57:52 +00:00
});