diff --git a/filesystem/file.go b/filesystem/file.go index a95bf9c60..c9eb4880f 100644 --- a/filesystem/file.go +++ b/filesystem/file.go @@ -2,6 +2,7 @@ package filesystem import ( "errors" + "fmt" "io" "io/ioutil" "mime/multipart" @@ -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 } diff --git a/filesystem/file_test.go b/filesystem/file_test.go index 7e6bd09ad..56518b029 100644 --- a/filesystem/file_test.go +++ b/filesystem/file_test.go @@ -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" @@ -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) +} diff --git a/route/gin_test.go b/route/gin_test.go index 6a9c84690..b2d366ac5 100644 --- a/route/gin_test.go +++ b/route/gin_test.go @@ -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") @@ -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) + }) } } @@ -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) + }) } } diff --git a/support/constant.go b/support/constant.go index 289dfe28f..fb1835fc9 100644 --- a/support/constant.go +++ b/support/constant.go @@ -1,5 +1,5 @@ package support -const Version string = "1.9.0" +const Version string = "v1.9.1" var RootPath string