Skip to content

Commit

Permalink
[handler] test handler invoke with data
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Dec 18, 2024
1 parent 24aa510 commit 7222df8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
26 changes: 26 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ paths:
summary: Returns info
x-cli-group: info

parameters:
- name: p1
required: true
in: path
description: The first param
schema:
type: integer
- name: p2
required: true
in: query
description: The second param
schema:
type: string
- name: p3
required: true
in: header
description: The third param
schema:
type: number
- name: p4
required: true
in: cookie
description: The fourth param
schema:
type: boolean

components:
schemas:
NumbersMap:
Expand Down
10 changes: 6 additions & 4 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ func parseExtensions(exts *orderedmap.Map[string, *yaml.Node]) (*extensions, err
}

func addParams(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerData) {
var (
queryParams []ParamMeta
pathParams []ParamMeta
headerParams []ParamMeta
cookieParams []ParamMeta
)
flags := cmd.Flags()
queryParams := []ParamMeta{}
pathParams := []ParamMeta{}
headerParams := []ParamMeta{}
cookieParams := []ParamMeta{}

for _, param := range op.Parameters {
schema := param.Schema.Schema()
Expand Down
8 changes: 7 additions & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ func TestBootstrapV3(t *testing.T) {
}

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

assertCmdTree(t, rootCmd, assertConf, rootCmd.Use)

rootCmd.SetArgs([]string{"info", "GetInfo", "--p1", "420", "--p2", "yes", "--p3", "420.69", "--p4", "true"})
rootCmd.Execute()
}

0 comments on commit 7222df8

Please sign in to comment.