-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathparameter_registration_test.go
65 lines (56 loc) · 1.58 KB
/
parameter_registration_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
package fuego
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_RegisterOpenAPIOperation(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {}
s := NewServer()
t.Run("Nil operation handling", func(t *testing.T) {
route := NewRouteWithParams[struct{}, struct{}, struct{}](
http.MethodGet,
"/test",
handler,
s.Engine,
)
route.Operation = nil
err := route.RegisterParams()
require.NoError(t, err)
assert.NotNil(t, route.Operation)
})
t.Run("Register with params", func(t *testing.T) {
route := NewRouteWithParams[struct {
QueryParam string `query:"queryParam"`
HeaderParam string `header:"headerParam"`
}, struct{}, struct{}](
http.MethodGet,
"/some/path/{pathParam}",
handler,
s.Engine,
)
err := route.RegisterParams()
require.NoError(t, err)
operation := route.Operation
assert.NotNil(t, operation)
assert.Len(t, operation.Parameters, 2)
queryParam := operation.Parameters.GetByInAndName("query", "queryParam")
assert.NotNil(t, queryParam)
assert.Equal(t, "queryParam", queryParam.Name)
headerParam := operation.Parameters.GetByInAndName("header", "headerParam")
assert.NotNil(t, headerParam)
assert.Equal(t, "headerParam", headerParam.Name)
})
t.Run("RegisterParams should not with interfaces", func(t *testing.T) {
route := NewRouteWithParams[any, struct{}, struct{}](
http.MethodGet,
"/no-interfaces",
handler,
s.Engine,
OptionDefaultStatusCode(201),
)
err := route.RegisterParams()
require.Error(t, err)
})
}