Skip to content

Commit 08ecc78

Browse files
committed
fix: the shell, python generator is incorrect
1 parent c827476 commit 08ecc78

File tree

9 files changed

+69
-48
lines changed

9 files changed

+69
-48
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

pkg/generator/code_generator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ func generate(testsuite *testing.TestSuite, testcase *testing.TestCase, template
8383
}
8484
}
8585
}
86+
if testcase != nil {
87+
if err = testcase.Request.Render(nil, ""); err != nil {
88+
return
89+
}
90+
}
8691
var tpl *template.Template
8792
if tpl, err = template.New(templateName).
8893
Funcs(template.FuncMap{"safeString": safeString}).

pkg/generator/curl_generator.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2025 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,53 +16,56 @@ limitations under the License.
1616
package generator
1717

1818
import (
19-
"bytes"
20-
_ "embed"
21-
"net/http"
22-
"strings"
23-
"text/template"
19+
"bytes"
20+
_ "embed"
21+
"net/http"
22+
"strings"
23+
"text/template"
2424

25-
"github.com/linuxsuren/api-testing/pkg/testing"
25+
"github.com/linuxsuren/api-testing/pkg/testing"
2626
)
2727

2828
type curlGenerator struct {
2929
}
3030

3131
func NewCurlGenerator() CodeGenerator {
32-
return &curlGenerator{}
32+
return &curlGenerator{}
3333
}
3434

3535
func (g *curlGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
36-
if testcase.Request.Method == "" {
37-
testcase.Request.Method = http.MethodGet
38-
}
36+
if testcase.Request.Method == "" {
37+
testcase.Request.Method = http.MethodGet
38+
}
3939

40-
if !strings.HasSuffix(testcase.Request.API, "?") {
41-
testcase.Request.API += "?"
42-
}
40+
if !strings.HasSuffix(testcase.Request.API, "?") {
41+
testcase.Request.API += "?"
42+
}
4343

44-
queryKeys := testcase.Request.Query.Keys()
45-
for _, k := range queryKeys {
46-
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
47-
}
44+
queryKeys := testcase.Request.Query.Keys()
45+
for _, k := range queryKeys {
46+
testcase.Request.API += k + "=" + testcase.Request.Query.GetValue(k) + "&"
47+
}
4848

49-
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
50-
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
49+
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "&")
50+
testcase.Request.API = strings.TrimSuffix(testcase.Request.API, "?")
51+
if err = testcase.Request.Render(nil, ""); err != nil {
52+
return
53+
}
5154

52-
var tpl *template.Template
53-
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
54-
buf := new(bytes.Buffer)
55-
if err = tpl.Execute(buf, testcase); err == nil {
56-
result = strings.TrimSpace(buf.String())
55+
var tpl *template.Template
56+
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
57+
buf := new(bytes.Buffer)
58+
if err = tpl.Execute(buf, testcase); err == nil {
59+
result = strings.TrimSpace(buf.String())
5760

58-
result = strings.ReplaceAll(result, "\n", " \\\n")
59-
}
60-
}
61-
return
61+
result = strings.TrimSuffix(result, " \\")
62+
}
63+
}
64+
return
6265
}
6366

6467
func init() {
65-
RegisterCodeGenerator("curl", NewCurlGenerator())
68+
RegisterCodeGenerator("curl", NewCurlGenerator())
6669
}
6770

6871
//go:embed data/curl.tpl

pkg/generator/curl_generator_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2025 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ func TestCurlGenerator(t *testing.T) {
3535
API: fooForTest,
3636
},
3737
},
38-
expect: `curl -X GET 'http://foo'`,
38+
expect: `curl -k -X GET 'http://foo'`,
3939
}, {
4040
name: "has query string",
4141
testCase: atest.TestCase{
@@ -47,7 +47,7 @@ func TestCurlGenerator(t *testing.T) {
4747
},
4848
},
4949
},
50-
expect: `curl -X GET 'http://foo?page=1&size=10'`,
50+
expect: `curl -k -X GET 'http://foo?page=1&size=10'`,
5151
}, {
5252
name: "basic HTTP POST",
5353
testCase: atest.TestCase{
@@ -56,7 +56,7 @@ func TestCurlGenerator(t *testing.T) {
5656
Method: http.MethodPost,
5757
},
5858
},
59-
expect: `curl -X POST 'http://foo'`,
59+
expect: `curl -k -X POST 'http://foo'`,
6060
}, {
6161
name: "has header",
6262
testCase: atest.TestCase{
@@ -68,7 +68,7 @@ func TestCurlGenerator(t *testing.T) {
6868
},
6969
},
7070
},
71-
expect: `curl -X GET 'http://foo' \
71+
expect: `curl -k -X GET 'http://foo' \
7272
-H 'Connection: keep-alive' \
7373
-H 'Content-Type: text/plain'`,
7474
}, {
@@ -79,8 +79,20 @@ func TestCurlGenerator(t *testing.T) {
7979
Body: atest.NewRequestBody("hello"),
8080
},
8181
},
82-
expect: `curl -X GET 'http://foo' \
82+
expect: `curl -k -X GET 'http://foo' \
8383
--data-raw 'hello'`,
84+
}, {
85+
name: "include golang template",
86+
testCase: atest.TestCase{
87+
Request: atest.Request{
88+
API: fooForTest,
89+
Header: map[string]string{
90+
util.ContentType: `{{ base64 "test"}}`,
91+
},
92+
},
93+
},
94+
expect: `curl -k -X GET 'http://foo' \
95+
-H 'Content-Type: dGVzdA=='`,
8496
}}
8597
for _, tt := range tests {
8698
t.Run(tt.name, func(t *testing.T) {

pkg/generator/data/curl.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
curl -X {{.Request.Method}} '{{.Request.API}}'
1+
curl -k -X {{.Request.Method}} '{{.Request.API}}' \
22
{{- range $key, $val := .Request.Header}}
3-
-H '{{$key}}: {{$val}}'
3+
-H '{{$key}}: {{$val}}' \
44
{{- end}}
55
{{- if .Request.Body.String }}
66
--data-raw '{{.Request.Body.String}}'

pkg/generator/data/main.python.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -23,7 +23,7 @@ def main():
2323
{{- end}}
2424
body = io.BytesIO(encoded_data.encode("utf-8"))
2525
{{- else}}
26-
body = io.BytesIO(b"{{.Request.Body.String}}")
26+
body = io.BytesIO(b"""{{.Request.Body.String}}""")
2727
{{- end}}
2828
{{- if gt (len .Request.Header) 0 }}
2929
{{- range $key, $val := .Request.Header}}
@@ -49,7 +49,7 @@ def main():
4949
raise e
5050
{{- end}}
5151

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

pkg/generator/testdata/expected_python_code.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -15,14 +15,14 @@ import requests
1515
from urllib.parse import urlencode
1616

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

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

pkg/generator/testdata/expected_python_cookie_request_code.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -26,7 +26,7 @@ def main():
2626
except requests.RequestException as e:
2727
raise e
2828

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

pkg/generator/testdata/expected_python_form_request_code.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Copyright 2024 API Testing Authors.
2+
Copyright 2024-2025 API Testing Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -25,7 +25,7 @@ def main():
2525
except requests.RequestException as e:
2626
raise e
2727

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

0 commit comments

Comments
 (0)