-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
67 lines (53 loc) · 1.32 KB
/
handler_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package hime
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHandler(t *testing.T) {
t.Parallel()
t.Run("panic on error", func(t *testing.T) {
app := New()
app.Handler(Handler(func(ctx *Context) error {
return fmt.Errorf("must panic")
}))
assert.Panics(t, func() {
invokeHandler(app, "GET", "/", nil)
})
})
t.Run("net/http", func(t *testing.T) {
app := New()
app.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
assert.HTTPBodyContains(t, app.ServeHTTP, "GET", "/", nil, "ok")
})
t.Run("hime", func(t *testing.T) {
app := New()
app.Handler(Handler(func(ctx *Context) error {
return ctx.String("ok")
}))
assert.HTTPBodyContains(t, app.ServeHTTP, "GET", "/", nil, "ok")
})
t.Run("default handler", func(t *testing.T) {
app := New()
assert.HTTPBodyContains(t, app.ServeHTTP, "GET", "/", nil, "404")
})
t.Run("cancel context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
app := New()
app.Handler(Handler(func(ctx *Context) error {
return ctx.Err()
}))
r := httptest.NewRequest("GET", "/", nil)
r = r.WithContext(ctx)
w := httptest.NewRecorder()
cancel()
assert.NotPanics(t, func() {
app.ServeHTTP(w, r)
})
})
}