2014-06-13 07:30:59 +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');
|
2014-06-13 07:30:59 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('response.is(type)', () => {
|
|
|
|
it('should ignore params', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
res.type = 'text/html; charset=utf-8';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is('text/*'), 'text/html');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-06-13 07:30:59 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when no type is set', () => {
|
|
|
|
it('should return false', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is(), false);
|
|
|
|
assert.equal(res.is('html'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-13 07:30:59 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when given no types', () => {
|
|
|
|
it('should return the type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
res.type = 'text/html; charset=utf-8';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is(), 'text/html');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-13 07:30:59 +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 res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
res.type = 'image/png';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is('png'), 'png');
|
|
|
|
assert.equal(res.is('.png'), '.png');
|
|
|
|
assert.equal(res.is('image/png'), 'image/png');
|
|
|
|
assert.equal(res.is('image/*'), 'image/png');
|
|
|
|
assert.equal(res.is('*/png'), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(res.is('jpeg'), false);
|
|
|
|
assert.equal(res.is('.jpeg'), false);
|
|
|
|
assert.equal(res.is('image/jpeg'), false);
|
|
|
|
assert.equal(res.is('text/*'), false);
|
|
|
|
assert.equal(res.is('*/jpeg'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-13 07:30:59 +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 res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
res.type = 'image/png';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is('png'), 'png');
|
|
|
|
assert.equal(res.is('.png'), '.png');
|
|
|
|
assert.equal(res.is('text/*', 'image/*'), 'image/png');
|
|
|
|
assert.equal(res.is('image/*', 'text/*'), 'image/png');
|
|
|
|
assert.equal(res.is('image/*', 'image/png'), 'image/png');
|
|
|
|
assert.equal(res.is('image/png', 'image/*'), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(res.is(['text/*', 'image/*']), 'image/png');
|
|
|
|
assert.equal(res.is(['image/*', 'text/*']), 'image/png');
|
|
|
|
assert.equal(res.is(['image/*', 'image/png']), 'image/png');
|
|
|
|
assert.equal(res.is(['image/png', 'image/*']), 'image/png');
|
|
|
|
|
|
|
|
assert.equal(res.is('jpeg'), false);
|
|
|
|
assert.equal(res.is('.jpeg'), false);
|
|
|
|
assert.equal(res.is('text/*', 'application/*'), false);
|
|
|
|
assert.equal(res.is('text/html', 'text/plain', 'application/json; charset=utf-8'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-06-13 07:30:59 +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 res = context().response;
|
2014-06-13 07:30:59 +00:00
|
|
|
res.type = 'application/x-www-form-urlencoded';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.is('urlencoded'), 'urlencoded');
|
|
|
|
assert.equal(res.is('json', 'urlencoded'), 'urlencoded');
|
|
|
|
assert.equal(res.is('urlencoded', 'json'), 'urlencoded');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|