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:
parent
768b79f0c3
commit
664451e6ee
|
@ -0,0 +1 @@
|
||||||
|
* text eol=lf
|
2411
go.work.sum
2411
go.work.sum
File diff suppressed because it is too large
Load Diff
|
@ -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
|
var tpl *template.Template
|
||||||
if tpl, err = template.New(templateName).
|
if tpl, err = template.New(templateName).
|
||||||
Funcs(template.FuncMap{"safeString": safeString}).
|
Funcs(template.FuncMap{"safeString": safeString}).
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -48,6 +48,9 @@ func (g *curlGenerator) Generate(testSuite *testing.TestSuite, testcase *testing
|
||||||
|
|
||||||
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
|
var tpl *template.Template
|
||||||
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
|
if tpl, err = template.New("curl template").Parse(curlTemplate); err == nil {
|
||||||
|
@ -55,7 +58,7 @@ func (g *curlGenerator) Generate(testSuite *testing.TestSuite, testcase *testing
|
||||||
if err = tpl.Execute(buf, testcase); err == nil {
|
if err = tpl.Execute(buf, testcase); err == nil {
|
||||||
result = strings.TrimSpace(buf.String())
|
result = strings.TrimSpace(buf.String())
|
||||||
|
|
||||||
result = strings.ReplaceAll(result, "\n", " \\\n")
|
result = strings.TrimSuffix(result, " \\")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with 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,
|
API: fooForTest,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expect: `curl -X GET 'http://foo'`,
|
expect: `curl -k -X GET 'http://foo'`,
|
||||||
}, {
|
}, {
|
||||||
name: "has query string",
|
name: "has query string",
|
||||||
testCase: atest.TestCase{
|
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",
|
name: "basic HTTP POST",
|
||||||
testCase: atest.TestCase{
|
testCase: atest.TestCase{
|
||||||
|
@ -56,7 +56,7 @@ func TestCurlGenerator(t *testing.T) {
|
||||||
Method: http.MethodPost,
|
Method: http.MethodPost,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expect: `curl -X POST 'http://foo'`,
|
expect: `curl -k -X POST 'http://foo'`,
|
||||||
}, {
|
}, {
|
||||||
name: "has header",
|
name: "has header",
|
||||||
testCase: atest.TestCase{
|
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 'Connection: keep-alive' \
|
||||||
-H 'Content-Type: text/plain'`,
|
-H 'Content-Type: text/plain'`,
|
||||||
}, {
|
}, {
|
||||||
|
@ -79,8 +79,20 @@ func TestCurlGenerator(t *testing.T) {
|
||||||
Body: atest.NewRequestBody("hello"),
|
Body: atest.NewRequestBody("hello"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expect: `curl -X GET 'http://foo' \
|
expect: `curl -k -X GET 'http://foo' \
|
||||||
--data-raw 'hello'`,
|
--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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
curl -X {{.Request.Method}} '{{.Request.API}}'
|
curl -k -X {{.Request.Method}} '{{.Request.API}}' \
|
||||||
{{- range $key, $val := .Request.Header}}
|
{{- range $key, $val := .Request.Header}}
|
||||||
-H '{{$key}}: {{$val}}'
|
-H '{{$key}}: {{$val}}' \
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- if .Request.Body.String }}
|
{{- if .Request.Body.String }}
|
||||||
--data-raw '{{.Request.Body.String}}'
|
--data-raw '{{.Request.Body.String}}'
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
You may obtain a copy of the License at
|
You may obtain a copy of the License at
|
||||||
|
@ -23,7 +23,7 @@ def main():
|
||||||
{{- end}}
|
{{- end}}
|
||||||
body = io.BytesIO(encoded_data.encode("utf-8"))
|
body = io.BytesIO(encoded_data.encode("utf-8"))
|
||||||
{{- else}}
|
{{- else}}
|
||||||
body = io.BytesIO(b"{{.Request.Body.String}}")
|
body = io.BytesIO(b"""{{.Request.Body.String}}""")
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- if gt (len .Request.Header) 0 }}
|
{{- if gt (len .Request.Header) 0 }}
|
||||||
{{- range $key, $val := .Request.Header}}
|
{{- range $key, $val := .Request.Header}}
|
||||||
|
@ -49,7 +49,7 @@ def main():
|
||||||
raise e
|
raise e
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
|
||||||
resp = requests.Session().send(req.prepare())
|
resp = requests.Session().send(req.prepare(), verify=False)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise Exception("status code is not 200")
|
raise Exception("status code is not 200")
|
||||||
|
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
You may obtain a copy of the License at
|
You may obtain a copy of the License at
|
||||||
|
@ -15,14 +15,14 @@ import requests
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
body = io.BytesIO(b"")
|
body = io.BytesIO(b"""""")
|
||||||
headers = {"User-Agent": "atest"}
|
headers = {"User-Agent": "atest"}
|
||||||
try:
|
try:
|
||||||
req = requests.Request("GET", "https://www.baidu.com", headers=headers, data=body)
|
req = requests.Request("GET", "https://www.baidu.com", headers=headers, data=body)
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
resp = requests.Session().send(req.prepare())
|
resp = requests.Session().send(req.prepare(), verify=False)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise Exception("status code is not 200")
|
raise Exception("status code is not 200")
|
||||||
|
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
You may obtain a copy of the License at
|
You may obtain a copy of the License at
|
||||||
|
@ -26,7 +26,7 @@ def main():
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
resp = requests.Session().send(req.prepare())
|
resp = requests.Session().send(req.prepare(), verify=False)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise Exception("status code is not 200")
|
raise Exception("status code is not 200")
|
||||||
|
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
You may obtain a copy of the License at
|
You may obtain a copy of the License at
|
||||||
|
@ -25,7 +25,7 @@ def main():
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
resp = requests.Session().send(req.prepare())
|
resp = requests.Session().send(req.prepare(), verify=False)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise Exception("status code is not 200")
|
raise Exception("status code is not 200")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue