-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
accepts.js
93 lines (83 loc) · 3.37 KB
/
accepts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict'
const assert = require('assert')
const context = require('../../test-helpers/context')
describe('ctx.accepts(types)', () => {
describe('with no arguments', () => {
describe('when Accept is populated', () => {
it('should return all accepted types', () => {
const ctx = context()
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'
assert.deepStrictEqual(ctx.accepts(), ['text/html', 'text/plain', 'image/jpeg', 'application/*'])
})
})
})
describe('with no valid types', () => {
describe('when Accept is populated', () => {
it('should return false', () => {
const ctx = context()
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'
assert.strictEqual(ctx.accepts('image/png', 'image/tiff'), false)
})
})
describe('when Accept is not populated', () => {
it('should return the first type', () => {
const ctx = context()
assert.strictEqual(ctx.accepts('text/html', 'text/plain', 'image/jpeg', 'application/*'), 'text/html')
})
})
})
describe('when extensions are given', () => {
it('should convert to mime types', () => {
const ctx = context()
ctx.req.headers.accept = 'text/plain, text/html'
assert.strictEqual(ctx.accepts('html'), 'html')
assert.strictEqual(ctx.accepts('.html'), '.html')
assert.strictEqual(ctx.accepts('txt'), 'txt')
assert.strictEqual(ctx.accepts('.txt'), '.txt')
assert.strictEqual(ctx.accepts('png'), false)
})
})
describe('when an array is given', () => {
it('should return the first match', () => {
const ctx = context()
ctx.req.headers.accept = 'text/plain, text/html'
assert.strictEqual(ctx.accepts(['png', 'text', 'html']), 'text')
assert.strictEqual(ctx.accepts(['png', 'html']), 'html')
})
})
describe('when multiple arguments are given', () => {
it('should return the first match', () => {
const ctx = context()
ctx.req.headers.accept = 'text/plain, text/html'
assert.strictEqual(ctx.accepts('png', 'text', 'html'), 'text')
assert.strictEqual(ctx.accepts('png', 'html'), 'html')
})
})
describe('when value present in Accept is an exact match', () => {
it('should return the type', () => {
const ctx = context()
ctx.req.headers.accept = 'text/plain, text/html'
assert.strictEqual(ctx.accepts('text/html'), 'text/html')
assert.strictEqual(ctx.accepts('text/plain'), 'text/plain')
})
})
describe('when value present in Accept is a type match', () => {
it('should return the type', () => {
const ctx = context()
ctx.req.headers.accept = 'application/json, */*'
assert.strictEqual(ctx.accepts('text/html'), 'text/html')
assert.strictEqual(ctx.accepts('text/plain'), 'text/plain')
assert.strictEqual(ctx.accepts('image/png'), 'image/png')
})
})
describe('when value present in Accept is a subtype match', () => {
it('should return the type', () => {
const ctx = context()
ctx.req.headers.accept = 'application/json, text/*'
assert.strictEqual(ctx.accepts('text/html'), 'text/html')
assert.strictEqual(ctx.accepts('text/plain'), 'text/plain')
assert.strictEqual(ctx.accepts('image/png'), false)
assert.strictEqual(ctx.accepts('png'), false)
})
})
})