-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
127 lines (108 loc) · 3.23 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var bufferEqual = require('buffer-equal');
var request = require('supertest');
var join = require('path').join;
var assert = require('assert');
var favicon = require('..');
var koa = require('koa');
var fs = require('fs');
describe('favicon()', function(){
var path = join(__dirname, 'fixtures', 'favicon.ico');
it('should only respond on /favicon.ico', function(done){
var app = koa();
app.use(favicon(path));
app.use(function *(next){
assert(!this.body);
assert(!this.get('Content-Type'));
this.body = 'hello';
});
request(app.listen())
.get('/')
.expect('hello', done);
});
it('should 404 if `path` is missing', function(done){
var app = koa();
app.use(favicon());
request(app.listen())
.post('/favicon.ico')
.expect(404, done);
});
it('should not accept POST requests', function(done){
var app = koa();
app.use(favicon(path));
request(app.listen())
.post('/favicon.ico')
.expect('Allow', 'GET, HEAD, OPTIONS')
.expect(405, done);
});
it('should send the favicon', function(done){
var body = fs.readFileSync(path);
var app = koa();
app.use(favicon(path));
request(app.listen())
.get('/favicon.ico')
.expect('Content-Type', 'image/x-icon')
.expect(200, function(err, res){
if (err) return done(err);
assert(bufferEqual(body, res.body));
done();
});
});
it('should set cache-control headers', function(done){
var app = koa();
app.use(favicon(path));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=86400')
.expect(200, done);
});
describe('options.maxAge', function(){
it('should set max-age', function(done){
var app = koa();
app.use(favicon(path, { maxAge: 5000 }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=5')
.expect(200, done);
});
it('should accept 0', function(done){
var app = koa();
app.use(favicon(path, { maxAge: 0 }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=0')
.expect(200, done);
});
it('should be valid delta-seconds', function(done){
var app = koa();
app.use(favicon(path, { maxAge: 1234 }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=1')
.expect(200, done);
});
it('should floor at 0', function(done){
var app = koa();
app.use(favicon(path, { maxAge: -4000 }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=0')
.expect(200, done);
});
it('should ceil at 31556926', function(done){
var app = koa();
app.use(favicon(path, { maxAge: 900000000000 }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=31556926')
.expect(200, done);
});
it('should accept Infinity', function(done){
var app = koa();
app.use(favicon(path, { maxAge: Infinity }));
request(app.listen())
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=31556926')
.expect(200, done);
});
})
})