mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
fix: post form data
This commit is contained in:
@@ -82,6 +82,7 @@ func TestCaseDemo(t *testing.T) {
|
||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||
hrp.Step("post form data").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
|
||||
WithParams(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
|
||||
|
||||
@@ -49,7 +49,8 @@ var demoTestCase = &TestCase{
|
||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||
Step("post form data").
|
||||
POST("/post").
|
||||
WithParams(map[string]interface{}{
|
||||
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
|
||||
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
|
||||
}).
|
||||
|
||||
@@ -100,7 +100,10 @@
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "/post",
|
||||
"params": {
|
||||
"headers": {
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
|
||||
},
|
||||
"body": {
|
||||
"foo1": "$varFoo1",
|
||||
"foo2": "${max($a, $b)}"
|
||||
}
|
||||
|
||||
@@ -67,7 +67,9 @@ teststeps:
|
||||
request:
|
||||
method: POST
|
||||
url: /post
|
||||
params:
|
||||
headers:
|
||||
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
|
||||
body:
|
||||
foo1: $varFoo1
|
||||
foo2: ${max($a, $b)}
|
||||
validate:
|
||||
|
||||
@@ -49,6 +49,7 @@ func TestCaseDemo(t *testing.T) {
|
||||
AssertEqual("body.json.foo2", 12.3, "check args foo2"),
|
||||
hrp.Step("post form data").
|
||||
POST("/post").
|
||||
WithHeaders(map[string]string{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}).
|
||||
WithParams(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
|
||||
|
||||
28
runner.go
28
runner.go
@@ -210,12 +210,23 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) {
|
||||
}
|
||||
var dataBytes []byte
|
||||
switch vv := data.(type) {
|
||||
case map[string]interface{}: // post json
|
||||
dataBytes, err = json.Marshal(vv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
case map[string]interface{}:
|
||||
contentType := req.Header.Get("Content-Type")
|
||||
if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") {
|
||||
// post form data
|
||||
formData := make(url.Values)
|
||||
for k, v := range vv {
|
||||
formData.Add(k, fmt.Sprint(v))
|
||||
}
|
||||
dataBytes = []byte(formData.Encode())
|
||||
} else {
|
||||
// post json
|
||||
dataBytes, err = json.Marshal(vv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
}
|
||||
setContentType(req, "application/json; charset=UTF-8")
|
||||
case string:
|
||||
dataBytes = []byte(vv)
|
||||
case []byte:
|
||||
@@ -237,7 +248,6 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) {
|
||||
|
||||
// do request action
|
||||
// req.Debug = r.debug
|
||||
// resp, err := r.client.Do(string(step.Request.Method), step.Request.URL, v...)
|
||||
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
@@ -315,9 +325,3 @@ func setBodyBytes(req *http.Request, data []byte) {
|
||||
req.Body = ioutil.NopCloser(bytes.NewReader(data))
|
||||
req.ContentLength = int64(len(data))
|
||||
}
|
||||
|
||||
func setContentType(req *http.Request, contentType string) {
|
||||
if req.Header.Get("Content-Type") == "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user