-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror_handler_test.go
More file actions
85 lines (74 loc) · 2.02 KB
/
error_handler_test.go
File metadata and controls
85 lines (74 loc) · 2.02 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
package stackr
import (
"bytes"
. "github.com/ricallinson/simplebdd"
"strings"
"testing"
)
func TestErrorHandler(t *testing.T) {
Describe("ErrorHandler()", func() {
var app *Server
var req *Request
var mock *MockResponseWriter
var res *Response
BeforeEach(func() {
app = CreateServer()
req = createRequest(NewMockHttpRequest())
mock = NewMockResponseWriter(false)
res = createResponse(mock)
})
It("should return [nil]", func() {
app.Use(ErrorHandler())
app.Use(func(req *Request, res *Response, next func()) {
res.End("nil")
})
app.Handle(req, res, 0)
w := bytes.NewBuffer(mock.Written).String()
AssertEqual(w, "nil")
})
It("should return [panic]", func() {
app.Env = "prod"
app.Use(ErrorHandler())
app.Use(func(req *Request, res *Response, next func()) {
panic("panic")
})
app.Handle(req, res, 0)
w := bytes.NewBuffer(mock.Written).String()
AssertEqual(w, "panic")
})
It("should return [34]", func() {
app.Env = "prod"
app.Use(ErrorHandler())
app.Use(func(req *Request, res *Response, next func()) {
panic("panic")
})
req.Header.Set("Accept", "text/html")
app.Handle(req, res, 0)
w := bytes.NewBuffer(mock.Written).String()
AssertEqual(strings.Index(w, "<title>panic</title>"), 34)
})
It("should return [67]", func() {
app.Env = "prod"
app.Use(ErrorHandler("Title"))
app.Use(func(req *Request, res *Response, next func()) {
panic("panic")
})
req.Header.Set("Accept", "text/html")
app.Handle(req, res, 0)
w := bytes.NewBuffer(mock.Written).String()
AssertEqual(strings.Index(w, "<h1>Title</h1>"), 67)
})
It("should return [{\"code\":\"500\",\"error\":\"panic\"}]", func() {
app.Env = "prod"
app.Use(ErrorHandler())
app.Use(func(req *Request, res *Response, next func()) {
panic("panic")
})
req.Header.Set("Accept", "application/json")
app.Handle(req, res, 0)
w := bytes.NewBuffer(mock.Written).String()
AssertEqual(w, "{\"code\":\"500\",\"error\":\"panic\"}")
})
})
Report(t)
}