refactor: post data with body

This commit is contained in:
debugtalk
2021-10-15 21:12:22 +08:00
parent 17035e1668
commit b0fe1e42af
14 changed files with 27 additions and 34 deletions

View File

@@ -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 AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
Step("post json data"). Step("post json data").
POST("/post"). POST("/post").
WithJSON(map[string]interface{}{ WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable "foo1": "$varFoo1", // reference former extracted variable
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here
}). }).

View File

@@ -69,7 +69,7 @@
"request": { "request": {
"method": "POST", "method": "POST",
"url": "/post", "url": "/post",
"json": { "body": {
"foo1": "$varFoo1", "foo1": "$varFoo1",
"foo2": "${max($a, $b)}" "foo2": "${max($a, $b)}"
} }

View File

@@ -47,7 +47,7 @@ teststeps:
request: request:
method: POST method: POST
url: /post url: /post
json: body:
foo1: $varFoo1 foo1: $varFoo1
foo2: ${max($a, $b)} foo2: ${max($a, $b)}
validate: validate:

View File

@@ -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 AssertEqual("body.args.foo2", "34.5", "check args foo2"), // notice: request params value will be converted to string
httpboomer.Step("post json data"). httpboomer.Step("post json data").
POST("/post"). POST("/post").
WithJSON(map[string]interface{}{ WithBody(map[string]interface{}{
"foo1": "$varFoo1", // reference former extracted variable "foo1": "$varFoo1", // reference former extracted variable
"foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here "foo2": "${max($a, $b)}", // 12.3; step level variables are independent, variable b is 3.45 here
}). }).

View File

@@ -74,7 +74,7 @@ func TestCaseExtractStepAssociation(t *testing.T) {
httpboomer.Step("post json data"). httpboomer.Step("post json data").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).
WithJSON(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
Validate(). Validate().
AssertEqual("status_code", "$statusCode", "check status code"). // assert with extracted variable from previous step 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 AssertEqual("$varFoo1", "bar1", "check json foo1"). // assert with extracted variable from previous step

View File

@@ -32,7 +32,7 @@ func TestCaseCallFunction(t *testing.T) {
httpboomer.Step("post json data with functions"). httpboomer.Step("post json data with functions").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). 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(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1"). AssertLengthEqual("body.json.foo1", 5, "check args foo1").

View File

@@ -27,7 +27,7 @@ func TestCaseBasicRequest(t *testing.T) {
httpboomer.Step("post raw text"). httpboomer.Step("post raw text").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "text/plain"}). 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(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"), 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"). httpboomer.Step("post json data").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).
WithJSON(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). WithBody(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
Validate(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertEqual("body.json.foo1", "bar1", "check json foo1"). AssertEqual("body.json.foo1", "bar1", "check json foo1").
@@ -50,7 +50,7 @@ func TestCaseBasicRequest(t *testing.T) {
httpboomer.Step("put request"). httpboomer.Step("put request").
PUT("/put"). PUT("/put").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "text/plain"}). 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(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"), AssertEqual("body.data", "This is expected to be sent back as part of response body.", "check data"),

View File

@@ -143,7 +143,7 @@ func TestCaseParseVariables(t *testing.T) {
httpboomer.Step("post json data with functions"). httpboomer.Step("post json data with functions").
POST("/post"). POST("/post").
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). 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(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertLengthEqual("body.json.foo1", 5, "check args foo1"). AssertLengthEqual("body.json.foo1", 5, "check args foo1").

View File

@@ -189,13 +189,13 @@ func (s *TStep) makeRequestBody(entry *Entry) error {
mimeType := entry.Request.PostData.MimeType mimeType := entry.Request.PostData.MimeType
if strings.HasPrefix(mimeType, "application/json") { if strings.HasPrefix(mimeType, "application/json") {
// post json // post json
var data interface{} var body interface{}
err := json.Unmarshal([]byte(entry.Request.PostData.Text), &data) err := json.Unmarshal([]byte(entry.Request.PostData.Text), &body)
if err != nil { if err != nil {
log.Printf("makeRequestBody error: %v", err) log.Printf("makeRequestBody error: %v", err)
return err return err
} }
s.Request.Data = data s.Request.Body = body
} else if strings.HasPrefix(mimeType, "application/x-www-form-urlencoded") { } else if strings.HasPrefix(mimeType, "application/x-www-form-urlencoded") {
// TODO: post form data // TODO: post form data
} }

View File

@@ -87,7 +87,7 @@ func TestMakeTestCase(t *testing.T) {
} }
// make request data // 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() t.Fail()
} }
} }

View File

@@ -28,8 +28,7 @@ type TRequest struct {
Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"` Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"` Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"`
Data interface{} `json:"data,omitempty" yaml:"data,omitempty"` Body interface{} `json:"body,omitempty" yaml:"body,omitempty"`
JSON interface{} `json:"json,omitempty" yaml:"json,omitempty"`
Timeout float32 `json:"timeout,omitempty" yaml:"timeout,omitempty"` Timeout float32 `json:"timeout,omitempty" yaml:"timeout,omitempty"`
AllowRedirects bool `json:"allow_redirects,omitempty" yaml:"allow_redirects,omitempty"` AllowRedirects bool `json:"allow_redirects,omitempty" yaml:"allow_redirects,omitempty"`
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"` Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`

View File

@@ -134,19 +134,17 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) {
} }
v = append(v, req.Param(params.(map[string]interface{}))) v = append(v, req.Param(params.(map[string]interface{})))
} }
if step.Request.Data != nil { if step.Request.Body != nil {
data, err := parseData(step.Request.Data, step.Variables) data, err := parseData(step.Request.Body, step.Variables)
if err != nil { if err != nil {
return nil, err return nil, err
} }
v = append(v, data) switch data.(type) {
} case map[string]interface{}: // post json
if step.Request.JSON != nil { v = append(v, req.BodyJSON(data))
jsonData, err := parseData(step.Request.JSON, step.Variables) default: // post raw data
if err != nil { v = append(v, data)
return nil, err
} }
v = append(v, req.BodyJSON(jsonData))
} }
for cookieName, cookieValue := range step.Request.Cookies { for cookieName, cookieValue := range step.Request.Cookies {
@@ -158,6 +156,7 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) {
// do request action // do request action
req.Debug = r.debug 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...) resp, err := r.client.Do(string(step.Request.Method), step.Request.URL, v...)
if err != nil { if err != nil {
return return

View File

@@ -148,13 +148,8 @@ func (s *requestWithOptionalArgs) WithCookies(cookies map[string]string) *reques
return s return s
} }
func (s *requestWithOptionalArgs) WithData(data interface{}) *requestWithOptionalArgs { func (s *requestWithOptionalArgs) WithBody(body interface{}) *requestWithOptionalArgs {
s.step.Request.Data = data s.step.Request.Body = body
return s
}
func (s *requestWithOptionalArgs) WithJSON(json interface{}) *requestWithOptionalArgs {
s.step.Request.JSON = json
return s return s
} }

View File

@@ -20,7 +20,7 @@ var (
POST("/post"). POST("/post").
WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}). 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"}). WithCookies(map[string]string{"user": "debugtalk"}).
Validate(). Validate().
AssertEqual("status_code", 200, "check status code") AssertEqual("status_code", 200, "check status code")
@@ -65,7 +65,7 @@ func TestRunRequestPostDataToStruct(t *testing.T) {
if tStep.Request.Cookies["user"] != "debugtalk" { if tStep.Request.Cookies["user"] != "debugtalk" {
t.Fatalf("tStep.Request.Cookies mismatch") 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") t.Fatalf("tStep.Request.Data mismatch")
} }
if tStep.Validators[0].Check != "status_code" || tStep.Validators[0].Expect != 200 { if tStep.Validators[0].Check != "status_code" || tStep.Validators[0].Expect != 200 {