koa-lite/test/response/type.js

71 lines
2 KiB
JavaScript
Raw Normal View History

'use strict';
const context = require('../helpers/context');
const assert = require('assert');
describe('ctx.type=', function(){
describe('with a mime', function(){
it('should set the Content-Type', function(){
const ctx = context();
ctx.type = 'text/plain';
ctx.type.should.equal('text/plain');
ctx.response.header['content-type'].should.equal('text/plain; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('with an extension', function(){
it('should lookup the mime', function(){
const ctx = context();
ctx.type = 'json';
ctx.type.should.equal('application/json');
ctx.response.header['content-type'].should.equal('application/json; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('without a charset', function(){
it('should default the charset', function(){
const ctx = context();
ctx.type = 'text/html';
ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('with a charset', function(){
it('should not default the charset', function(){
const ctx = context();
ctx.type = 'text/html; charset=foo';
ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=foo');
2015-10-12 20:36:41 +00:00
});
});
describe('with an unknown extension', function(){
it('should not set a content-type', function(){
var 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', function(){
describe('with no Content-Type', function(){
2015-04-28 17:44:02 +00:00
it('should return ""', function(){
const ctx = context();
// TODO: this is lame
2015-04-28 17:44:02 +00:00
assert('' === ctx.type);
2015-10-12 20:36:41 +00:00
});
});
describe('with a Content-Type', function(){
it('should return the mime', function(){
const ctx = context();
ctx.type = 'json';
ctx.type.should.equal('application/json');
2015-10-12 20:36:41 +00:00
});
});
});