-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
83 lines (67 loc) · 2.07 KB
/
test.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
/*!
* koa-better-serve <https://github.com/tunnckoCore/koa-better-serve>
*
* Copyright (c) 2017 Charlike Mike Reagent <[email protected]> (https://i.am.charlike.online)
* Released under the MIT license.
*/
const path = require('path')
const test = require('mukla') // eslint-disable-line
const Koa = require('koa')
const request = require('supertest')
const serve = require('./src/index')
const relative = (fp) => path.join(__dirname, fp)
test('1. throw error if `root` not a string', () => {
function fixture () {
serve(123)
}
test.throws(fixture, /TypeError/)
test.throws(fixture, /expect `root` to be string/)
})
test('2. throw error if `pathname` not a string or `options` object', () => {
function fixture () {
serve('./foo', 123)
}
test.throws(fixture, /TypeError/)
test.throws(fixture, /expect `pathname` to be string/)
})
test('3. accept `options` as second parameter', () => {
let app = new Koa()
app.use(
serve(__dirname, {
hidden: true,
})
)
return request(app.callback())
.get('/.travis.yml')
.expect(/language: node_js/)
.expect(200)
})
test('4. response 404 when `root` not exists', () => {
let app = new Koa()
app.use(serve('./not-exists', '/'))
return request(app.callback()).get('/LICENSE').expect(404, /Not Found/)
})
test('5. serve file from root when pathname `/pkg/`', () => {
let server = new Koa()
// both `/pkg/` and `pkg/` works
return request(server.use(serve(__dirname, 'pkg/')).callback())
.get('/pkg/package.json')
.expect(/name": "koa-better-serve"/)
.expect(200)
})
test('6. serve package.json from nested root', () => {
let app = new Koa()
app.use(serve(relative('node_modules/koa-send')))
return request(app.callback())
.get('/package.json')
.expect(/name": "koa-send"/)
.expect(200)
})
test('7. serve file when request has prefix', () => {
let app = new Koa()
app.use(serve(relative('node_modules/supertest'), '/some/foo/bar'))
return request(app.callback())
.get('/some/foo/bar/package.json')
.expect(/supertest/)
.expect(200)
})