mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 06:51:44 +08:00
fix: error of concurrent map writes occurred while uploading in boom mode
This commit is contained in:
@@ -117,6 +117,10 @@ func extendWithAPI(testStep *TStep, overriddenStep *API) {
|
|||||||
}
|
}
|
||||||
// merge & override request
|
// merge & override request
|
||||||
testStep.Request = overriddenStep.Request
|
testStep.Request = overriddenStep.Request
|
||||||
|
// init upload
|
||||||
|
if testStep.Request.Upload != nil {
|
||||||
|
initUpload(testStep)
|
||||||
|
}
|
||||||
// merge & override variables
|
// merge & override variables
|
||||||
testStep.Variables = mergeVariables(testStep.Variables, overriddenStep.Variables)
|
testStep.Variables = mergeVariables(testStep.Variables, overriddenStep.Variables)
|
||||||
// merge & override extractors
|
// merge & override extractors
|
||||||
|
|||||||
@@ -259,24 +259,31 @@ func (r *requestBuilder) prepareBody(stepVariables map[string]interface{}) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareUpload(parser *Parser, step *TStep) (err error) {
|
func initUpload(step *TStep) {
|
||||||
if step.Request.Upload == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
step.Request.Upload, err = parser.ParseVariables(step.Request.Upload)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if step.Variables == nil {
|
|
||||||
step.Variables = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
step.Variables["m_upload"] = step.Request.Upload
|
|
||||||
step.Variables["m_encoder"] = fmt.Sprintf("${multipart_encoder($m_upload)}")
|
|
||||||
if step.Request.Headers == nil {
|
if step.Request.Headers == nil {
|
||||||
step.Request.Headers = make(map[string]string)
|
step.Request.Headers = make(map[string]string)
|
||||||
}
|
}
|
||||||
step.Request.Headers["Content-Type"] = "${multipart_content_type($m_encoder)}"
|
step.Request.Headers["Content-Type"] = "${multipart_content_type($m_encoder)}"
|
||||||
step.Request.Body = "$m_encoder"
|
step.Request.Body = "$m_encoder"
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareUpload(parser *Parser, step *TStep, stepVariables map[string]interface{}) (err error) {
|
||||||
|
if step.Request.Upload == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uploadSlice := map[string]interface{}{}
|
||||||
|
for key, value := range step.Request.Upload {
|
||||||
|
uploadSlice[key], err = parser.Parse(value, stepVariables)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stepVariables["m_upload"] = uploadSlice
|
||||||
|
mEncoder, err := parser.Parse("${multipart_encoder($m_upload)}", stepVariables)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stepVariables["m_encoder"] = mEncoder
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,13 +302,13 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = prepareUpload(r.parser, step)
|
// override step variables
|
||||||
|
stepVariables, err := r.MergeStepVariables(step.Variables)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// override step variables
|
err = prepareUpload(r.parser, step, stepVariables)
|
||||||
stepVariables, err := r.MergeStepVariables(step.Variables)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -822,6 +829,8 @@ func (s *StepRequestWithOptionalArgs) WithBody(body interface{}) *StepRequestWit
|
|||||||
|
|
||||||
// WithUpload sets HTTP request body for uploading file(s).
|
// WithUpload sets HTTP request body for uploading file(s).
|
||||||
func (s *StepRequestWithOptionalArgs) WithUpload(upload map[string]interface{}) *StepRequestWithOptionalArgs {
|
func (s *StepRequestWithOptionalArgs) WithUpload(upload map[string]interface{}) *StepRequestWithOptionalArgs {
|
||||||
|
// init upload
|
||||||
|
initUpload(s.step)
|
||||||
s.step.Request.Upload = upload
|
s.step.Request.Upload = upload
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,6 +144,10 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
|
|||||||
step: step,
|
step: step,
|
||||||
})
|
})
|
||||||
} else if step.Request != nil {
|
} else if step.Request != nil {
|
||||||
|
// init upload
|
||||||
|
if step.Request.Upload != nil {
|
||||||
|
initUpload(step)
|
||||||
|
}
|
||||||
testCase.TestSteps = append(testCase.TestSteps, &StepRequestWithOptionalArgs{
|
testCase.TestSteps = append(testCase.TestSteps, &StepRequestWithOptionalArgs{
|
||||||
step: step,
|
step: step,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,12 +9,13 @@ import (
|
|||||||
func TestCaseUploadFile(t *testing.T) {
|
func TestCaseUploadFile(t *testing.T) {
|
||||||
testcase := &hrp.TestCase{
|
testcase := &hrp.TestCase{
|
||||||
Config: hrp.NewConfig("test upload file to httpbin").
|
Config: hrp.NewConfig("test upload file to httpbin").
|
||||||
SetBaseURL("https://httpbin.org"),
|
SetBaseURL("https://httpbin.org").
|
||||||
|
WithVariables(map[string]interface{}{"upload_file": "test.env"}),
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("upload file").
|
hrp.NewStep("upload file").
|
||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
"m_encoder": "${multipart_encoder($m_upload)}",
|
"m_encoder": "${multipart_encoder($m_upload)}",
|
||||||
"m_upload": map[string]interface{}{"file": "test.env"},
|
"m_upload": map[string]interface{}{"file": "$upload_file"},
|
||||||
}).
|
}).
|
||||||
POST("/post").
|
POST("/post").
|
||||||
WithHeaders(map[string]string{"Content-Type": "${multipart_content_type($m_encoder)}"}).
|
WithHeaders(map[string]string{"Content-Type": "${multipart_content_type($m_encoder)}"}).
|
||||||
@@ -24,7 +25,7 @@ func TestCaseUploadFile(t *testing.T) {
|
|||||||
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),
|
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),
|
||||||
hrp.NewStep("upload file with keyword").
|
hrp.NewStep("upload file with keyword").
|
||||||
POST("/post").
|
POST("/post").
|
||||||
WithUpload(map[string]interface{}{"file": "test.env"}).
|
WithUpload(map[string]interface{}{"file": "$upload_file"}).
|
||||||
Validate().
|
Validate().
|
||||||
AssertEqual("status_code", 200, "check status code").
|
AssertEqual("status_code", 200, "check status code").
|
||||||
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),
|
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),
|
||||||
|
|||||||
Reference in New Issue
Block a user