-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresult_test.go
More file actions
82 lines (65 loc) · 1.73 KB
/
result_test.go
File metadata and controls
82 lines (65 loc) · 1.73 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
package hime
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func invokeHandler(h http.Handler, method string, target string, body io.Reader) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, body)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
func panicRecovery(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
}
}()
h.ServeHTTP(w, r)
})
}
func TestPanicInView(t *testing.T) {
t.Parallel()
t.Run("MinifyDisabled", func(t *testing.T) {
t.Parallel()
app := New()
app.TemplateFunc("panic", func() string { panic("panic") })
tmpl := app.Template()
tmpl.Dir("testdata")
tmpl.Root("root")
tmpl.ParseFiles("index", "panic.tmpl")
app.Handler(Handler(func(ctx *Context) error {
return ctx.View("index", nil)
}))
ts := httptest.NewServer(panicRecovery(app))
defer ts.Close()
resp, err := http.Get(ts.URL)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusInternalServerError)
})
t.Run("MinifyEnabled", func(t *testing.T) {
t.Parallel()
app := New()
app.TemplateFunc("panic", func() string { panic("panic") })
tmpl := app.Template()
tmpl.Dir("testdata")
tmpl.Root("root")
tmpl.ParseFiles("index", "panic.tmpl")
tmpl.Minify()
app.
Handler(Handler(func(ctx *Context) error {
return ctx.View("index", nil)
}))
ts := httptest.NewServer(panicRecovery(app))
defer ts.Close()
resp, err := http.Get(ts.URL)
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusInternalServerError)
})
}