From e6467dfc280fe70d929d4e882e4631c0f0418ec4 Mon Sep 17 00:00:00 2001 From: buyuxiang <347586493@qq.com> Date: Tue, 15 Feb 2022 17:05:39 +0800 Subject: [PATCH] fix startswith endswith and unittest Change-Id: I3e1c6fd82812d13b4e23ecd6b23c4d57170852e1 --- docs/BUILTIN.md | 4 +- docs/cmd/hrp.md | 2 +- docs/cmd/hrp_boom.md | 2 +- docs/cmd/hrp_har2case.md | 2 +- docs/cmd/hrp_run.md | 2 +- docs/cmd/hrp_startproject.md | 2 +- examples/demo.json | 2 +- examples/demo.yaml | 2 +- examples/extract_test.go | 2 + examples/httpbingo_test.json | 81 ----------------------------------- examples/request_test.go | 1 + examples/validate_test.go | 5 ++- examples/variables_test.go | 3 ++ internal/builtin/assertion.go | 4 +- step_test.go | 1 + validate.go | 4 +- 16 files changed, 23 insertions(+), 96 deletions(-) delete mode 100644 examples/httpbingo_test.json diff --git a/docs/BUILTIN.md b/docs/BUILTIN.md index 4e818624..899e822c 100644 --- a/docs/BUILTIN.md +++ b/docs/BUILTIN.md @@ -39,8 +39,8 @@ Currently, HttpRunner+ has the following built-in assertion functions. | `contained_by` | contained by | A in B | 'a' contained_by 'abc', 1 contained_by [1,2] | | `type_match` | A and B are in the same type | type(A) == type(B) | 123 type_match 1 | | `regex_match` | regex matches | re.match(B, A) | 'abcdef' regex_match 'a\w+d' | -| `starts_with` | starts with | A.starts_with(B) is True | 'abc' starts_with 'ab' | -| `ends_with` | ends with | A.ends_with(B) is True | 'abc' ends_with 'bc' | +| `startswith` | starts with | A.startswith(B) is True | 'abc' startswith 'ab' | +| `endswith` | ends with | A.endswith(B) is True | 'abc' endswith 'bc' | ## Builtin functions diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index 2a01d308..9d12a34e 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -33,4 +33,4 @@ Copyright 2021 debugtalk * [hrp run](hrp_run.md) - run API test * [hrp startproject](hrp_startproject.md) - create a scaffold project -###### Auto generated by spf13/cobra on 14-Feb-2022 +###### Auto generated by spf13/cobra on 15-Feb-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index 914f747f..a0dcf982 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -39,4 +39,4 @@ hrp boom [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 14-Feb-2022 +###### Auto generated by spf13/cobra on 15-Feb-2022 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md index 504d6524..a24d244b 100644 --- a/docs/cmd/hrp_har2case.md +++ b/docs/cmd/hrp_har2case.md @@ -23,4 +23,4 @@ hrp har2case $har_path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 14-Feb-2022 +###### Auto generated by spf13/cobra on 15-Feb-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 7c6ca58c..2c781641 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -33,4 +33,4 @@ hrp run $path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 14-Feb-2022 +###### Auto generated by spf13/cobra on 15-Feb-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md index 1894cd0b..7921fc50 100644 --- a/docs/cmd/hrp_startproject.md +++ b/docs/cmd/hrp_startproject.md @@ -16,4 +16,4 @@ hrp startproject $project_name [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 14-Feb-2022 +###### Auto generated by spf13/cobra on 15-Feb-2022 diff --git a/examples/demo.json b/examples/demo.json index c59570f2..198bcbaa 100644 --- a/examples/demo.json +++ b/examples/demo.json @@ -48,7 +48,7 @@ }, { "check": "headers.\"Content-Type\"", - "assert": "starts_with", + "assert": "startswith", "expect": "application/json" }, { diff --git a/examples/demo.yaml b/examples/demo.yaml index b70adc53..ed6131dc 100644 --- a/examples/demo.yaml +++ b/examples/demo.yaml @@ -33,7 +33,7 @@ teststeps: expect: 200 msg: check response status code - check: headers."Content-Type" - assert: starts_with + assert: startswith expect: application/json - check: body.args.foo1 assert: length_equals diff --git a/examples/extract_test.go b/examples/extract_test.go index ac1d27b2..0d1184bd 100644 --- a/examples/extract_test.go +++ b/examples/extract_test.go @@ -58,9 +58,11 @@ func TestCaseExtractStepAssociation(t *testing.T) { WithHeaders(map[string]string{"User-Agent": "$agent"}). Extract(). WithJmesPath("status_code", "statusCode"). + WithJmesPath("headers.\"Content-Type\"", "contentType"). WithJmesPath("body.args.foo1", "varFoo1"). Validate(). AssertEqual("$statusCode", 200, "check status code"). + AssertEqual("$contentType", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("$varFoo1", "bar1", "check args foo1"). AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"), diff --git a/examples/httpbingo_test.json b/examples/httpbingo_test.json deleted file mode 100644 index f61b007e..00000000 --- a/examples/httpbingo_test.json +++ /dev/null @@ -1,81 +0,0 @@ -{ - "config": { - "name": "request methods testcase: validate with parameters", - "base_url": "https://httpbingo.bytedance.net" - }, - "teststeps": [ - { - "name": "get with params", - "request": { - "method": "GET", - "url": "/get", - "params": { - "a": 1, - "foo": "I'm a happy guy" - } - }, - "validate": [ - { - "check": "body.args.a", - "assert": "eq", - "expect": "1", - "msg": "check argument a" - }, - { - "check": "status_code", - "assert": "lt", - "expect": 201, - "msg": "check status code" - }, - { - "check": "status_code", - "assert": "gt", - "expect": 199, - "msg": "check status code" - }, - { - "check": "status_code", - "assert": "ge", - "expect": 200, - "msg": "check status code" - }, - { - "check": "status_code", - "assert": "le", - "expect": 200, - "msg": "check status code" - }, - { - "check": "status_code", - "assert": "ne", - "expect": 199, - "msg": "check status code" - }, - { - "check": "body.args.foo", - "assert": "contains", - "expect": "happy", - "msg": "check status code" - }, - { - "check": "body.args.foo", - "assert": "type_match", - "expect": "string", - "msg": "check status code" - }, - { - "check": "body.args.foo", - "assert": "starts_with", - "expect": "I'm", - "msg": "check status code" - }, - { - "check": "body.args.foo", - "assert": "type_match", - "expect": "string", - "msg": "check status code" - } - ] - } - ] -} diff --git a/examples/request_test.go b/examples/request_test.go index 6394cee6..2a8a2383 100644 --- a/examples/request_test.go +++ b/examples/request_test.go @@ -20,6 +20,7 @@ func TestCaseBasicRequest(t *testing.T) { }). Validate(). AssertEqual("status_code", 200, "check status code"). + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("body.args.foo1", "bar1", "check args foo1"). AssertEqual("body.args.foo2", "bar2", "check args foo2"), hrp.NewStep("post raw text"). diff --git a/examples/validate_test.go b/examples/validate_test.go index 941ea5c0..4f27075f 100644 --- a/examples/validate_test.go +++ b/examples/validate_test.go @@ -24,8 +24,9 @@ func TestCaseValidateStep(t *testing.T) { Extract(). WithJmesPath("body.args.foo1", "varFoo1"). Validate(). - AssertEqual("status_code", "$expectedStatusCode", "check status code"). // assert status code - AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath + AssertEqual("status_code", "$expectedStatusCode", "check status code"). // assert status code + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). // assert response header, with double quotes + AssertEqual("body.args.foo1", "bar1", "check args foo1"). // assert response json body with jmespath AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"), hrp.NewStep("get with params"). diff --git a/examples/variables_test.go b/examples/variables_test.go index d9875237..d1f856fc 100644 --- a/examples/variables_test.go +++ b/examples/variables_test.go @@ -22,6 +22,7 @@ func TestCaseConfigVariables(t *testing.T) { WithHeaders(map[string]string{"User-Agent": "$agent"}). Validate(). AssertEqual("status_code", "$expectedStatusCode", "check status code"). + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("body.args.foo1", "bar1", "check args foo1"). AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"), @@ -51,6 +52,7 @@ func TestCaseStepVariables(t *testing.T) { WithHeaders(map[string]string{"User-Agent": "$agent"}). Validate(). AssertEqual("status_code", "$expectedStatusCode", "check status code"). + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("body.args.foo1", "bar1", "check args foo1"). AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"), @@ -84,6 +86,7 @@ func TestCaseOverrideConfigVariables(t *testing.T) { WithHeaders(map[string]string{"User-Agent": "$agent"}). Validate(). AssertEqual("status_code", "$expectedStatusCode", "check status code"). + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("body.args.foo1", "bar1", "check args foo1"). AssertEqual("body.args.foo2", "bar2", "check args foo2"). AssertEqual("body.headers.\"user-agent\"", "HttpRunnerPlus", "check header user agent"), diff --git a/internal/builtin/assertion.go b/internal/builtin/assertion.go index 6473666e..01eb3157 100644 --- a/internal/builtin/assertion.go +++ b/internal/builtin/assertion.go @@ -25,8 +25,8 @@ var Assertions = map[string]func(t assert.TestingT, actual interface{}, expected "contains": assert.Contains, "type_match": assert.IsType, // custom assertions - "starts_with": StartsWith, - "ends_with": EndsWith, + "startswith": StartsWith, + "endswith": EndsWith, "len_eq": EqualLength, "length_equals": EqualLength, "length_equal": EqualLength, diff --git a/step_test.go b/step_test.go index 578f50ef..b81b48d3 100644 --- a/step_test.go +++ b/step_test.go @@ -12,6 +12,7 @@ var ( WithCookies(map[string]string{"user": "debugtalk"}). Validate(). AssertEqual("status_code", 200, "check status code"). + AssertEqual("headers.\"Content-Type\"", "application/json; charset=utf-8", "check header Content-Type"). AssertEqual("body.args.foo1", "bar1", "check param foo1"). AssertEqual("body.args.foo2", "bar2", "check param foo2") stepPOSTData = NewStep("post form data"). diff --git a/validate.go b/validate.go index fe742b64..51150563 100644 --- a/validate.go +++ b/validate.go @@ -126,7 +126,7 @@ func (s *StepRequestValidation) AssertRegexp(jmesPath string, expected interface func (s *StepRequestValidation) AssertStartsWith(jmesPath string, expected interface{}, msg string) *StepRequestValidation { v := Validator{ Check: jmesPath, - Assert: "starts_with", + Assert: "startswith", Expect: expected, Message: msg, } @@ -137,7 +137,7 @@ func (s *StepRequestValidation) AssertStartsWith(jmesPath string, expected inter func (s *StepRequestValidation) AssertEndsWith(jmesPath string, expected interface{}, msg string) *StepRequestValidation { v := Validator{ Check: jmesPath, - Assert: "ends_with", + Assert: "endswith", Expect: expected, Message: msg, }