From dfb5b4122a2e4980be6ea01f68643fb8bddea600 Mon Sep 17 00:00:00 2001 From: zhouzhou1017 Date: Fri, 19 Apr 2024 12:24:47 -0400 Subject: [PATCH 1/7] update code_gen_test --- pkg/generator/code_generator_test.go | 28 +++++++++- pkg/generator/data/main.python.tpl | 44 +++++++++++++++ pkg/generator/python_generator.go | 54 +++++++++++++++++++ .../testdata/expected_python_code.txt | 21 ++++++++ .../expected_python_cookie_request_code.txt | 25 +++++++++ .../expected_python_form_request_code.txt | 24 +++++++++ 6 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 pkg/generator/data/main.python.tpl create mode 100644 pkg/generator/python_generator.go create mode 100644 pkg/generator/testdata/expected_python_code.txt create mode 100644 pkg/generator/testdata/expected_python_cookie_request_code.txt create mode 100644 pkg/generator/testdata/expected_python_form_request_code.txt diff --git a/pkg/generator/code_generator_test.go b/pkg/generator/code_generator_test.go index e06653ac..ba1ecb11 100644 --- a/pkg/generator/code_generator_test.go +++ b/pkg/generator/code_generator_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 API Testing Authors. +Copyright 2024 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. @@ -63,6 +63,12 @@ func TestGenerators(t *testing.T) { assert.Equal(t, expectedJavaCode, result) }) + t.Run("python", func(t *testing.T) { + result, err := generator.GetCodeGenerator("python").Generate(nil, testcase) + assert.NoError(t, err) + assert.Equal(t, expectedPythonCode, result) + }) + formRequest := &atest.TestCase{Request: testcase.Request} formRequest.Request.Form = map[string]string{ "key": "value", @@ -79,6 +85,12 @@ func TestGenerators(t *testing.T) { assert.Equal(t, expectedFormRequestJavaCode, result, result) }) + t.Run("python form HTTP request", func(t *testing.T) { + result, err := generator.GetCodeGenerator("python").Generate(nil, formRequest) + assert.NoError(t, err) + assert.Equal(t, expectedFormRequestPythonCode, result, result) + }) + cookieRequest := &atest.TestCase{Request: formRequest.Request} cookieRequest.Request.Cookie = map[string]string{ "name": "value", @@ -102,6 +114,11 @@ func TestGenerators(t *testing.T) { result, err := generator.GetCodeGenerator("golang").Generate(nil, bodyRequest) assert.NoError(t, err) assert.Equal(t, expectedBodyRequestGoCode, result, result) + + t.Run("python cookie HTTP request", func(t *testing.T) { + result, err := generator.GetCodeGenerator("python").Generate(nil, cookieRequest) + assert.NoError(t, err) + assert.Equal(t, expectedCookieRequestPythonCode, result, result) }) } @@ -125,3 +142,12 @@ var expectedCookieRequestJavaCode string //go:embed testdata/expected_go_body_request_code.txt var expectedBodyRequestGoCode string + +//go:embed testdata/expected_python_code.txt +var expectedPythonCode string + +//go:embed testdata/expected_python_form_request_code.txt +var expectedFormRequestPythonCode string + +//go:embed testdata/expected_python_cookie_request_code.txt +var expectedCookieRequestPythonCode string diff --git a/pkg/generator/data/main.python.tpl b/pkg/generator/data/main.python.tpl new file mode 100644 index 00000000..6deec2c4 --- /dev/null +++ b/pkg/generator/data/main.python.tpl @@ -0,0 +1,44 @@ +import io +import requests +from urllib.parse import urlencode + +def main(): + {{- if gt (len .Request.Form) 0 }} + data = {} + {{- range $key, $val := .Request.Form}} + data["{{$key}}"] = "{{$val}}" + encoded_data = urlencode(data) + {{- end}} + body = io.BytesIO(encoded_data.encode("utf-8")) + {{- else}} + body = io.BytesIO(b"{{.Request.Body.String}}") + {{- end}} + {{- range $key, $val := .Request.Header}} + headers = {"{{$key}}": "{{$val}}"} + {{- end}} + {{- if gt (len .Request.Cookie) 0 }} + {{- range $key, $val := .Request.Cookie}} + cookies = {"{{$key}}": "{{$val}}"} + {{- end}} + {{- end}} + {{- if gt (len .Request.Cookie) 0 }} + try: + req = requests.Request("{{.Request.Method}}", "{{.Request.API}}", headers=headers, cookies=cookies, data=body) + except requests.RequestException as e: + raise e + {{- else}} + try: + req = requests.Request("{{.Request.Method}}", "{{.Request.API}}", headers=headers, data=body) + except requests.RequestException as e: + raise e + {{- end}} + + resp = requests.Session().send(req.prepare()) + if resp.status_code != 200: + raise Exception("status code is not 200") + + data = resp.content + print(data.decode("utf-8")) + +if __name__ == "__main__": + main() diff --git a/pkg/generator/python_generator.go b/pkg/generator/python_generator.go new file mode 100644 index 00000000..71ae2a11 --- /dev/null +++ b/pkg/generator/python_generator.go @@ -0,0 +1,54 @@ +/* +Copyright 2023 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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package generator + +import ( + "bytes" + "html/template" + "net/http" + + _ "embed" + + "github.com/linuxsuren/api-testing/pkg/testing" +) + +type pythonGenerator struct { +} + +func NewPythonGenerator() CodeGenerator { + return &pythonGenerator{} +} + +func (g *pythonGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) { + if testcase.Request.Method == "" { + testcase.Request.Method = http.MethodGet + } + var tpl *template.Template + if tpl, err = template.New("python template").Parse(pythonTemplate); err == nil { + buf := new(bytes.Buffer) + if err = tpl.Execute(buf, testcase); err == nil { + result = buf.String() + } + } + return +} + +func init() { + RegisterCodeGenerator("python", NewPythonGenerator()) +} + +//go:embed data/main.python.tpl +var pythonTemplate string diff --git a/pkg/generator/testdata/expected_python_code.txt b/pkg/generator/testdata/expected_python_code.txt new file mode 100644 index 00000000..399c21d0 --- /dev/null +++ b/pkg/generator/testdata/expected_python_code.txt @@ -0,0 +1,21 @@ +import io +import requests +from urllib.parse import urlencode + +def main(): + 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()) + if resp.status_code != 200: + raise Exception("status code is not 200") + + data = resp.content + print(data.decode("utf-8")) + +if __name__ == "__main__": + main() diff --git a/pkg/generator/testdata/expected_python_cookie_request_code.txt b/pkg/generator/testdata/expected_python_cookie_request_code.txt new file mode 100644 index 00000000..5139b134 --- /dev/null +++ b/pkg/generator/testdata/expected_python_cookie_request_code.txt @@ -0,0 +1,25 @@ +import io +import requests +from urllib.parse import urlencode + +def main(): + data = {} + data["key"] = "value" + encoded_data = urlencode(data) + body = io.BytesIO(encoded_data.encode("utf-8")) + headers = {"User-Agent": "atest"} + cookies = {"name": "value"} + try: + req = requests.Request("GET", "https://www.baidu.com", headers=headers, cookies=cookies, data=body) + except requests.RequestException as e: + raise e + + resp = requests.Session().send(req.prepare()) + if resp.status_code != 200: + raise Exception("status code is not 200") + + data = resp.content + print(data.decode("utf-8")) + +if __name__ == "__main__": + main() diff --git a/pkg/generator/testdata/expected_python_form_request_code.txt b/pkg/generator/testdata/expected_python_form_request_code.txt new file mode 100644 index 00000000..abcb15a9 --- /dev/null +++ b/pkg/generator/testdata/expected_python_form_request_code.txt @@ -0,0 +1,24 @@ +import io +import requests +from urllib.parse import urlencode + +def main(): + data = {} + data["key"] = "value" + encoded_data = urlencode(data) + body = io.BytesIO(encoded_data.encode("utf-8")) + 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()) + if resp.status_code != 200: + raise Exception("status code is not 200") + + data = resp.content + print(data.decode("utf-8")) + +if __name__ == "__main__": + main() From fe87616bdc0cadd1387f44512f61edb0b67cd520 Mon Sep 17 00:00:00 2001 From: zhouzhou1017 Date: Fri, 19 Apr 2024 12:54:13 -0400 Subject: [PATCH 2/7] update 2 --- pkg/server/remote_server_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/server/remote_server_test.go b/pkg/server/remote_server_test.go index 04242b71..6dbe5cdf 100644 --- a/pkg/server/remote_server_test.go +++ b/pkg/server/remote_server_test.go @@ -593,7 +593,7 @@ func TestCodeGenerator(t *testing.T) { t.Run("ListCodeGenerator", func(t *testing.T) { generators, err := server.ListCodeGenerator(ctx, &Empty{}) assert.NoError(t, err) - assert.Equal(t, 4, len(generators.Data)) + assert.Equal(t, 5, len(generators.Data)) }) t.Run("GenerateCode, no generator found", func(t *testing.T) { From f3d8f9c1d502c4f9d5067c8c45d0e41c594bd9ef Mon Sep 17 00:00:00 2001 From: zyx Date: Tue, 23 Apr 2024 17:03:22 +0800 Subject: [PATCH 3/7] update --- pkg/generator/python_generator.go | 2 +- pkg/server/remote_server_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/generator/python_generator.go b/pkg/generator/python_generator.go index 71ae2a11..9ed3180e 100644 --- a/pkg/generator/python_generator.go +++ b/pkg/generator/python_generator.go @@ -1,5 +1,5 @@ /* -Copyright 2023 API Testing Authors. +Copyright 2024 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. diff --git a/pkg/server/remote_server_test.go b/pkg/server/remote_server_test.go index 6dbe5cdf..c329dd27 100644 --- a/pkg/server/remote_server_test.go +++ b/pkg/server/remote_server_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 API Testing Authors. +Copyright 2024 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. From 0ae1d1266e99eff09804ea9a4e6924da53e69c03 Mon Sep 17 00:00:00 2001 From: zyx Date: Fri, 19 Apr 2024 18:00:39 +0800 Subject: [PATCH 4/7] update_pygen --- .github/pre-commit | 3 --- console/atest-ui/gen.sh | 0 e2e/entrypoint.sh | 0 e2e/start.sh | 0 4 files changed, 3 deletions(-) mode change 100755 => 100644 .github/pre-commit mode change 100755 => 100644 console/atest-ui/gen.sh mode change 100755 => 100644 e2e/entrypoint.sh mode change 100755 => 100644 e2e/start.sh diff --git a/.github/pre-commit b/.github/pre-commit old mode 100755 new mode 100644 index 19ae29f1..e69de29b --- a/.github/pre-commit +++ b/.github/pre-commit @@ -1,3 +0,0 @@ -#!/bin/sh - -make fmt test-all diff --git a/console/atest-ui/gen.sh b/console/atest-ui/gen.sh old mode 100755 new mode 100644 diff --git a/e2e/entrypoint.sh b/e2e/entrypoint.sh old mode 100755 new mode 100644 diff --git a/e2e/start.sh b/e2e/start.sh old mode 100755 new mode 100644 From da24082b9bd9f26aa979be02db4db97e6ef0477b Mon Sep 17 00:00:00 2001 From: zyx Date: Tue, 23 Apr 2024 18:44:22 +0800 Subject: [PATCH 5/7] solve commit conflicts --- pkg/generator/code_generator_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/generator/code_generator_test.go b/pkg/generator/code_generator_test.go index ba1ecb11..0d0cf674 100644 --- a/pkg/generator/code_generator_test.go +++ b/pkg/generator/code_generator_test.go @@ -107,18 +107,18 @@ func TestGenerators(t *testing.T) { assert.Equal(t, expectedCookieRequestJavaCode, result, result) }) + t.Run("python cookie HTTP request", func(t *testing.T) { + result, err := generator.GetCodeGenerator("python").Generate(nil, cookieRequest) + assert.NoError(t, err) + assert.Equal(t, expectedCookieRequestPythonCode, result, result) + bodyRequest := &atest.TestCase{Request: testcase.Request} bodyRequest.Request.Body.Value = `{"key": "value"}` - + t.Run("golang body HTTP request", func(t *testing.T) { result, err := generator.GetCodeGenerator("golang").Generate(nil, bodyRequest) assert.NoError(t, err) assert.Equal(t, expectedBodyRequestGoCode, result, result) - - t.Run("python cookie HTTP request", func(t *testing.T) { - result, err := generator.GetCodeGenerator("python").Generate(nil, cookieRequest) - assert.NoError(t, err) - assert.Equal(t, expectedCookieRequestPythonCode, result, result) }) } @@ -151,3 +151,6 @@ var expectedFormRequestPythonCode string //go:embed testdata/expected_python_cookie_request_code.txt var expectedCookieRequestPythonCode string + +//go:embed testdata/expected_go_body_request_code.txt +var expectedBodyRequestGoCode string From 9100e47517ea6edbc02e414d6cb0c8bfe2010ebe Mon Sep 17 00:00:00 2001 From: zhouzhou1017 Date: Tue, 23 Apr 2024 11:00:31 -0400 Subject: [PATCH 6/7] add license --- pkg/generator/code_generator_test.go | 5 +++-- pkg/generator/data/main.python.tpl | 12 ++++++++++++ pkg/generator/testdata/expected_python_code.txt | 13 +++++++++++++ .../expected_python_cookie_request_code.txt | 12 ++++++++++++ .../testdata/expected_python_form_request_code.txt | 12 ++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/pkg/generator/code_generator_test.go b/pkg/generator/code_generator_test.go index 0d0cf674..97fa5f7e 100644 --- a/pkg/generator/code_generator_test.go +++ b/pkg/generator/code_generator_test.go @@ -111,10 +111,11 @@ func TestGenerators(t *testing.T) { result, err := generator.GetCodeGenerator("python").Generate(nil, cookieRequest) assert.NoError(t, err) assert.Equal(t, expectedCookieRequestPythonCode, result, result) - + }) + bodyRequest := &atest.TestCase{Request: testcase.Request} bodyRequest.Request.Body.Value = `{"key": "value"}` - + t.Run("golang body HTTP request", func(t *testing.T) { result, err := generator.GetCodeGenerator("golang").Generate(nil, bodyRequest) assert.NoError(t, err) diff --git a/pkg/generator/data/main.python.tpl b/pkg/generator/data/main.python.tpl index 6deec2c4..1259f42e 100644 --- a/pkg/generator/data/main.python.tpl +++ b/pkg/generator/data/main.python.tpl @@ -1,3 +1,15 @@ +/* +Copyright 2024 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 + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ import io import requests from urllib.parse import urlencode diff --git a/pkg/generator/testdata/expected_python_code.txt b/pkg/generator/testdata/expected_python_code.txt index 399c21d0..7417f897 100644 --- a/pkg/generator/testdata/expected_python_code.txt +++ b/pkg/generator/testdata/expected_python_code.txt @@ -1,3 +1,16 @@ + +/* +Copyright 2024 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 + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ import io import requests from urllib.parse import urlencode diff --git a/pkg/generator/testdata/expected_python_cookie_request_code.txt b/pkg/generator/testdata/expected_python_cookie_request_code.txt index 5139b134..d05f3f5d 100644 --- a/pkg/generator/testdata/expected_python_cookie_request_code.txt +++ b/pkg/generator/testdata/expected_python_cookie_request_code.txt @@ -1,3 +1,15 @@ +/* +Copyright 2024 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 + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ import io import requests from urllib.parse import urlencode diff --git a/pkg/generator/testdata/expected_python_form_request_code.txt b/pkg/generator/testdata/expected_python_form_request_code.txt index abcb15a9..2180c44a 100644 --- a/pkg/generator/testdata/expected_python_form_request_code.txt +++ b/pkg/generator/testdata/expected_python_form_request_code.txt @@ -1,3 +1,15 @@ +/* +Copyright 2024 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 + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ import io import requests from urllib.parse import urlencode From 8ca12b1efa1bac45ad9c46238848b56807600c98 Mon Sep 17 00:00:00 2001 From: zhouzhou1017 Date: Tue, 23 Apr 2024 11:10:12 -0400 Subject: [PATCH 7/7] fix something --- pkg/generator/code_generator_test.go | 3 --- pkg/generator/testdata/expected_python_code.txt | 1 - 2 files changed, 4 deletions(-) diff --git a/pkg/generator/code_generator_test.go b/pkg/generator/code_generator_test.go index 97fa5f7e..ab0bf89b 100644 --- a/pkg/generator/code_generator_test.go +++ b/pkg/generator/code_generator_test.go @@ -152,6 +152,3 @@ var expectedFormRequestPythonCode string //go:embed testdata/expected_python_cookie_request_code.txt var expectedCookieRequestPythonCode string - -//go:embed testdata/expected_go_body_request_code.txt -var expectedBodyRequestGoCode string diff --git a/pkg/generator/testdata/expected_python_code.txt b/pkg/generator/testdata/expected_python_code.txt index 7417f897..0e532874 100644 --- a/pkg/generator/testdata/expected_python_code.txt +++ b/pkg/generator/testdata/expected_python_code.txt @@ -1,4 +1,3 @@ - /* Copyright 2024 API Testing Authors. Licensed under the Apache License, Version 2.0 (the "License");