forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-pages-test.js
More file actions
98 lines (82 loc) · 3.03 KB
/
error-pages-test.js
File metadata and controls
98 lines (82 loc) · 3.03 KB
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
'use strict'
const chai = require('chai')
const sinon = require('sinon')
const { expect } = chai
chai.use(require('sinon-chai'))
chai.use(require('dirty-chai'))
chai.should()
const errorPages = require('../../lib/handlers/error-pages')
describe('handlers/error-pages', () => {
describe('handler()', () => {
it('should use the custom error handler if available', () => {
const ldp = { errorHandler: sinon.stub() }
const req = { app: { locals: { ldp } } }
const res = { status: sinon.stub(), send: sinon.stub() }
const err = {}
const next = {}
errorPages.handler(err, req, res, next)
expect(ldp.errorHandler).to.have.been.calledWith(err, req, res, next)
expect(res.status).to.not.have.been.called()
expect(res.send).to.not.have.been.called()
})
it('defaults to status code 500 if none is specified in the error', () => {
const ldp = { noErrorPages: true }
const req = { app: { locals: { ldp } } }
const res = { status: sinon.stub(), send: sinon.stub(), header: sinon.stub() }
const err = { message: 'Unspecified error' }
const next = {}
errorPages.handler(err, req, res, next)
expect(res.status).to.have.been.calledWith(500)
expect(res.header).to.have.been.calledWith('Content-Type', 'text/plain;charset=utf-8')
expect(res.send).to.have.been.calledWith('Unspecified error\n')
})
})
describe('sendErrorResponse()', () => {
it('should send http status code and error message', () => {
const statusCode = 404
const error = {
message: 'Error description'
}
const res = {
status: sinon.stub(),
header: sinon.stub(),
send: sinon.stub()
}
errorPages.sendErrorResponse(statusCode, res, error)
expect(res.status).to.have.been.calledWith(404)
expect(res.header).to.have.been.calledWith('Content-Type', 'text/plain;charset=utf-8')
expect(res.send).to.have.been.calledWith('Error description\n')
})
})
describe('setAuthenticateHeader()', () => {
it('should do nothing for a non-implemented auth method', () => {
const err = {}
const req = {
app: { locals: { authMethod: null } }
}
const res = {
set: sinon.stub()
}
errorPages.setAuthenticateHeader(req, res, err)
expect(res.set).to.not.have.been.called()
})
})
describe('sendErrorPage()', () => {
it('falls back the default sendErrorResponse if no page is found', () => {
const statusCode = 400
const res = {
status: sinon.stub(),
header: sinon.stub(),
send: sinon.stub()
}
const err = { message: 'Error description' }
const ldp = { errorPages: './' }
return errorPages.sendErrorPage(statusCode, res, err, ldp)
.then(() => {
expect(res.status).to.have.been.calledWith(400)
expect(res.header).to.have.been.calledWith('Content-Type', 'text/plain;charset=utf-8')
expect(res.send).to.have.been.calledWith('Error description\n')
})
})
})
})