fix: error of concurrent map writes occurred while uploading in boom mode

This commit is contained in:
xucong053
2022-06-30 23:29:16 +08:00
parent 14dc566eea
commit ee8cfcae86
4 changed files with 37 additions and 19 deletions

View File

@@ -117,6 +117,10 @@ func extendWithAPI(testStep *TStep, overriddenStep *API) {
}
// merge & override request
testStep.Request = overriddenStep.Request
// init upload
if testStep.Request.Upload != nil {
initUpload(testStep)
}
// merge & override variables
testStep.Variables = mergeVariables(testStep.Variables, overriddenStep.Variables)
// merge & override extractors

View File

@@ -259,24 +259,31 @@ func (r *requestBuilder) prepareBody(stepVariables map[string]interface{}) error
return nil
}
func prepareUpload(parser *Parser, step *TStep) (err error) {
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)}")
func initUpload(step *TStep) {
if step.Request.Headers == nil {
step.Request.Headers = make(map[string]string)
}
step.Request.Headers["Content-Type"] = "${multipart_content_type($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
}
@@ -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 {
return
}
// override step variables
stepVariables, err := r.MergeStepVariables(step.Variables)
err = prepareUpload(r.parser, step, stepVariables)
if err != nil {
return
}
@@ -822,6 +829,8 @@ func (s *StepRequestWithOptionalArgs) WithBody(body interface{}) *StepRequestWit
// WithUpload sets HTTP request body for uploading file(s).
func (s *StepRequestWithOptionalArgs) WithUpload(upload map[string]interface{}) *StepRequestWithOptionalArgs {
// init upload
initUpload(s.step)
s.step.Request.Upload = upload
return s
}

View File

@@ -144,6 +144,10 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
step: step,
})
} else if step.Request != nil {
// init upload
if step.Request.Upload != nil {
initUpload(step)
}
testCase.TestSteps = append(testCase.TestSteps, &StepRequestWithOptionalArgs{
step: step,
})

View File

@@ -9,12 +9,13 @@ import (
func TestCaseUploadFile(t *testing.T) {
testcase := &hrp.TestCase{
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{
hrp.NewStep("upload file").
WithVariables(map[string]interface{}{
"m_encoder": "${multipart_encoder($m_upload)}",
"m_upload": map[string]interface{}{"file": "test.env"},
"m_upload": map[string]interface{}{"file": "$upload_file"},
}).
POST("/post").
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"),
hrp.NewStep("upload file with keyword").
POST("/post").
WithUpload(map[string]interface{}{"file": "test.env"}).
WithUpload(map[string]interface{}{"file": "$upload_file"}).
Validate().
AssertEqual("status_code", 200, "check status code").
AssertStartsWith("body.files.file", "UserName=test", "check uploaded file"),