-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathshow_errors_test.go
51 lines (40 loc) · 1.27 KB
/
show_errors_test.go
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
package mango
import (
"net/http"
"testing"
)
func showErrorsTestServer(env Env) (Status, Headers, Body) {
panic("foo!")
return 200, Headers{}, Body("Hello World!")
}
func TestShowErrors(t *testing.T) {
// Compile the stack
showErrorsStack := new(Stack)
showErrorsStack.Middleware(ShowErrors("<html><body>{{.Error|html}}</body></html>"))
showErrorsApp := showErrorsStack.Compile(showErrorsTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/", nil)
status, _, body := showErrorsApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 500 {
t.Error("Expected status to equal 500, got:", status)
}
expected := "<html><body>foo!</body></html>"
if string(body) != expected {
t.Error("Expected response body to equal: \"", expected, "\" got: \"", string(body), "\"")
}
}
func BenchmarkShowErrors(b *testing.B) {
b.StopTimer()
showErrorsStack := new(Stack)
showErrorsStack.Middleware(ShowErrors("<html><body>{Error|html}</body></html>"))
showErrorsApp := showErrorsStack.Compile(showErrorsTestServer)
request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
showErrorsApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}