refactor: post data with body

This commit is contained in:
debugtalk
2021-10-15 21:12:22 +08:00
parent 54531339f8
commit 65712f626a
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
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
}).

View File

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

View File

@@ -47,7 +47,7 @@ teststeps:
request:
method: POST
url: /post
json:
body:
foo1: $varFoo1
foo2: ${max($a, $b)}
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
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
}).

View File

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

View File

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

View File

@@ -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"),

View File

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

View File

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

View File

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

View File

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

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{})))
}
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

View File

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

View File

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