Skip to content

Commit

Permalink
fix: [#375] [Bug] using fiber, unable to handle files alongside other…
Browse files Browse the repository at this point in the history
… data with multipart formdata (#67)
  • Loading branch information
hwbrzzl authored Mar 18, 2024
1 parent 7a5e38d commit 29df926
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ func getPostData(ctx *Context) (map[string]any, error) {
contentType := ctx.instance.Get("Content-Type")
data := make(map[string]any)

if contentType == "application/json" {
if strings.Contains(contentType, "application/json") {
bodyBytes := ctx.instance.Body()

if err := json.Unmarshal(bodyBytes, &data); err != nil {
return nil, fmt.Errorf("decode json [%v] error: %v", string(bodyBytes), err)
}
}

if contentType == "multipart/form-data" {
if strings.Contains(contentType, "multipart/form-data") {
if form, err := ctx.instance.MultipartForm(); err == nil {
for k, v := range form.Value {
data[k] = strings.Join(v, ",")
Expand All @@ -454,7 +454,7 @@ func getPostData(ctx *Context) (map[string]any, error) {
}
}

if contentType == "application/x-www-form-urlencoded" {
if strings.Contains(contentType, "application/x-www-form-urlencoded") {
args := ctx.instance.Request().PostArgs()
args.VisitAll(func(key, value []byte) {
data[string(key)] = string(value)
Expand Down
20 changes: 16 additions & 4 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ func TestRequest(t *testing.T) {
mockStorage.On("Disk", "local").Return(mockDriver).Once()
frameworkfilesystem.StorageFacade = mockStorage

name := ctx.Request().Input("name")
fileInfo, err := ctx.Request().File("file")

mockDriver.On("PutFile", "test", fileInfo).Return("test/README.md", nil).Once()
Expand All @@ -947,10 +948,11 @@ func TestRequest(t *testing.T) {

return ctx.Response().Success().Json(contractshttp.Json{
"exist": mockStorage.Exists(filePath),
"extension": extension,
"file_path_length": len(filePath),
"hash_name_length": len(fileInfo.HashName()),
"hash_name_length1": len(fileInfo.HashName("test")),
"file_path_length": len(filePath),
"extension": extension,
"name": name,
"original_name": fileInfo.GetClientOriginalName(),
"original_extension": fileInfo.GetClientOriginalExtension(),
})
Expand All @@ -962,7 +964,13 @@ func TestRequest(t *testing.T) {
if err != nil {
return err
}

defer readme.Close()

if err := writer.WriteField("name", "goravel"); err != nil {
return err
}

part1, err := writer.CreateFormFile("file", filepath.Base("./README.md"))
if err != nil {
return err
Expand All @@ -976,13 +984,17 @@ func TestRequest(t *testing.T) {
return err
}

req, _ = http.NewRequest(method, url, payload)
req, err = http.NewRequest(method, url, payload)
if err != nil {
return err
}

req.Header.Set("Content-Type", writer.FormDataContentType())

return nil
},
expectCode: http.StatusOK,
expectBodyJson: "{\"exist\":true,\"extension\":\"txt\",\"file_path_length\":14,\"hash_name_length\":44,\"hash_name_length1\":49,\"original_extension\":\"md\",\"original_name\":\"README.md\"}",
expectBodyJson: "{\"exist\":true,\"extension\":\"txt\",\"file_path_length\":14,\"hash_name_length\":44,\"hash_name_length1\":49,\"name\":\"goravel\",\"original_extension\":\"md\",\"original_name\":\"README.md\"}",
},
{
name: "GET with params and validator, validate pass",
Expand Down

0 comments on commit 29df926

Please sign in to comment.