Skip to content

Commit

Permalink
fix: python generated code error when without HTTP headers (LinuxSuRe…
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarCompiler authored Jun 1, 2024
1 parent c0d9a8a commit 1735b8f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
15 changes: 10 additions & 5 deletions e2e/code-generator/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ mkdir -p /var/data
nohup atest server --local-storage '/workspace/test-suites/*.yaml'&
sleep 1

curl http://localhost:8080/server.Runner/GenerateCode -X POST \
-d '{"TestSuite": "test", "TestCase": "requestWithHeader", "Generator": "'"$lang"'"}' > code.json
test_cases=("requestWithHeader" "requestWithoutHeader")

cat code.json | jq .message -r | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/\\\"/"/g' > code.txt
cat code.txt
for test_case in "${test_cases[@]}"
do
curl http://localhost:8080/server.Runner/GenerateCode -X POST \
-d '{"TestSuite": "test", "TestCase": "'"${test_case}"'", "Generator": "'"$lang"'"}' > code.json

sh /workspace/${lang}.sh code.txt
cat code.json | jq .message -r | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/\\\"/"/g' > code.txt
cat code.txt

sh /workspace/${lang}.sh code.txt
done

exit 0
16 changes: 10 additions & 6 deletions e2e/code-generator/test-suites/test-suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ param:
suiteName: test
caseName: test
items:
- name: requestWithHeader
request:
api: /GetSuites
method: POST
header:
auth: fake
- name: requestWithHeader
request:
api: /GetSuites
method: POST
header:
auth: fake
- name: requestWithoutHeader
request:
api: /GetSuites
method: POST
4 changes: 4 additions & 0 deletions pkg/generator/data/main.python.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ def main():
{{- else}}
body = io.BytesIO(b"{{.Request.Body.String}}")
{{- end}}
{{- if gt (len .Request.Header) 0 }}
{{- range $key, $val := .Request.Header}}
headers = {"{{$key}}": "{{$val}}"}
{{- end}}
{{- else }}
headers = {}
{{- end }}
{{- if gt (len .Request.Cookie) 0 }}
{{- range $key, $val := .Request.Cookie}}
cookies = {"{{$key}}": "{{$val}}"}
Expand Down
3 changes: 1 addition & 2 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *TestResult, er
task.Env = withDefaultValue(task.Env, map[string]string{}).(map[string]string)

var suite *testing.TestSuite

// TODO may not safe in multiple threads
oldEnv := map[string]string{}
for key, val := range task.Env {
Expand Down Expand Up @@ -632,7 +631,6 @@ func (s *server) ListCodeGenerator(ctx context.Context, in *Empty) (reply *Simpl

func (s *server) GenerateCode(ctx context.Context, in *CodeGenerateRequest) (reply *CommonResult, err error) {
reply = &CommonResult{}

instance := generator.GetCodeGenerator(in.Generator)
if instance == nil {
reply.Success = false
Expand All @@ -652,6 +650,7 @@ func (s *server) GenerateCode(ctx context.Context, in *CodeGenerateRequest) (rep
}

if result, err = loader.GetTestCase(in.TestSuite, in.TestCase); err == nil {

result.Request.RenderAPI(suite.API)

output, genErr := instance.Generate(&suite, &result)
Expand Down

0 comments on commit 1735b8f

Please sign in to comment.