koa-lite/test/response/is.js

87 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2014-06-13 07:30:59 +00:00
'use strict';
const context = require('../helpers/context');
const assert = require('assert');
2014-06-13 07:30:59 +00:00
describe('response.is(type)', () => {
it('should ignore params', () => {
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
describe('when no type is set', () => {
it('should return false', () => {
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
describe('when given no types', () => {
it('should return the type', () => {
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
describe('given one type', () => {
it('should return the type or false', () => {
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
describe('given multiple types', () => {
it('should return the first match or false', () => {
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
describe('when Content-Type: application/x-www-form-urlencoded', () => {
it('should match "urlencoded"', () => {
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
});
});
});