Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the shell, python generator is incorrect #601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
5 changes: 5 additions & 0 deletions pkg/generator/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func generate(testsuite *testing.TestSuite, testcase *testing.TestCase, template
}
}
}
if testcase != nil {
if err = testcase.Request.Render(nil, ""); err != nil {
return
}
}
var tpl *template.Template
if tpl, err = template.New(templateName).
Funcs(template.FuncMap{"safeString": safeString}).
Expand Down
63 changes: 33 additions & 30 deletions pkg/generator/curl_generator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,53 +16,56 @@ limitations under the License.
package generator

import (
"bytes"
_ "embed"
"net/http"
"strings"
"text/template"
"bytes"
_ "embed"
"net/http"
"strings"
"text/template"

"github.com/linuxsuren/api-testing/pkg/testing"
"github.com/linuxsuren/api-testing/pkg/testing"
)

type curlGenerator struct {
}

func NewCurlGenerator() CodeGenerator {
return &curlGenerator{}
return &curlGenerator{}
}

func (g *curlGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
if testcase.Request.Method == "" {
testcase.Request.Method = http.MethodGet
}
if testcase.Request.Method == "" {
testcase.Request.Method = http.MethodGet
}

if !strings.HasSuffix(testcase.Request.API, "?") {
testcase.Request.API += "?"
}
if !strings.HasSuffix(testcase.Request.API, "?") {
testcase.Request.API += "?"
}

queryKeys := testcase.Request.Query.Keys()
for _, k := range queryKeys {
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
}
queryKeys := testcase.Request.Query.Keys()
for _, k := range queryKeys {
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
}

testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
if err = testcase.Request.Render(nil, ""); err != nil {
return
}

var tpl *template.Template
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
buf := new(bytes.Buffer)
if err = tpl.Execute(buf, testcase); err == nil {
result = strings.TrimSpace(buf.String())
var tpl *template.Template
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
buf := new(bytes.Buffer)
if err = tpl.Execute(buf, testcase); err == nil {
result = strings.TrimSpace(buf.String())

result = strings.ReplaceAll(result, "\n", " \\\n")
}
}
return
result = strings.TrimSuffix(result, " \\")
}
}
return
}

func init() {
RegisterCodeGenerator("curl", NewCurlGenerator())
RegisterCodeGenerator("curl", NewCurlGenerator())
}

//go:embed data/curl.tpl
Expand Down
24 changes: 18 additions & 6 deletions pkg/generator/curl_generator_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2025 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,7 +35,7 @@ func TestCurlGenerator(t *testing.T) {
API: fooForTest,
},
},
expect: `curl -X GET 'http://foo'`,
expect: `curl -k -X GET 'http://foo'`,
}, {
name: "has query string",
testCase: atest.TestCase{
Expand All @@ -47,7 +47,7 @@ func TestCurlGenerator(t *testing.T) {
},
},
},
expect: `curl -X GET 'http://foo?page=1&size=10'`,
expect: `curl -k -X GET 'http://foo?page=1&size=10'`,
}, {
name: "basic HTTP POST",
testCase: atest.TestCase{
Expand All @@ -56,7 +56,7 @@ func TestCurlGenerator(t *testing.T) {
Method: http.MethodPost,
},
},
expect: `curl -X POST 'http://foo'`,
expect: `curl -k -X POST 'http://foo'`,
}, {
name: "has header",
testCase: atest.TestCase{
Expand All @@ -68,7 +68,7 @@ func TestCurlGenerator(t *testing.T) {
},
},
},
expect: `curl -X GET 'http://foo' \
expect: `curl -k -X GET 'http://foo' \
-H 'Connection: keep-alive' \
-H 'Content-Type: text/plain'`,
}, {
Expand All @@ -79,8 +79,20 @@ func TestCurlGenerator(t *testing.T) {
Body: atest.NewRequestBody("hello"),
},
},
expect: `curl -X GET 'http://foo' \
expect: `curl -k -X GET 'http://foo' \
--data-raw 'hello'`,
}, {
name: "include golang template",
testCase: atest.TestCase{
Request: atest.Request{
API: fooForTest,
Header: map[string]string{
util.ContentType: `{{ base64 "test"}}`,
},
},
},
expect: `curl -k -X GET 'http://foo' \
-H 'Content-Type: dGVzdA=='`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/data/curl.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
curl -X {{.Request.Method}} '{{.Request.API}}'
curl -k -X {{.Request.Method}} '{{.Request.API}}' \
{{- range $key, $val := .Request.Header}}
-H '{{$key}}: {{$val}}'
-H '{{$key}}: {{$val}}' \
{{- end}}
{{- if .Request.Body.String }}
--data-raw '{{.Request.Body.String}}'
Expand Down
6 changes: 3 additions & 3 deletions pkg/generator/data/main.python.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2024 API Testing Authors.
Copyright 2024-2025 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -23,7 +23,7 @@ def main():
{{- end}}
body = io.BytesIO(encoded_data.encode("utf-8"))
{{- else}}
body = io.BytesIO(b"{{.Request.Body.String}}")
body = io.BytesIO(b"""{{.Request.Body.String}}""")
{{- end}}
{{- if gt (len .Request.Header) 0 }}
{{- range $key, $val := .Request.Header}}
Expand All @@ -49,7 +49,7 @@ def main():
raise e
{{- end}}

resp = requests.Session().send(req.prepare())
resp = requests.Session().send(req.prepare(), verify=False)
if resp.status_code != 200:
raise Exception("status code is not 200")

Expand Down
6 changes: 3 additions & 3 deletions pkg/generator/testdata/expected_python_code.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2024 API Testing Authors.
Copyright 2024-2025 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -15,14 +15,14 @@ import requests
from urllib.parse import urlencode

def main():
body = io.BytesIO(b"")
body = io.BytesIO(b"""""")
headers = {"User-Agent": "atest"}
try:
req = requests.Request("GET", "https://www.baidu.com", headers=headers, data=body)
except requests.RequestException as e:
raise e

resp = requests.Session().send(req.prepare())
resp = requests.Session().send(req.prepare(), verify=False)
if resp.status_code != 200:
raise Exception("status code is not 200")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2024 API Testing Authors.
Copyright 2024-2025 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -26,7 +26,7 @@ def main():
except requests.RequestException as e:
raise e

resp = requests.Session().send(req.prepare())
resp = requests.Session().send(req.prepare(), verify=False)
if resp.status_code != 200:
raise Exception("status code is not 200")

Expand Down
4 changes: 2 additions & 2 deletions pkg/generator/testdata/expected_python_form_request_code.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'''
Copyright 2024 API Testing Authors.
Copyright 2024-2025 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -25,7 +25,7 @@ def main():
except requests.RequestException as e:
raise e

resp = requests.Session().send(req.prepare())
resp = requests.Session().send(req.prepare(), verify=False)
if resp.status_code != 200:
raise Exception("status code is not 200")

Expand Down
Loading