forked from gocraft/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvalid_setup_test.go
More file actions
120 lines (90 loc) · 2.51 KB
/
invalid_setup_test.go
File metadata and controls
120 lines (90 loc) · 2.51 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package web
import (
"testing"
"github.com/stretchr/testify/assert"
)
func (c *Context) InvalidHandler() {}
func (c *Context) InvalidHandler2(w ResponseWriter, r *Request) string { return "" }
func (c *Context) InvalidHandler3(w ResponseWriter, r ResponseWriter) {}
type invalidSubcontext struct{}
func (c *invalidSubcontext) Handler(w ResponseWriter, r *Request) {}
type invalidSubcontext2 struct {
*invalidSubcontext
}
type contextInterface interface {
SignalMethod()
}
type interfaceImplementingContext struct{}
var _ contextInterface = (*interfaceImplementingContext)(nil)
func (ctx interfaceImplementingContext) SignalMethod() {}
func TestInvalidContext(t *testing.T) {
assert.Panics(t, func() {
New(1)
})
assert.Panics(t, func() {
router := New(Context{})
router.Subrouter(invalidSubcontext{}, "")
})
assert.Panics(t, func() {
router := New(Context{})
router.Subrouter(invalidSubcontext2{}, "")
})
}
func TestInvalidHandler(t *testing.T) {
router := New(Context{})
assert.Panics(t, func() {
router.Get("/action", 1)
})
assert.Panics(t, func() {
router.Get("/action", (*Context).InvalidHandler)
})
// Returns a string:
assert.Panics(t, func() {
router.Get("/action", (*Context).InvalidHandler2)
})
// Two writer inputs:
assert.Panics(t, func() {
router.Get("/action", (*Context).InvalidHandler3)
})
// Wrong context type:
assert.Panics(t, func() {
router.Get("/action", (*invalidSubcontext).Handler)
})
//
}
func TestPassContextByInterface(t *testing.T) {
router := New(interfaceImplementingContext{})
t.Run("TypesMatch", func(t *testing.T) {
assert.NotPanics(t, func() {
router.Get("/action", func(ctx *interfaceImplementingContext, w ResponseWriter, r *Request) {})
})
})
t.Run("BaseTypeMatchesInterface", func(t *testing.T) {
assert.NotPanics(t, func() {
router.Get("/action", func(ctx contextInterface, w ResponseWriter, r *Request) {})
})
})
}
func TestInvalidMiddleware(t *testing.T) {
router := New(Context{})
assert.Panics(t, func() {
router.Middleware((*Context).InvalidHandler)
})
}
func TestInvalidNotFound(t *testing.T) {
router := New(Context{})
assert.Panics(t, func() {
router.NotFound((*Context).InvalidHandler)
})
// Valid handler not on main router:
subrouter := router.Subrouter(Context{}, "")
assert.Panics(t, func() {
subrouter.NotFound((*Context).A)
})
}
func TestInvalidError(t *testing.T) {
router := New(Context{})
assert.Panics(t, func() {
router.Error((*Context).InvalidHandler)
})
}