2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2015-10-12 04:59:30 +00:00
|
|
|
const context = require('../helpers/context');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.accepts(types)', () => {
|
|
|
|
describe('with no arguments', () => {
|
|
|
|
describe('when Accept is populated', () => {
|
|
|
|
it('should return all accepted types', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-21 02:00:24 +00:00
|
|
|
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.deepEqual(ctx.accepts(), ['text/html', 'text/plain', 'image/jpeg', 'application/*']);
|
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('with no valid types', () => {
|
|
|
|
describe('when Accept is populated', () => {
|
|
|
|
it('should return false', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-21 02:00:24 +00:00
|
|
|
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('image/png', 'image/tiff'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-21 02:00:24 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when Accept is not populated', () => {
|
|
|
|
it('should return the first type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*'), 'text/html');
|
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('when extensions are given', () => {
|
|
|
|
it('should convert to mime types', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'text/plain, text/html';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('html'), 'html');
|
|
|
|
assert.equal(ctx.accepts('.html'), '.html');
|
|
|
|
assert.equal(ctx.accepts('txt'), 'txt');
|
|
|
|
assert.equal(ctx.accepts('.txt'), '.txt');
|
|
|
|
assert.equal(ctx.accepts('png'), 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('when an array is given', () => {
|
|
|
|
it('should return the first match', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'text/plain, text/html';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts(['png', 'text', 'html']), 'text');
|
|
|
|
assert.equal(ctx.accepts(['png', 'html']), 'html');
|
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('when multiple arguments are given', () => {
|
|
|
|
it('should return the first match', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'text/plain, text/html';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('png', 'text', 'html'), 'text');
|
|
|
|
assert.equal(ctx.accepts('png', 'html'), 'html');
|
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('when present in Accept as an exact match', () => {
|
|
|
|
it('should return the type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'text/plain, text/html';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('text/html'), 'text/html');
|
|
|
|
assert.equal(ctx.accepts('text/plain'), 'text/plain');
|
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('when present in Accept as a type match', () => {
|
|
|
|
it('should return the type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'application/json, */*';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('text/html'), 'text/html');
|
|
|
|
assert.equal(ctx.accepts('text/plain'), 'text/plain');
|
|
|
|
assert.equal(ctx.accepts('image/png'), '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('when present in Accept as a subtype match', () => {
|
|
|
|
it('should return the type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.accept = 'application/json, text/*';
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.accepts('text/html'), 'text/html');
|
|
|
|
assert.equal(ctx.accepts('text/plain'), 'text/plain');
|
|
|
|
assert.equal(ctx.accepts('image/png'), false);
|
|
|
|
assert.equal(ctx.accepts('png'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|