fix #1352: avoid conversion to exponential notation

This commit is contained in:
debugtalk
2022-06-14 13:22:07 +08:00
parent 50c4c94100
commit 5a5b83fc89
13 changed files with 33 additions and 27 deletions

View File

@@ -8,6 +8,7 @@
- feat: assert python3 package is ready with specified version - feat: assert python3 package is ready with specified version
- refactor: build plugin mechanism, cancel automatic installation of dependencies - refactor: build plugin mechanism, cancel automatic installation of dependencies
- fix: pip upgrade httprunner when installing hrp - fix: pip upgrade httprunner when installing hrp
- fix #1352: avoid conversion to exponential notation
**python version** **python version**

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-with-go-plugin", "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" "hrp_version": "v4.1.3"
} }

View File

@@ -22,7 +22,7 @@
"variables": { "variables": {
"foo1": "${ENV(USERNAME)}", "foo1": "${ENV(USERNAME)}",
"foo2": "bar21", "foo2": "bar21",
"sum_v": "${sum_two_int(1, 2)}" "sum_v": "${sum_two_int(10000000, 20000000)}"
}, },
"request": { "request": {
"method": "GET", "method": "GET",
@@ -52,7 +52,7 @@
{ {
"check": "body.args.sum_v", "check": "body.args.sum_v",
"assert": "equal", "assert": "equal",
"expect": "3", "expect": "30000000",
"msg": "check body.args.sum_v" "msg": "check body.args.sum_v"
}, },
{ {

View File

@@ -16,7 +16,7 @@ teststeps:
variables: variables:
foo1: ${ENV(USERNAME)} foo1: ${ENV(USERNAME)}
foo2: bar21 foo2: bar21
sum_v: "${sum_two_int(1, 2)}" sum_v: "${sum_two_int(10000000, 20000000)}"
request: request:
method: GET method: GET
url: $base_url/get url: $base_url/get
@@ -29,7 +29,7 @@ teststeps:
validate: validate:
- eq: ["status_code", 200] - eq: ["status_code", 200]
- eq: ["body.args.foo1", "debugtalk"] - eq: ["body.args.foo1", "debugtalk"]
- eq: ["body.args.sum_v", "3"] - eq: ["body.args.sum_v", "30000000"]
- eq: ["body.args.foo2", "bar21"] - eq: ["body.args.foo2", "bar21"]
- -
name: post raw text name: post raw text

View File

@@ -1,5 +1,5 @@
{ {
"project_name": "demo-with-py-plugin", "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" "hrp_version": "v4.1.3"
} }

View File

@@ -22,7 +22,7 @@
"variables": { "variables": {
"foo1": "${ENV(USERNAME)}", "foo1": "${ENV(USERNAME)}",
"foo2": "bar21", "foo2": "bar21",
"sum_v": "${sum_two_int(1, 2)}" "sum_v": "${sum_two_int(10000000, 20000000)}"
}, },
"request": { "request": {
"method": "GET", "method": "GET",
@@ -52,7 +52,7 @@
{ {
"check": "body.args.sum_v", "check": "body.args.sum_v",
"assert": "equal", "assert": "equal",
"expect": "3", "expect": "30000000",
"msg": "check body.args.sum_v" "msg": "check body.args.sum_v"
}, },
{ {

View File

@@ -16,7 +16,7 @@ teststeps:
variables: variables:
foo1: ${ENV(USERNAME)} foo1: ${ENV(USERNAME)}
foo2: bar21 foo2: bar21
sum_v: "${sum_two_int(1, 2)}" sum_v: "${sum_two_int(10000000, 20000000)}"
request: request:
method: GET method: GET
url: $base_url/get url: $base_url/get
@@ -29,7 +29,7 @@ teststeps:
validate: validate:
- eq: ["status_code", 200] - eq: ["status_code", 200]
- eq: ["body.args.foo1", "debugtalk"] - eq: ["body.args.foo1", "debugtalk"]
- eq: ["body.args.sum_v", "3"] - eq: ["body.args.sum_v", "30000000"]
- eq: ["body.args.foo2", "bar21"] - eq: ["body.args.foo2", "bar21"]
- -
name: post raw text name: post raw text

View File

@@ -22,7 +22,7 @@
"variables": { "variables": {
"foo1": "${ENV(USERNAME)}", "foo1": "${ENV(USERNAME)}",
"foo2": "bar21", "foo2": "bar21",
"sum_v": "${sum_two_int(1, 2)}" "sum_v": "${sum_two_int(10000000, 20000000)}"
}, },
"request": { "request": {
"method": "GET", "method": "GET",
@@ -52,7 +52,7 @@
{ {
"check": "body.args.sum_v", "check": "body.args.sum_v",
"assert": "equal", "assert": "equal",
"expect": "3", "expect": "30000000",
"msg": "check body.args.sum_v" "msg": "check body.args.sum_v"
}, },
{ {

View File

@@ -16,7 +16,7 @@ teststeps:
variables: variables:
foo1: ${ENV(USERNAME)} foo1: ${ENV(USERNAME)}
foo2: bar21 foo2: bar21
sum_v: "${sum_two_int(1, 2)}" sum_v: "${sum_two_int(10000000, 20000000)}"
request: request:
method: GET method: GET
url: $base_url/get url: $base_url/get
@@ -29,7 +29,7 @@ teststeps:
validate: validate:
- eq: ["status_code", 200] - eq: ["status_code", 200]
- eq: ["body.args.foo1", "debugtalk"] - eq: ["body.args.foo1", "debugtalk"]
- eq: ["body.args.sum_v", "3"] - eq: ["body.args.sum_v", "30000000"]
- eq: ["body.args.foo2", "bar21"] - eq: ["body.args.foo2", "bar21"]
- -
name: post raw text name: post raw text

View File

@@ -26,7 +26,7 @@ class TestCaseDemoRequests(HttpRunner):
Step( Step(
RunRequest("get with params") RunRequest("get with params")
.with_variables( .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") .get("/get")
.with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"}) .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
@@ -36,7 +36,7 @@ class TestCaseDemoRequests(HttpRunner):
.validate() .validate()
.assert_equal("status_code", 200) .assert_equal("status_code", 200)
.assert_equal("body.args.foo1", "bar11") .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") .assert_equal("body.args.foo2", "bar21")
), ),
Step( Step(

View File

@@ -7,6 +7,7 @@ import (
"path" "path"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/httprunner/funplugin" "github.com/httprunner/funplugin"
@@ -66,13 +67,15 @@ func (p *Parser) ParseHeaders(rawHeaders map[string]string, variablesMapping map
} }
func convertString(raw interface{}) string { func convertString(raw interface{}) string {
if value, ok := raw.(string); ok { if str, ok := raw.(string); ok {
return value return str
} else {
// raw is not string, e.g. int, float, etc.
// convert to string
return fmt.Sprintf("%v", raw)
} }
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) { 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" // raw_string contains one or many functions, e.g. "abc${add_one(3)}def"
matchStartPosition += len(funcMatched[0]) matchStartPosition += len(funcMatched[0])
parsedString += fmt.Sprintf("%v", result) parsedString += convertString(result)
remainedString = raw[matchStartPosition:] remainedString = raw[matchStartPosition:]
log.Debug(). log.Debug().
Str("parsedString", parsedString). Str("parsedString", parsedString).
@@ -232,7 +235,7 @@ func (p *Parser) ParseString(raw string, variablesMapping map[string]interface{}
} }
matchStartPosition += len(varMatched[0]) matchStartPosition += len(varMatched[0])
parsedString += fmt.Sprintf("%v", varValue) parsedString += convertString(varValue)
remainedString = raw[matchStartPosition:] remainedString = raw[matchStartPosition:]
log.Debug(). log.Debug().
Str("parsedString", parsedString). Str("parsedString", parsedString).

View File

@@ -613,6 +613,8 @@ func TestConvertString(t *testing.T) {
{"123", "123"}, {"123", "123"},
{123, "123"}, {123, "123"},
{1.23, "1.23"}, {1.23, "1.23"},
{100000000000, "100000000000"}, // avoid exponential notation
{100000000000.23, "100000000000.23"},
{nil, "<nil>"}, {nil, "<nil>"},
} }

View File

@@ -125,7 +125,7 @@ func (r *requestBuilder) prepareHeaders(stepVariables map[string]interface{}) er
} }
r.req.AddCookie(&http.Cookie{ r.req.AddCookie(&http.Cookie{
Name: cookieName, 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 { if len(parsedParams) > 0 {
queryParams = make(url.Values) queryParams = make(url.Values)
for k, v := range parsedParams { 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 // post form data
formData := make(url.Values) formData := make(url.Values)
for k, v := range vv { for k, v := range vv {
formData.Add(k, fmt.Sprint(v)) formData.Add(k, convertString(v))
} }
dataBytes = []byte(formData.Encode()) dataBytes = []byte(formData.Encode())
} else { } else {