fix #1352: avoid conversion to exponential notation

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

View File

@@ -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**

View File

@@ -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"
}

View File

@@ -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"
},
{

View File

@@ -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

View File

@@ -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"
}

View File

@@ -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"
},
{

View File

@@ -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

View File

@@ -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"
},
{

View File

@@ -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

View File

@@ -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(

View File

@@ -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).

View File

@@ -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, "<nil>"},
}

View File

@@ -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 {