2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2017-03-08 06:59:24 +00:00
|
|
|
const request = require('supertest');
|
2015-10-12 04:59:30 +00:00
|
|
|
const response = require('../helpers/context').response;
|
2017-03-08 06:59:24 +00:00
|
|
|
const Koa = require('../..');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('res.header', () => {
|
|
|
|
it('should return the response header object', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2013-11-13 17:01:15 +00:00
|
|
|
res.set('X-Foo', 'bar');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(res.header, { 'x-foo': 'bar' });
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-10-01 12:42:29 +00:00
|
|
|
|
2017-03-08 06:59:24 +00:00
|
|
|
it('should use res.getHeaders() accessor when available', () => {
|
|
|
|
const res = response();
|
|
|
|
res.res._headers = null;
|
|
|
|
res.res.getHeaders = () => ({ 'x-foo': 'baz' });
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(res.header, { 'x-foo': 'baz' });
|
2017-03-08 06:59:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the response header object when no mocks are in use', async () => {
|
|
|
|
const app = new Koa();
|
|
|
|
let header;
|
|
|
|
|
|
|
|
app.use(ctx => {
|
|
|
|
ctx.set('x-foo', '42');
|
|
|
|
header = Object.assign({}, ctx.response.header);
|
|
|
|
});
|
|
|
|
|
|
|
|
await request(app.listen())
|
|
|
|
.get('/');
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(header, { 'x-foo': '42' });
|
2017-03-08 06:59:24 +00:00
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when res._headers not present', () => {
|
|
|
|
it('should return empty object', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = response();
|
2014-10-01 12:42:29 +00:00
|
|
|
res.res._headers = null;
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(res.header, {});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|