-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
201 lines (158 loc) · 4.68 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const request = require('supertest');
const test = require('ava');
const Koa = require('koa');
const mount = require('koa-mount');
const serve = require('..');
// serve(root)
// when root = "."
test('should serve from cwd', async (t) => {
const app = new Koa();
app.use(serve('.'));
const res = await request(app.listen()).get('/package.json');
t.is(res.status, 200);
});
// when path is not a file
test('should 404', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen()).get('/something');
t.is(res.status, 404);
});
// when upstream middleware responds
test('should respond', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
app.use(async (ctx, next) => {
await next();
ctx.body = 'hey';
});
const res = await request(app.listen()).get('/hello.txt');
t.is(res.status, 200);
t.is(res.text, 'world');
});
// the path is valid
test('should serve the file', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen()).get('/hello.txt');
t.is(res.status, 200);
t.is(res.text, 'world');
});
// .index
// when present
test('should alter the index file supported', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures', { index: 'index.txt' }));
const res = await request(app.listen()).get('/');
t.is(res.status, 200);
t.is(res.headers['content-type'], 'text/plain; charset=utf-8');
t.is(res.text, 'text index');
});
// when added
test('should use index.html', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures', { index: 'index.html' }));
const res = await request(app.listen()).get('/world/');
t.is(res.status, 200);
t.is(res.headers['content-type'], 'text/html; charset=utf-8');
t.is(res.text, 'html index');
});
// by default
test('should not use index.html', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen()).get('/world/');
t.is(res.status, 404);
});
// when method is not `GET` or `HEAD`
test('when method is not GET or HEAD should 404', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen()).post('/hello.txt');
t.is(res.status, 404);
});
// option - format'
// when format: false'
test('when format false should 404', async (t) => {
const app = new Koa();
app.use(
serve('test/fixtures', {
index: 'index.html',
format: false
})
);
const res = await request(app.listen()).get('/world');
t.is(res.status, 404);
});
test('should 200', async (t) => {
const app = new Koa();
app.use(
serve('test/fixtures', {
index: 'index.html',
format: false
})
);
const res = await request(app.listen()).get('/world/');
t.is(res.status, 200);
});
// when format: true
test('when format true should 200', async (t) => {
const app = new Koa();
app.use(
serve('test/fixtures', {
index: 'index.html',
format: true
})
);
const res = await request(app.listen()).get('/world');
t.is(res.status, 200);
});
test('when format true (directory) should 200', async (t) => {
const app = new Koa();
app.use(
serve('test/fixtures', {
index: 'index.html',
format: true
})
);
const res = await request(app.listen()).get('/world/');
t.is(res.status, 200);
});
// Support if-modified-since
test('should 304', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen()).get('/world/index.html');
t.is(res.status, 200);
const lastModified = res.headers['last-modified'];
const newRes = await request(app.callback())
.get('/world/index.html')
.set('if-modified-since', lastModified);
t.is(newRes.status, 304);
});
test('support if-modified-since should 200', async (t) => {
const app = new Koa();
app.use(serve('test/fixtures'));
const res = await request(app.listen())
.get('/world/index.html')
.set('if-modified-since', 'Mon Jan 18 2011 23:04:34 GMT-0600');
t.is(res.status, 200);
});
// Work with koa-mount
test('should mount fine', async (t) => {
const app = new Koa();
app.use(
mount('/fixtures', serve(require('node:path').join(__dirname, '/fixtures')))
);
const res = await request(app.listen()).get('/fixtures/hello.txt');
t.is(res.status, 200);
});
// This is more of a test of js, than of the logic. But something we rely on
// Dates should truncate not, round
test('dates should truncate not round, should mount fine', (t) => {
const str = new Date().toUTCString();
let ms = Date.parse(str);
ms += 999; // Add 999 ms
const nd = new Date(ms);
t.is(nd.toUTCString(), str);
});