-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
validate_test.go
142 lines (111 loc) · 2.76 KB
/
validate_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package huma
import (
"net/http"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestOperationDescriptionRequired(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.register(http.MethodGet, "/", &openAPIOperation{})
})
}
func TestOperationResponseRequired(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.register(http.MethodGet, "/", &openAPIOperation{
description: "Test",
})
})
}
func TestOperationHandlerInput(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
SimpleDependency("test"),
ResponseText(200, "Test"),
).Get("Test", func() string {
// Wrong number of inputs!
return "fails"
})
})
}
func TestOperationHandlerOutput(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
ResponseHeader("x-test", "Test"),
ResponseText(200, "Test", Headers("x-test")),
).Get("Test", func() string {
// Wrong number of outputs!
return "fails"
})
})
}
func TestOperationListAutoID(t *testing.T) {
r := NewTestRouter(t)
r.Resource("/items").Get("Test", func() []string {
return []string{"test"}
})
o := r.api.Paths["/items"][http.MethodGet]
assert.Equal(t, "list-items", o.id)
}
func TestOperationContextPointer(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
GinContextDependency(),
).Get("Test", func(c gin.Context) string {
return "test"
})
})
}
func TestOperationInvalidDep(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
SimpleDependency(nil),
).Get("Test", func(o openAPIOperation) string {
return "test"
})
})
}
func TestOperationParamDep(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
QueryParam("foo", "Test", ""),
).Get("Test", func(c *gin.Context) string {
return "test"
})
})
}
func TestOperationParamRedeclare(t *testing.T) {
r := NewTestRouter(t)
param := QueryParam("foo", "Test", 0)
r.Resource("/a", param).Get("Test", func(p int) string { return "a" })
// Redeclare param `p` as a string while it was an int above.
assert.Panics(t, func() {
r.Resource("/b", param).Get("Test", func(p string) string { return "b" })
})
}
func TestOperationParamExampleType(t *testing.T) {
r := NewTestRouter(t)
assert.Panics(t, func() {
r.Resource("/",
QueryParam("foo", "Test", "", Example(123)),
).Get("Test", func(p string) string {
return "test"
})
})
}
func TestOperationParamExampleSchema(t *testing.T) {
r := NewTestRouter(t)
p := QueryParam("foo", "Test", 0, Example(123))
r.Resource("/", p).Get("Test", func(p int) string {
return "test"
})
param := r.api.Paths["/"][http.MethodGet].params[0]
assert.Equal(t, 123, param.Schema.Example)
}