diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index db8a18f8..9262a3f3 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -8,6 +8,7 @@ - feat: assert python3 package is ready with specified version - refactor: build plugin mechanism, cancel automatic installation of dependencies - fix: pip upgrade httprunner when installing hrp +- fix #1352: avoid conversion to exponential notation **python version** diff --git a/examples/demo-with-go-plugin/proj.json b/examples/demo-with-go-plugin/proj.json index 03850cc4..fac542f7 100644 --- a/examples/demo-with-go-plugin/proj.json +++ b/examples/demo-with-go-plugin/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-with-go-plugin", - "create_time": "2022-06-14T11:34:25.941159+08:00", + "create_time": "2022-06-14T13:19:05.262039+08:00", "hrp_version": "v4.1.3" } diff --git a/examples/demo-with-go-plugin/testcases/requests.json b/examples/demo-with-go-plugin/testcases/requests.json index 75c464f9..4c01ec74 100644 --- a/examples/demo-with-go-plugin/testcases/requests.json +++ b/examples/demo-with-go-plugin/testcases/requests.json @@ -22,7 +22,7 @@ "variables": { "foo1": "${ENV(USERNAME)}", "foo2": "bar21", - "sum_v": "${sum_two_int(1, 2)}" + "sum_v": "${sum_two_int(10000000, 20000000)}" }, "request": { "method": "GET", @@ -52,7 +52,7 @@ { "check": "body.args.sum_v", "assert": "equal", - "expect": "3", + "expect": "30000000", "msg": "check body.args.sum_v" }, { diff --git a/examples/demo-with-go-plugin/testcases/requests.yml b/examples/demo-with-go-plugin/testcases/requests.yml index 1db4e4d1..5922ab12 100644 --- a/examples/demo-with-go-plugin/testcases/requests.yml +++ b/examples/demo-with-go-plugin/testcases/requests.yml @@ -16,7 +16,7 @@ teststeps: variables: foo1: ${ENV(USERNAME)} foo2: bar21 - sum_v: "${sum_two_int(1, 2)}" + sum_v: "${sum_two_int(10000000, 20000000)}" request: method: GET url: $base_url/get @@ -29,7 +29,7 @@ teststeps: validate: - eq: ["status_code", 200] - eq: ["body.args.foo1", "debugtalk"] - - eq: ["body.args.sum_v", "3"] + - eq: ["body.args.sum_v", "30000000"] - eq: ["body.args.foo2", "bar21"] - name: post raw text diff --git a/examples/demo-with-py-plugin/proj.json b/examples/demo-with-py-plugin/proj.json index 3cb92bd4..e0ae1816 100644 --- a/examples/demo-with-py-plugin/proj.json +++ b/examples/demo-with-py-plugin/proj.json @@ -1,5 +1,5 @@ { "project_name": "demo-with-py-plugin", - "create_time": "2022-06-14T11:34:26.276891+08:00", + "create_time": "2022-06-14T13:19:05.680344+08:00", "hrp_version": "v4.1.3" } diff --git a/examples/demo-with-py-plugin/testcases/requests.json b/examples/demo-with-py-plugin/testcases/requests.json index 75c464f9..4c01ec74 100644 --- a/examples/demo-with-py-plugin/testcases/requests.json +++ b/examples/demo-with-py-plugin/testcases/requests.json @@ -22,7 +22,7 @@ "variables": { "foo1": "${ENV(USERNAME)}", "foo2": "bar21", - "sum_v": "${sum_two_int(1, 2)}" + "sum_v": "${sum_two_int(10000000, 20000000)}" }, "request": { "method": "GET", @@ -52,7 +52,7 @@ { "check": "body.args.sum_v", "assert": "equal", - "expect": "3", + "expect": "30000000", "msg": "check body.args.sum_v" }, { diff --git a/examples/demo-with-py-plugin/testcases/requests.yml b/examples/demo-with-py-plugin/testcases/requests.yml index 1db4e4d1..5922ab12 100644 --- a/examples/demo-with-py-plugin/testcases/requests.yml +++ b/examples/demo-with-py-plugin/testcases/requests.yml @@ -16,7 +16,7 @@ teststeps: variables: foo1: ${ENV(USERNAME)} foo2: bar21 - sum_v: "${sum_two_int(1, 2)}" + sum_v: "${sum_two_int(10000000, 20000000)}" request: method: GET url: $base_url/get @@ -29,7 +29,7 @@ teststeps: validate: - eq: ["status_code", 200] - eq: ["body.args.foo1", "debugtalk"] - - eq: ["body.args.sum_v", "3"] + - eq: ["body.args.sum_v", "30000000"] - eq: ["body.args.foo2", "bar21"] - name: post raw text diff --git a/hrp/internal/scaffold/templates/testcases/demo_requests.json b/hrp/internal/scaffold/templates/testcases/demo_requests.json index 75c464f9..4c01ec74 100644 --- a/hrp/internal/scaffold/templates/testcases/demo_requests.json +++ b/hrp/internal/scaffold/templates/testcases/demo_requests.json @@ -22,7 +22,7 @@ "variables": { "foo1": "${ENV(USERNAME)}", "foo2": "bar21", - "sum_v": "${sum_two_int(1, 2)}" + "sum_v": "${sum_two_int(10000000, 20000000)}" }, "request": { "method": "GET", @@ -52,7 +52,7 @@ { "check": "body.args.sum_v", "assert": "equal", - "expect": "3", + "expect": "30000000", "msg": "check body.args.sum_v" }, { diff --git a/hrp/internal/scaffold/templates/testcases/demo_requests.yml b/hrp/internal/scaffold/templates/testcases/demo_requests.yml index 1db4e4d1..5922ab12 100644 --- a/hrp/internal/scaffold/templates/testcases/demo_requests.yml +++ b/hrp/internal/scaffold/templates/testcases/demo_requests.yml @@ -16,7 +16,7 @@ teststeps: variables: foo1: ${ENV(USERNAME)} foo2: bar21 - sum_v: "${sum_two_int(1, 2)}" + sum_v: "${sum_two_int(10000000, 20000000)}" request: method: GET url: $base_url/get @@ -29,7 +29,7 @@ teststeps: validate: - eq: ["status_code", 200] - eq: ["body.args.foo1", "debugtalk"] - - eq: ["body.args.sum_v", "3"] + - eq: ["body.args.sum_v", "30000000"] - eq: ["body.args.foo2", "bar21"] - name: post raw text diff --git a/hrp/internal/scaffold/templates/testcases/demo_requests_test.py b/hrp/internal/scaffold/templates/testcases/demo_requests_test.py index dd65d1f0..30d7f022 100644 --- a/hrp/internal/scaffold/templates/testcases/demo_requests_test.py +++ b/hrp/internal/scaffold/templates/testcases/demo_requests_test.py @@ -26,7 +26,7 @@ class TestCaseDemoRequests(HttpRunner): Step( RunRequest("get with params") .with_variables( - **{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two_int(1, 2)}"} + **{"foo1": "bar11", "foo2": "bar21", "sum_v": "${sum_two_int(10000000, 20000000)}"} ) .get("/get") .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"}) @@ -36,7 +36,7 @@ class TestCaseDemoRequests(HttpRunner): .validate() .assert_equal("status_code", 200) .assert_equal("body.args.foo1", "bar11") - .assert_equal("body.args.sum_v", "3") + .assert_equal("body.args.sum_v", "30000000") .assert_equal("body.args.foo2", "bar21") ), Step( diff --git a/hrp/parser.go b/hrp/parser.go index 84482e3a..99e4359b 100644 --- a/hrp/parser.go +++ b/hrp/parser.go @@ -7,6 +7,7 @@ import ( "path" "reflect" "regexp" + "strconv" "strings" "github.com/httprunner/funplugin" @@ -66,13 +67,15 @@ func (p *Parser) ParseHeaders(rawHeaders map[string]string, variablesMapping map } func convertString(raw interface{}) string { - if value, ok := raw.(string); ok { - return value - } else { - // raw is not string, e.g. int, float, etc. - // convert to string - return fmt.Sprintf("%v", raw) + if str, ok := raw.(string); ok { + return str } + if float, ok := raw.(float64); ok { + // f: avoid conversion to exponential notation + return strconv.FormatFloat(float, 'f', -1, 64) + } + // convert to string + return fmt.Sprintf("%v", raw) } func (p *Parser) Parse(raw interface{}, variablesMapping map[string]interface{}) (interface{}, error) { @@ -203,7 +206,7 @@ func (p *Parser) ParseString(raw string, variablesMapping map[string]interface{} // raw_string contains one or many functions, e.g. "abc${add_one(3)}def" matchStartPosition += len(funcMatched[0]) - parsedString += fmt.Sprintf("%v", result) + parsedString += convertString(result) remainedString = raw[matchStartPosition:] log.Debug(). Str("parsedString", parsedString). @@ -232,7 +235,7 @@ func (p *Parser) ParseString(raw string, variablesMapping map[string]interface{} } matchStartPosition += len(varMatched[0]) - parsedString += fmt.Sprintf("%v", varValue) + parsedString += convertString(varValue) remainedString = raw[matchStartPosition:] log.Debug(). Str("parsedString", parsedString). diff --git a/hrp/parser_test.go b/hrp/parser_test.go index ee0b6fb3..ced53b69 100644 --- a/hrp/parser_test.go +++ b/hrp/parser_test.go @@ -613,6 +613,8 @@ func TestConvertString(t *testing.T) { {"123", "123"}, {123, "123"}, {1.23, "1.23"}, + {100000000000, "100000000000"}, // avoid exponential notation + {100000000000.23, "100000000000.23"}, {nil, ""}, } diff --git a/hrp/step_request.go b/hrp/step_request.go index f890a93e..8c4de3a4 100644 --- a/hrp/step_request.go +++ b/hrp/step_request.go @@ -125,7 +125,7 @@ func (r *requestBuilder) prepareHeaders(stepVariables map[string]interface{}) er } r.req.AddCookie(&http.Cookie{ Name: cookieName, - Value: fmt.Sprintf("%v", value), + Value: convertString(value), }) } @@ -163,7 +163,7 @@ func (r *requestBuilder) prepareUrlParams(stepVariables map[string]interface{}) if len(parsedParams) > 0 { queryParams = make(url.Values) for k, v := range parsedParams { - queryParams.Add(k, fmt.Sprint(v)) + queryParams.Add(k, convertString(v)) } } } @@ -217,7 +217,7 @@ func (r *requestBuilder) prepareBody(stepVariables map[string]interface{}) error // post form data formData := make(url.Values) for k, v := range vv { - formData.Add(k, fmt.Sprint(v)) + formData.Add(k, convertString(v)) } dataBytes = []byte(formData.Encode()) } else {