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';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is('text/*'), '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
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is(), null);
|
|
|
|
assert.equal(ctx.is('image/*'), null);
|
|
|
|
assert.equal(ctx.is('image/*', 'text/*'), null);
|
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';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is(), false);
|
|
|
|
assert.equal(ctx.is('image/*'), false);
|
|
|
|
assert.equal(ctx.is('text/*', 'image/*'), 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';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is(), '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
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is('png'), 'png');
|
|
|
|
assert.equal(ctx.is('.png'), '.png');
|
|
|
|
assert.equal(ctx.is('image/png'), 'image/png');
|
|
|
|
assert.equal(ctx.is('image/*'), 'image/png');
|
|
|
|
assert.equal(ctx.is('*/png'), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(ctx.is('jpeg'), false);
|
|
|
|
assert.equal(ctx.is('.jpeg'), false);
|
|
|
|
assert.equal(ctx.is('image/jpeg'), false);
|
|
|
|
assert.equal(ctx.is('text/*'), false);
|
|
|
|
assert.equal(ctx.is('*/jpeg'), 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
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is('png'), 'png');
|
|
|
|
assert.equal(ctx.is('.png'), '.png');
|
|
|
|
assert.equal(ctx.is('text/*', 'image/*'), 'image/png');
|
|
|
|
assert.equal(ctx.is('image/*', 'text/*'), 'image/png');
|
|
|
|
assert.equal(ctx.is('image/*', 'image/png'), 'image/png');
|
|
|
|
assert.equal(ctx.is('image/png', 'image/*'), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(ctx.is(['text/*', 'image/*']), 'image/png');
|
|
|
|
assert.equal(ctx.is(['image/*', 'text/*']), 'image/png');
|
|
|
|
assert.equal(ctx.is(['image/*', 'image/png']), 'image/png');
|
|
|
|
assert.equal(ctx.is(['image/png', 'image/*']), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(ctx.is('jpeg'), false);
|
|
|
|
assert.equal(ctx.is('.jpeg'), false);
|
|
|
|
assert.equal(ctx.is('text/*', 'application/*'), false);
|
|
|
|
assert.equal(ctx.is('text/html', 'text/plain', 'application/json; charset=utf-8'), 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';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.is('urlencoded'), 'urlencoded');
|
|
|
|
assert.equal(ctx.is('json', 'urlencoded'), 'urlencoded');
|
|
|
|
assert.equal(ctx.is('urlencoded', 'json'), 'urlencoded');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|