Skip to content

Commit 7222df8

Browse files
committed
[handler] test handler invoke with data
1 parent 24aa510 commit 7222df8

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

api.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,32 @@ paths:
6060
summary: Returns info
6161
x-cli-group: info
6262

63+
parameters:
64+
- name: p1
65+
required: true
66+
in: path
67+
description: The first param
68+
schema:
69+
type: integer
70+
- name: p2
71+
required: true
72+
in: query
73+
description: The second param
74+
schema:
75+
type: string
76+
- name: p3
77+
required: true
78+
in: header
79+
description: The third param
80+
schema:
81+
type: number
82+
- name: p4
83+
required: true
84+
in: cookie
85+
description: The fourth param
86+
schema:
87+
type: boolean
88+
6389
components:
6490
schemas:
6591
NumbersMap:

lib.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ func parseExtensions(exts *orderedmap.Map[string, *yaml.Node]) (*extensions, err
8282
}
8383

8484
func addParams(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerData) {
85+
var (
86+
queryParams []ParamMeta
87+
pathParams []ParamMeta
88+
headerParams []ParamMeta
89+
cookieParams []ParamMeta
90+
)
8591
flags := cmd.Flags()
86-
queryParams := []ParamMeta{}
87-
pathParams := []ParamMeta{}
88-
headerParams := []ParamMeta{}
89-
cookieParams := []ParamMeta{}
9092

9193
for _, param := range op.Parameters {
9294
schema := param.Schema.Schema()

lib_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func TestBootstrapV3(t *testing.T) {
8484
}
8585

8686
handler := func(opts *cobra.Command, args []string, data HandlerData) {
87-
// TODO: test handlers when?
87+
assert.Equal(t, data.PathParams, []ParamMeta{{Name: "p1", Type: Integer}})
88+
assert.Equal(t, data.QueryParams, []ParamMeta{{Name: "p2", Type: String}})
89+
assert.Equal(t, data.HeaderParams, []ParamMeta{{Name: "p3", Type: Number}})
90+
assert.Equal(t, data.CookieParams, []ParamMeta{{Name: "p4", Type: Boolean}})
8891
}
8992
rootCmd := &cobra.Command{
9093
Use: "calc",
@@ -156,4 +159,7 @@ func TestBootstrapV3(t *testing.T) {
156159
}
157160

158161
assertCmdTree(t, rootCmd, assertConf, rootCmd.Use)
162+
163+
rootCmd.SetArgs([]string{"info", "GetInfo", "--p1", "420", "--p2", "yes", "--p3", "420.69", "--p4", "true"})
164+
rootCmd.Execute()
159165
}

0 commit comments

Comments
 (0)