Skip to content

Commit

Permalink
Fix file suffix (#59)
Browse files Browse the repository at this point in the history
* Fix fix suffix

* Update version

* Optimize unit test
  • Loading branch information
hwbrzzl authored Feb 26, 2023
1 parent bde8fe6 commit 0c52ca7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
4 changes: 3 additions & 1 deletion filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filesystem

import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
Expand Down Expand Up @@ -38,7 +39,8 @@ func NewFileFromRequest(fileHeader *multipart.FileHeader) (*File, error) {
}
defer src.Close()

tempFile, err := ioutil.TempFile(os.TempDir(), "goravel-")
tempFileName := fmt.Sprintf("%s_*%s", facades.Config.GetString("app.name"), path.Ext(fileHeader.Filename))
tempFile, err := ioutil.TempFile(os.TempDir(), tempFileName)
if err != nil {
return nil, err
}
Expand Down
33 changes: 33 additions & 0 deletions filesystem/file_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package filesystem

import (
"bytes"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"path"
"testing"

"github.com/gin-gonic/gin"

"github.com/goravel/framework/testing/mock"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -55,3 +63,28 @@ func (s *FileTestSuite) TestExtension() {
s.Empty(extension)
s.EqualError(err, "unknown file extension")
}

func TestNewFileFromRequest(t *testing.T) {
mockConfig := mock.Config()
mockConfig.On("GetString", "app.name").Return("goravel").Once()
mockConfig.On("GetString", "filesystems.default").Return("local").Once()

buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
w, err := mw.CreateFormFile("file", "test.txt")
if assert.NoError(t, err) {
_, err = w.Write([]byte("test"))
assert.NoError(t, err)
}
mw.Close()
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
file, err := NewFileFromRequest(f)
fmt.Println(file)
assert.Nil(t, err)
assert.Equal(t, ".txt", path.Ext(file.file))

mockConfig.AssertExpectations(t)
}
55 changes: 30 additions & 25 deletions route/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func TestGinRequest(t *testing.T) {
url: "/file",
setup: func(method, url string) error {
gin.Post("/file", func(ctx httpcontract.Context) {
mockConfig.On("GetString", "app.name").Return("goravel").Once()
mockConfig.On("GetString", "filesystems.default").Return("local").Once()

fileInfo, err := ctx.Request().File("file")
Expand Down Expand Up @@ -653,17 +654,19 @@ func TestGinRequest(t *testing.T) {
}

for _, test := range tests {
beforeEach()
err := test.setup(test.method, test.url)
assert.Nil(t, err)

w := httptest.NewRecorder()
gin.ServeHTTP(w, req)

if test.expectBody != "" {
assert.Equal(t, test.expectBody, w.Body.String(), test.name)
}
assert.Equal(t, test.expectCode, w.Code, test.name)
t.Run(test.name, func(t *testing.T) {
beforeEach()
err := test.setup(test.method, test.url)
assert.Nil(t, err)

w := httptest.NewRecorder()
gin.ServeHTTP(w, req)

if test.expectBody != "" {
assert.Equal(t, test.expectBody, w.Body.String(), test.name)
}
assert.Equal(t, test.expectCode, w.Code, test.name)
})
}
}

Expand Down Expand Up @@ -923,20 +926,22 @@ func TestGinResponse(t *testing.T) {
}

for _, test := range tests {
beforeEach()
err := test.setup(test.method, test.url)
assert.Nil(t, err)

w := httptest.NewRecorder()
gin.ServeHTTP(w, req)

if test.expectBody != "" {
assert.Equal(t, test.expectBody, w.Body.String(), test.name)
}
if test.expectHeader != "" {
assert.Equal(t, test.expectHeader, strings.Join(w.Header().Values("Hello"), ""), test.name)
}
assert.Equal(t, test.expectCode, w.Code, test.name)
t.Run(test.name, func(t *testing.T) {
beforeEach()
err := test.setup(test.method, test.url)
assert.Nil(t, err)

w := httptest.NewRecorder()
gin.ServeHTTP(w, req)

if test.expectBody != "" {
assert.Equal(t, test.expectBody, w.Body.String(), test.name)
}
if test.expectHeader != "" {
assert.Equal(t, test.expectHeader, strings.Join(w.Header().Values("Hello"), ""), test.name)
}
assert.Equal(t, test.expectCode, w.Code, test.name)
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion support/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package support

const Version string = "1.9.0"
const Version string = "v1.9.1"

var RootPath string

0 comments on commit 0c52ca7

Please sign in to comment.