2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
var context = require('../context');
|
2013-11-28 20:47:23 +00:00
|
|
|
var should = require('should');
|
|
|
|
var assert = require('assert');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('ctx.is(type)', function(){
|
|
|
|
it('should ignore params', function(){
|
|
|
|
var ctx = context();
|
|
|
|
ctx.header['content-type'] = 'text/html; charset=utf-8';
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
2013-11-28 20:47:23 +00:00
|
|
|
ctx.is('text/*').should.equal('text/html');
|
2013-11-28 08:13:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when no body is given', function(){
|
|
|
|
it('should return null', function(){
|
|
|
|
var ctx = context();
|
|
|
|
|
2013-11-28 20:47:23 +00:00
|
|
|
assert(null == ctx.is());
|
|
|
|
assert(null == ctx.is('image/*'));
|
|
|
|
assert(null == ctx.is('image/*', 'text/*'));
|
2013-11-28 08:13:13 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when no content type is given', function(){
|
|
|
|
it('should return false', function(){
|
|
|
|
var ctx = context();
|
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
|
|
|
ctx.is().should.be.false;
|
|
|
|
ctx.is('image/*').should.be.false;
|
|
|
|
ctx.is('text/*', 'image/*').should.be.false;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('give no types', function(){
|
|
|
|
it('should return the mime type', function(){
|
|
|
|
var ctx = context();
|
|
|
|
ctx.header['content-type'] = 'image/png';
|
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
|
|
|
ctx.is().should.equal('image/png');
|
|
|
|
})
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
|
2013-11-28 08:13:13 +00:00
|
|
|
describe('given one type', function(){
|
|
|
|
it('should return the type or false', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
var ctx = context();
|
|
|
|
ctx.header['content-type'] = 'image/png';
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-28 20:47:23 +00:00
|
|
|
ctx.is('png').should.equal('image/png');
|
|
|
|
ctx.is('.png').should.equal('image/png');
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.is('image/png').should.equal('image/png');
|
2013-11-28 20:47:23 +00:00
|
|
|
ctx.is('image/*').should.equal('image/png');
|
|
|
|
ctx.is('*/png').should.equal('image/png');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.is('jpeg').should.be.false;
|
|
|
|
ctx.is('.jpeg').should.be.false;
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.is('image/jpeg').should.be.false;
|
|
|
|
ctx.is('text/*').should.be.false;
|
|
|
|
ctx.is('*/jpeg').should.be.false;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-11-28 08:13:13 +00:00
|
|
|
describe('given multiple types', function(){
|
|
|
|
it('should return the first match or false', function(){
|
2013-11-13 17:01:15 +00:00
|
|
|
var ctx = context();
|
|
|
|
ctx.header['content-type'] = 'image/png';
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2013-11-28 20:47:23 +00:00
|
|
|
ctx.is('png').should.equal('image/png');
|
|
|
|
ctx.is('.png').should.equal('image/png');
|
|
|
|
ctx.is('text/*', 'image/*').should.equal('image/png');
|
|
|
|
ctx.is('image/*', 'text/*').should.equal('image/png');
|
|
|
|
ctx.is('image/*', 'image/png').should.equal('image/png');
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.is('image/png', 'image/*').should.equal('image/png');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
ctx.is('jpeg').should.be.false;
|
|
|
|
ctx.is('.jpeg').should.be.false;
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.is('text/*', 'application/*').should.be.false;
|
|
|
|
ctx.is('text/html', 'text/plain', 'application/json').should.be.false;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when Content-Type: application/x-www-form-urlencoded', function(){
|
2013-11-28 20:47:23 +00:00
|
|
|
it('should match "urlencoded"', function(){
|
2013-11-28 08:13:13 +00:00
|
|
|
var ctx = context();
|
|
|
|
ctx.header['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
2013-11-28 20:47:23 +00:00
|
|
|
ctx.is('urlencoded').should.equal('application/x-www-form-urlencoded');
|
|
|
|
ctx.is('json', 'urlencoded').should.equal('application/x-www-form-urlencoded');
|
|
|
|
ctx.is('urlencoded', 'json').should.equal('application/x-www-form-urlencoded');
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|