fix: the shell, python generator is incorrect (#601)

* fix: the shell, python generator is incorrect

* update go.sum

* update the end-of-line

* remove go.work.sum

---------

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2025-01-26 10:54:25 +08:00 committed by GitHub
parent 768b79f0c3
commit 664451e6ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 210 additions and 2600 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text eol=lf

File diff suppressed because it is too large Load Diff

View File

@ -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}).

View File

@ -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.
@ -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

View File

@ -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.
@ -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{
@ -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{
@ -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{
@ -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'`,
}, {
@ -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) {

View File

@ -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}}'

View File

@ -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
@ -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}}
@ -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")

View File

@ -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
@ -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")

View File

@ -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
@ -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")

View File

@ -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
@ -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")