mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 19:47:14 +08:00
fix: post json with empty data
This commit is contained in:
@@ -182,7 +182,6 @@ func (s *tStep) makeRequestMethod(entry *Entry) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *tStep) makeRequestURL(entry *Entry) error {
|
func (s *tStep) makeRequestURL(entry *Entry) error {
|
||||||
|
|
||||||
u, err := url.Parse(entry.Request.URL)
|
u, err := url.Parse(entry.Request.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("make request url failed")
|
log.Error().Err(err).Msg("make request url failed")
|
||||||
@@ -230,10 +229,14 @@ func (s *tStep) makeRequestBody(entry *Entry) error {
|
|||||||
if strings.HasPrefix(mimeType, "application/json") {
|
if strings.HasPrefix(mimeType, "application/json") {
|
||||||
// post json
|
// post json
|
||||||
var body interface{}
|
var body interface{}
|
||||||
err := json.Unmarshal([]byte(entry.Request.PostData.Text), &body)
|
if entry.Request.PostData.Text == "" {
|
||||||
if err != nil {
|
body = nil
|
||||||
log.Error().Err(err).Msg("make request body failed")
|
} else {
|
||||||
return err
|
err := json.Unmarshal([]byte(entry.Request.PostData.Text), &body)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("make request body failed")
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s.Request.Body = body
|
s.Request.Body = body
|
||||||
} else if strings.HasPrefix(mimeType, "application/x-www-form-urlencoded") {
|
} else if strings.HasPrefix(mimeType, "application/x-www-form-urlencoded") {
|
||||||
|
|||||||
@@ -120,3 +120,130 @@ func TestGetFilenameWithoutExtension(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMakeRequestDataParams(t *testing.T) {
|
||||||
|
har := NewHAR("")
|
||||||
|
entry := &Entry{
|
||||||
|
Request: Request{
|
||||||
|
Method: "POST",
|
||||||
|
PostData: PostData{
|
||||||
|
MimeType: "application/x-www-form-urlencoded; charset=utf-8",
|
||||||
|
Params: []PostParam{
|
||||||
|
{Name: "a", Value: "1"},
|
||||||
|
{Name: "b", Value: "2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
step, err := har.prepareTestStep(entry)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Equal(t, "a=1&b=2", step.Request.Body) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeRequestDataJSON(t *testing.T) {
|
||||||
|
har := NewHAR("")
|
||||||
|
entry := &Entry{
|
||||||
|
Request: Request{
|
||||||
|
Method: "POST",
|
||||||
|
PostData: PostData{
|
||||||
|
MimeType: "application/json; charset=utf-8",
|
||||||
|
Text: "{\"a\":\"1\",\"b\":\"2\"}",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
step, err := har.prepareTestStep(entry)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Equal(t, map[string]interface{}{"a": "1", "b": "2"}, step.Request.Body) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeRequestDataTextEmpty(t *testing.T) {
|
||||||
|
har := NewHAR("")
|
||||||
|
entry := &Entry{
|
||||||
|
Request: Request{
|
||||||
|
Method: "POST",
|
||||||
|
PostData: PostData{
|
||||||
|
MimeType: "application/json; charset=utf-8",
|
||||||
|
Text: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
step, err := har.prepareTestStep(entry)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Equal(t, nil, step.Request.Body) { // TODO
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeValidate(t *testing.T) {
|
||||||
|
har := NewHAR("")
|
||||||
|
entry := &Entry{
|
||||||
|
Response: Response{
|
||||||
|
Status: 200,
|
||||||
|
Headers: []NVP{
|
||||||
|
{Name: "Content-Type", Value: "application/json; charset=utf-8"},
|
||||||
|
},
|
||||||
|
Content: Content{
|
||||||
|
Size: 71,
|
||||||
|
MimeType: "application/json; charset=utf-8",
|
||||||
|
// map[Code:200 IsSuccess:true Message:<nil> Value:map[BlnResult:true]]
|
||||||
|
Text: "eyJJc1N1Y2Nlc3MiOnRydWUsIkNvZGUiOjIwMCwiTWVzc2FnZSI6bnVsbCwiVmFsdWUiOnsiQmxuUmVzdWx0Ijp0cnVlfX0=",
|
||||||
|
Encoding: "base64",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
step, err := har.prepareTestStep(entry)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
validator, ok := step.Validators[0].(hrp.Validator)
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, validator,
|
||||||
|
hrp.Validator{
|
||||||
|
Check: "status_code",
|
||||||
|
Expect: 200,
|
||||||
|
Assert: "equals",
|
||||||
|
Message: "assert response status code"}) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
validator, ok = step.Validators[1].(hrp.Validator)
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, validator,
|
||||||
|
hrp.Validator{
|
||||||
|
Check: "headers.\"Content-Type\"",
|
||||||
|
Expect: "application/json; charset=utf-8",
|
||||||
|
Assert: "equals",
|
||||||
|
Message: "assert response header Content-Type"}) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
validator, ok = step.Validators[2].(hrp.Validator)
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, validator,
|
||||||
|
hrp.Validator{
|
||||||
|
Check: "body.Code",
|
||||||
|
Expect: float64(200), // TODO
|
||||||
|
Assert: "equals",
|
||||||
|
Message: "assert response body Code"}) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user