starsz commented on a change in pull request #1465: URL: https://github.com/apache/apisix-dashboard/pull/1465#discussion_r580794487
########## File path: api/test/e2enew/base/http.go ########## @@ -92,3 +104,44 @@ func BatchTestServerPort(times int) map[string]int { return res } + +func GetReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string, error) { + if strings.Index(contentType, "json") > -1 { + bytesData, _ := json.Marshal(reqParams) + return bytes.NewReader(bytesData), contentType, nil + } + if files != nil { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + for _, uploadFile := range files { + file, err := os.Open(uploadFile.Filepath) + if err != nil { + return nil, "", err + } + part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath)) + if err != nil { + return nil, "", err + } + _, err = io.Copy(part, file) + defer file.Close() + } + for k, v := range reqParams { + if err := writer.WriteField(k, v); err != nil { + return nil, "", err + } + } + if err := writer.Close(); err != nil { + return nil, "", err + } + return body, writer.FormDataContentType(), nil + } + + urlValues := url.Values{} + for key, val := range reqParams { + urlValues.Set(key, val) + } + + reqBody := urlValues.Encode() + + return strings.NewReader(reqBody), contentType, nil Review comment: Put the `param` to the body? This confused me. ########## File path: api/test/e2enew/base/http.go ########## @@ -92,3 +104,44 @@ func BatchTestServerPort(times int) map[string]int { return res } + +func GetReader(reqParams map[string]string, contentType string, files []UploadFile) (io.Reader, string, error) { + if strings.Index(contentType, "json") > -1 { + bytesData, _ := json.Marshal(reqParams) + return bytes.NewReader(bytesData), contentType, nil + } + if files != nil { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + for _, uploadFile := range files { + file, err := os.Open(uploadFile.Filepath) + if err != nil { + return nil, "", err + } + part, err := writer.CreateFormFile(uploadFile.Name, filepath.Base(uploadFile.Filepath)) + if err != nil { + return nil, "", err + } + _, err = io.Copy(part, file) + defer file.Close() Review comment: `defer file.Close()` should be put when the file is opened. At line 121. Else if the writer.CreateFormFile's err is not nil, the file wouldn't be closed. This will cause `fd leak`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org