koa-lite/test/response/type.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
const context = require('../helpers/context');
const assert = require('assert');
describe('ctx.type=', () => {
describe('with a mime', () => {
it('should set the Content-Type', () => {
const ctx = context();
ctx.type = 'text/plain';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.type, 'text/plain');
assert.equal(ctx.response.header['content-type'], 'text/plain; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('with an extension', () => {
it('should lookup the mime', () => {
const ctx = context();
ctx.type = 'json';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.type, 'application/json');
assert.equal(ctx.response.header['content-type'], 'application/json; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('without a charset', () => {
it('should default the charset', () => {
const ctx = context();
ctx.type = 'text/html';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.type, 'text/html');
assert.equal(ctx.response.header['content-type'], 'text/html; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('with a charset', () => {
it('should not default the charset', () => {
const ctx = context();
ctx.type = 'text/html; charset=foo';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.type, 'text/html');
assert.equal(ctx.response.header['content-type'], 'text/html; charset=foo');
2015-10-12 20:36:41 +00:00
});
});
describe('with an unknown extension', () => {
it('should not set a content-type', () => {
2015-10-22 22:46:47 +00:00
const ctx = context();
ctx.type = 'asdf';
assert(!ctx.type);
assert(!ctx.response.header['content-type']);
2015-10-12 20:36:41 +00:00
});
});
});
describe('ctx.type', () => {
describe('with no Content-Type', () => {
it('should return ""', () => {
const ctx = context();
assert(!ctx.type);
2015-10-12 20:36:41 +00:00
});
});
describe('with a Content-Type', () => {
it('should return the mime', () => {
const ctx = context();
ctx.type = 'json';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.type, 'application/json');
2015-10-12 20:36:41 +00:00
});
});
});