From 65712f626a4e06c26ff180d05a2ba8fa63abbc89 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 15 Oct 2021 21:12:22 +0800 Subject: [PATCH] refactor: post data with body --- convert_test.go | 2 +- examples/demo.json | 2 +- examples/demo.yaml | 2 +- examples/demo_test.go | 2 +- examples/extract_test.go | 2 +- examples/function_test.go | 2 +- examples/request_test.go | 6 +++--- examples/variables_test.go | 2 +- har2case/core.go | 6 +++--- har2case/core_test.go | 2 +- models.go | 3 +-- runner.go | 17 ++++++++--------- step.go | 9 ++------- step_test.go | 4 ++-- 14 files changed, 27 insertions(+), 34 deletions(-) diff --git a/convert_test.go b/convert_test.go index 0af879d1..40adb3bc 100644 --- a/convert_test.go +++ b/convert_test.go @@ -39,7 +39,7 @@ var demoTestCase = &TestCase{ AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string Step("post json data"). POST("/post"). - WithJSON(map[string]interface{}{ + WithBody(map[string]interface{}{ "foo1": "$varFoo1", // reference former extracted variable "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here }). diff --git a/examples/demo.json b/examples/demo.json index 99d1dcda..ffac9d65 100644 --- a/examples/demo.json +++ b/examples/demo.json @@ -69,7 +69,7 @@ "request": { "method": "POST", "url": "/post", - "json": { + "body": { "foo1": "$varFoo1", "foo2": "${max($a, $b)}" } diff --git a/examples/demo.yaml b/examples/demo.yaml index c14421ee..63df1984 100644 --- a/examples/demo.yaml +++ b/examples/demo.yaml @@ -47,7 +47,7 @@ teststeps: request: method: POST url: /post - json: + body: foo1: $varFoo1 foo2: ${max($a, $b)} validate: diff --git a/examples/demo_test.go b/examples/demo_test.go index 25247c2d..b60aa513 100644 --- a/examples/demo_test.go +++ b/examples/demo_test.go @@ -39,7 +39,7 @@ func TestCaseDemo(t *testing.T) { AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string httpboomer.Step("post json data"). POST("/post"). - WithJSON(map[string]interface{}{ + WithBody(map[string]interface{}{ "foo1": "$varFoo1", // reference former extracted variable "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here }). diff --git a/examples/extract_test.go b/examples/extract_test.go index f85368a2..bc0aaa63 100644 --- a/examples/extract_test.go +++ b/examples/extract_test.go @@ -74,7 +74,7 @@ func TestCaseExtractStepAssociation(t *testing.T) { httpboomer.Step("post json data"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). - WithJSON(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). + WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). Validate(). AssertEqual("status_code", "$statusCode", "check status code"). // assert with extracted variable from previous step AssertEqual("$varFoo1", "bar1", "check json foo1"). // assert with extracted variable from previous step diff --git a/examples/function_test.go b/examples/function_test.go index 098847fd..5e825724 100644 --- a/examples/function_test.go +++ b/examples/function_test.go @@ -32,7 +32,7 @@ func TestCaseCallFunction(t *testing.T) { httpboomer.Step("post json data with functions"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). - WithJSON(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). + WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). Validate(). AssertEqual("status_code", 200, "check status code"). AssertLengthEqual("body.json.foo1", 5, "check args foo1"). diff --git a/examples/request_test.go b/examples/request_test.go index 4a99f98f..58578291 100644 --- a/examples/request_test.go +++ b/examples/request_test.go @@ -27,7 +27,7 @@ func TestCaseBasicRequest(t *testing.T) { httpboomer.Step("post raw text"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "text/plain"}). - WithData("This is expected to be sent back as part of response body."). + WithBody("This is expected to be sent back as part of response body."). Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"), @@ -42,7 +42,7 @@ func TestCaseBasicRequest(t *testing.T) { httpboomer.Step("post json data"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). - WithJSON(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). + WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("body.json.foo1", "bar1", "check json foo1"). @@ -50,7 +50,7 @@ func TestCaseBasicRequest(t *testing.T) { httpboomer.Step("put request"). PUT("/put"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "text/plain"}). - WithData("This is expected to be sent back as part of response body."). + WithBody("This is expected to be sent back as part of response body."). Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"), diff --git a/examples/variables_test.go b/examples/variables_test.go index c297daa2..225faa3e 100644 --- a/examples/variables_test.go +++ b/examples/variables_test.go @@ -143,7 +143,7 @@ func TestCaseParseVariables(t *testing.T) { httpboomer.Step("post json data with functions"). POST("/post"). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). - WithJSON(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). + WithBody(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}). Validate(). AssertEqual("status_code", 200, "check status code"). AssertLengthEqual("body.json.foo1", 5, "check args foo1"). diff --git a/har2case/core.go b/har2case/core.go index 269e0314..1bd2439d 100644 --- a/har2case/core.go +++ b/har2case/core.go @@ -189,13 +189,13 @@ func (s *TStep) makeRequestBody(entry *Entry) error { mimeType := entry.Request.PostData.MimeType if strings.HasPrefix(mimeType, "application/json") { // post json - var data interface{} - err := json.Unmarshal([]byte(entry.Request.PostData.Text), &data) + var body interface{} + err := json.Unmarshal([]byte(entry.Request.PostData.Text), &body) if err != nil { log.Printf("makeRequestBody error: %v", err) return err } - s.Request.Data = data + s.Request.Body = body } else if strings.HasPrefix(mimeType, "application/x-www-form-urlencoded") { // TODO: post form data } diff --git a/har2case/core_test.go b/har2case/core_test.go index 7b52d14b..38081193 100644 --- a/har2case/core_test.go +++ b/har2case/core_test.go @@ -87,7 +87,7 @@ func TestMakeTestCase(t *testing.T) { } // make request data - if !assert.Equal(t, map[string]interface{}{"foo1": "HDnY8", "foo2": 12.3}, tCase.TestSteps[1].Request.Data) { + if !assert.Equal(t, map[string]interface{}{"foo1": "HDnY8", "foo2": 12.3}, tCase.TestSteps[1].Request.Body) { t.Fail() } } diff --git a/models.go b/models.go index d7b078bb..0431f988 100644 --- a/models.go +++ b/models.go @@ -28,8 +28,7 @@ type TRequest struct { Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"` Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"` - Data interface{} `json:"data,omitempty" yaml:"data,omitempty"` - JSON interface{} `json:"json,omitempty" yaml:"json,omitempty"` + Body interface{} `json:"body,omitempty" yaml:"body,omitempty"` Timeout float32 `json:"timeout,omitempty" yaml:"timeout,omitempty"` AllowRedirects bool `json:"allow_redirects,omitempty" yaml:"allow_redirects,omitempty"` Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"` diff --git a/runner.go b/runner.go index 3e593076..cc358d2d 100644 --- a/runner.go +++ b/runner.go @@ -134,19 +134,17 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { } v = append(v, req.Param(params.(map[string]interface{}))) } - if step.Request.Data != nil { - data, err := parseData(step.Request.Data, step.Variables) + if step.Request.Body != nil { + data, err := parseData(step.Request.Body, step.Variables) if err != nil { return nil, err } - v = append(v, data) - } - if step.Request.JSON != nil { - jsonData, err := parseData(step.Request.JSON, step.Variables) - if err != nil { - return nil, err + switch data.(type) { + case map[string]interface{}: // post json + v = append(v, req.BodyJSON(data)) + default: // post raw data + v = append(v, data) } - v = append(v, req.BodyJSON(jsonData)) } for cookieName, cookieValue := range step.Request.Cookies { @@ -158,6 +156,7 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { // do request action req.Debug = r.debug + // r.client.SetProxyUrl("http://127.0.0.1:8888") resp, err := r.client.Do(string(step.Request.Method), step.Request.URL, v...) if err != nil { return diff --git a/step.go b/step.go index b4bbd021..1dc959b2 100644 --- a/step.go +++ b/step.go @@ -148,13 +148,8 @@ func (s *requestWithOptionalArgs) WithCookies(cookies map[string]string) *reques return s } -func (s *requestWithOptionalArgs) WithData(data interface{}) *requestWithOptionalArgs { - s.step.Request.Data = data - return s -} - -func (s *requestWithOptionalArgs) WithJSON(json interface{}) *requestWithOptionalArgs { - s.step.Request.JSON = json +func (s *requestWithOptionalArgs) WithBody(body interface{}) *requestWithOptionalArgs { + s.step.Request.Body = body return s } diff --git a/step_test.go b/step_test.go index 530a8691..a3033150 100644 --- a/step_test.go +++ b/step_test.go @@ -20,7 +20,7 @@ var ( POST("/post"). WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}). - WithData("a=1&b=2"). + WithBody("a=1&b=2"). WithCookies(map[string]string{"user": "debugtalk"}). Validate(). AssertEqual("status_code", 200, "check status code") @@ -65,7 +65,7 @@ func TestRunRequestPostDataToStruct(t *testing.T) { if tStep.Request.Cookies["user"] != "debugtalk" { t.Fatalf("tStep.Request.Cookies mismatch") } - if tStep.Request.Data != "a=1&b=2" { + if tStep.Request.Body != "a=1&b=2" { t.Fatalf("tStep.Request.Data mismatch") } if tStep.Validators[0].Check != "status_code" || tStep.Validators[0].Expect != 200 {