Skip to content

Commit

Permalink
feat: add curl command in yaml (#929)
Browse files Browse the repository at this point in the history
* feat: add curl command in yaml

Signed-off-by: Akash Kumar <meakash7902@gmail.com>

* fix: make curl top level field in .yaml

Signed-off-by: Akash Kumar <meakash7902@gmail.com>

* fix: remove content-length from header in curl cmd

Signed-off-by: Akash Kumar <meakash7902@gmail.com>

---------

Signed-off-by: Akash Kumar <meakash7902@gmail.com>
  • Loading branch information
AkashKumar7902 committed Oct 3, 2023
1 parent d8afa82 commit 7947b9a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/platform/yaml/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ type NetworkTrafficDoc struct {
Kind models.Kind `json:"kind" yaml:"kind"`
Name string `json:"name" yaml:"name"`
Spec yamlLib.Node `json:"spec" yaml:"spec"`
Curl string `json:"curl" yaml:"curl"`
}

// func Encode(tc models.TestCase, logger *zap.Logger) (*NetworkTrafficDoc, []NetworkTrafficDoc, error) {
func EncodeTestcase(tc models.TestCase, logger *zap.Logger) (*NetworkTrafficDoc, error) {

header := pkg.ToHttpHeader(tc.HttpReq.Header)
curl := pkg.MakeCurlCommand(string(tc.HttpReq.Method), tc.HttpReq.URL, pkg.ToYamlHttpHeader(header), tc.HttpReq.Body)
doc := &NetworkTrafficDoc{
Version: tc.Version,
Kind: tc.Kind,
Name: tc.Name,
Curl: curl,
}
// mocks := []NetworkTrafficDoc{}
// find noisy fields
Expand Down
14 changes: 14 additions & 0 deletions pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,17 @@ func GenerateRandomID() int {
id := rand.Intn(1000000000) // Adjust the range as needed
return id
}

func MakeCurlCommand(method string, url string, header map[string]string, body string) string {
curl := fmt.Sprintf("curl --request %s \\\n", method)
curl = curl + fmt.Sprintf(" --url %s \\\n", url)
for k, v := range header {
if k != "Content-Length" {
curl = curl + fmt.Sprintf(" --header '%s: %s' \\\n", k, v)
}
}
if body != "" {
curl = curl + fmt.Sprintf(" --data '%s'", body)
}
return curl
}

0 comments on commit 7947b9a

Please sign in to comment.