koa-lite/test/request/acceptsCharsets.js

53 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2017-05-11 03:30:32 +00:00
const assert = require('assert');
const context = require('../helpers/context');
describe('ctx.acceptsCharsets()', () => {
describe('with no arguments', () => {
describe('when Accept-Charset is populated', () => {
it('should return accepted types', () => {
const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
2017-05-11 03:30:32 +00:00
assert.deepEqual(ctx.acceptsCharsets(), ['utf-8', 'utf-7', 'iso-8859-1']);
2015-10-12 20:36:41 +00:00
});
});
});
describe('with multiple arguments', () => {
describe('when Accept-Charset is populated', () => {
describe('if any types match', () => {
it('should return the best fit', () => {
const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.acceptsCharsets('utf-7', 'utf-8'), 'utf-8');
2015-10-12 20:36:41 +00:00
});
});
describe('if no types match', () => {
it('should return false', () => {
const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.acceptsCharsets('utf-16'), false);
2015-10-12 20:36:41 +00:00
});
});
});
describe('when Accept-Charset is not populated', () => {
it('should return the first type', () => {
const ctx = context();
2017-05-11 03:30:32 +00:00
assert.equal(ctx.acceptsCharsets('utf-7', 'utf-8'), 'utf-7');
2015-10-12 20:36:41 +00:00
});
});
});
describe('with an array', () => {
it('should return the best fit', () => {
const ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
2017-05-11 03:30:32 +00:00
assert.equal(ctx.acceptsCharsets(['utf-7', 'utf-8']), 'utf-8');
2015-10-12 20:36:41 +00:00
});
});
});