koa-lite/test/request/is.js

104 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
const context = require('../helpers/context');
const assert = require('assert');
describe('ctx.is(type)', () => {
it('should ignore params', () => {
const ctx = context();
ctx.header['content-type'] = 'text/html; charset=utf-8';
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
});
describe('when no body is given', () => {
it('should return null', () => {
const ctx = context();
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
});
});
describe('when no content type is given', () => {
it('should return false', () => {
const ctx = context();
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
});
});
describe('give no types', () => {
it('should return the mime type', () => {
const ctx = context();
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
});
});
describe('given one type', () => {
it('should return the type or false', () => {
const ctx = context();
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
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
});
});
describe('given multiple types', () => {
it('should return the first match or false', () => {
const ctx = context();
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
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
});
});
describe('when Content-Type: application/x-www-form-urlencoded', () => {
it('should match "urlencoded"', () => {
const ctx = context();
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
});
});
});