You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/02/23 07:36:23 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #1465: feat(be): Online debug function adds support for transfer files

nic-chen commented on a change in pull request #1465:
URL: https://github.com/apache/apisix-dashboard/pull/1465#discussion_r580819191



##########
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:
       I feel that there is a problem with this. The content of the file should not be read in the test case, but the content of the uploaded file should be read in the main program.




----------------------------------------------------------------
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:
users@infra.apache.org