2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-12 04:59:30 +00:00
|
|
|
const context = require('../helpers/context');
|
2015-10-05 18:23:47 +00:00
|
|
|
const assert = require('assert');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.is(type)', () => {
|
|
|
|
it('should ignore params', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
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');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-28 08:13:13 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when no body is given', () => {
|
|
|
|
it('should return null', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-28 08:13:13 +00:00
|
|
|
|
2016-12-01 10:16:02 +00:00
|
|
|
assert(null === ctx.is());
|
|
|
|
assert(null === ctx.is('image/*'));
|
|
|
|
assert(null === ctx.is('image/*', 'text/*'));
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-28 08:13:13 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when no content type is given', () => {
|
|
|
|
it('should return false', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
|
|
|
ctx.is().should.be.false;
|
|
|
|
ctx.is('image/*').should.be.false;
|
|
|
|
ctx.is('text/*', 'image/*').should.be.false;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-28 08:13:13 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('give no types', () => {
|
|
|
|
it('should return the mime type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['content-type'] = 'image/png';
|
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
|
|
|
ctx.is().should.equal('image/png');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('given one type', () => {
|
|
|
|
it('should return the type or false', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
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-12-28 02:53:19 +00:00
|
|
|
ctx.is('png').should.equal('png');
|
|
|
|
ctx.is('.png').should.equal('.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;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('given multiple types', () => {
|
|
|
|
it('should return the first match or false', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
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-12-28 02:53:19 +00:00
|
|
|
ctx.is('png').should.equal('png');
|
|
|
|
ctx.is('.png').should.equal('.png');
|
2013-11-28 20:47:23 +00:00
|
|
|
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
|
|
|
|
2014-07-06 08:43:14 +00:00
|
|
|
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');
|
|
|
|
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;
|
2014-06-04 04:44:25 +00:00
|
|
|
ctx.is('text/html', 'text/plain', 'application/json; charset=utf-8').should.be.false;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-28 08:13:13 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when Content-Type: application/x-www-form-urlencoded', () => {
|
|
|
|
it('should match "urlencoded"', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-28 08:13:13 +00:00
|
|
|
ctx.header['content-type'] = 'application/x-www-form-urlencoded';
|
|
|
|
ctx.header['transfer-encoding'] = 'chunked';
|
|
|
|
|
2013-12-28 02:53:19 +00:00
|
|
|
ctx.is('urlencoded').should.equal('urlencoded');
|
|
|
|
ctx.is('json', 'urlencoded').should.equal('urlencoded');
|
|
|
|
ctx.is('urlencoded', 'json').should.equal('urlencoded');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|