koa-lite/test/response/header.js

47 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2017-05-11 03:30:32 +00:00
const assert = require('assert');
const request = require('supertest');
const response = require('../helpers/context').response;
const Koa = require('../..');
describe('res.header', () => {
it('should return the response header object', () => {
const res = response();
res.set('X-Foo', 'bar');
res.set('X-Number', 200);
assert.deepEqual(res.header, { 'x-foo': 'bar', 'x-number': '200' });
2015-10-12 20:36:41 +00:00
});
2014-10-01 12:42:29 +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' });
});
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.callback())
.get('/');
2017-05-11 03:30:32 +00:00
assert.deepEqual(header, { 'x-foo': '42' });
});
describe('when res._headers not present', () => {
it('should return empty object', () => {
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
});
});
});