/
index.js
152 lines (131 loc) · 3.85 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var koa = require('koa')
var assert = require('assert')
var request = require('supertest')
var calculate = require('etag')
var fs = require('mz/fs')
var sendfile = require('..')
describe('when path is a directory', function () {
it('should 404', function (done) {
var app = koa()
app.use(function* (next) {
var stats = yield sendfile(this, __dirname)
assert.ok(stats)
})
request(app.listen())
.get('/')
.expect(404, done)
})
})
describe('when path does not exist', function () {
it('should 404', function (done) {
var app = koa()
app.use(function* (next) {
var stats = yield sendfile(this, __dirname + '/kljasdlkfjaklsdf')
assert.ok(!stats)
})
request(app.listen())
.get('/')
.expect(404, done)
})
})
describe('when path exists', function () {
describe('when GET', function () {
it('should send the file', function (done) {
var app = koa()
var stats, tag
tag = fs.stat(process.cwd() + '/test/fixture.txt')
app.use(function* (next) {
stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
tag = calculate(stats, {
weak: true
})
})
request(app.listen())
.get('/')
.expect(200)
.expect(/test/, function (err, res) {
if (err) return done(err)
assert(/text\/plain/.test(res.headers['content-type']))
res.headers['content-length'].should.equal(String(stats.size))
res.headers['last-modified'].should.equal(stats.mtime.toUTCString())
res.headers['etag'].should.equal(tag)
done()
})
})
it('should support weak etag 304', function (done) {
var app = koa()
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
var tag = calculate(stat, {
weak: true
})
app.use(function* (next) {
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
})
request(app.listen())
.get('/')
.set('If-None-Match', tag)
.expect(304, done)
})
})
it('should support last-modified 304', function (done) {
var app = koa()
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
app.use(function* (next) {
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
})
request(app.listen())
.get('/')
.set('If-Modified-Since', stat.mtime.toUTCString())
.expect(304, done)
})
})
it('should cleanup on socket error', function (done) {
var app = koa()
var stream
app.use(function* (next) {
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
stream = this.body
this.socket.emit('error', new Error('boom'))
})
request(app.listen())
.get('/')
.end(function (err) {
assert.ok(err)
assert.ok(stream.destroyed)
done()
})
})
})
describe('when HEAD', function () {
it('should 200', function (done) {
var app = koa()
app.use(function* (next) {
yield sendfile(this, process.cwd() + '/test/fixture.txt')
.then(assert.ok)
})
request(app.listen())
.head('/')
.expect(200)
.expect('content-type', /text\/plain/)
.expect('', done)
})
it('should 304', function (done) {
var app = koa()
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
app.use(function* (next) {
yield sendfile(this, process.cwd() + '/test/fixture.txt')
.then(assert.ok)
})
request(app.listen())
.head('/')
.set('If-Modified-Since', stat.mtime.toUTCString())
.expect(304)
.expect('', done)
})
})
})
})