forked from sashabaranov/go-fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_handler_test.go
More file actions
59 lines (54 loc) · 1.45 KB
/
validate_handler_test.go
File metadata and controls
59 lines (54 loc) · 1.45 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
package fastapi
import (
"testing"
"github.com/gin-gonic/gin"
)
func TestValidateHandlerInvalidCases(t *testing.T) {
tests := []struct {
name string
handler interface{}
}{
{
name: "Wrong number of arguments",
handler: func(_ struct{}) struct{} { return struct{}{} },
},
{
name: "Wrong number of return values",
handler: func(_ *gin.Context, _ struct{}) {},
},
{
name: "First argument not *gin.Context",
handler: func(_ struct{}, _ *gin.Context) (struct{}, error) { return struct{}{}, nil },
},
{
name: "Second argument not a struct",
handler: func(_ *gin.Context, _ string) (struct{}, error) { return struct{}{}, nil },
},
{
name: "Second return value not an error",
handler: func(_ *gin.Context, _ struct{}) (struct{}, string) { return struct{}{}, "" },
},
{
name: "First return value not a struct",
handler: func(_ *gin.Context, _ struct{}) (string, error) { return "", nil },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("validateHandler did not panic for case: %s", tt.name)
}
}()
validateHandler(tt.handler)
})
}
}
func TestValidateHandlerValidCase(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("validateHandler panicked for valid handler")
}
}()
validateHandler(func(_ *gin.Context, _ struct{}) (struct{}, error) { return struct{}{}, nil })
}