@@ -3,6 +3,7 @@ package v3_test
33import (
44 "bytes"
55 "os"
6+ "strings"
67 "testing"
78
89 "github.com/stretchr/testify/assert"
@@ -40,3 +41,79 @@ func main() {
4041 assert .Contains (t , content , `IndexNames: []string{"index.html"}` )
4142 assert .Contains (t , buf .String (), "Migrating filesystem middleware" )
4243}
44+
45+ func Test_MigrateFilesystemMiddleware_WithNotFoundFile (t * testing.T ) {
46+ t .Parallel ()
47+
48+ dir , err := os .MkdirTemp ("" , "mfs-notfound" )
49+ require .NoError (t , err )
50+ defer func () { require .NoError (t , os .RemoveAll (dir )) }()
51+
52+ file := writeTempFile (t , dir , `package main
53+ import (
54+ "github.com/gofiber/fiber/v2/middleware/filesystem"
55+ "net/http"
56+ )
57+ func main() {
58+ app.All("/*", filesystem.New(filesystem.Config{
59+ Root: http.Dir("./dist"),
60+ NotFoundFile: "index.html",
61+ Index: "index.html",
62+ }))
63+ }` )
64+
65+ var buf bytes.Buffer
66+ cmd := newCmd (& buf )
67+ require .NoError (t , v3 .MigrateFilesystemMiddleware (cmd , dir , nil , nil ))
68+
69+ content := readFile (t , file )
70+ // Check that NotFoundFile is commented out
71+ assert .Contains (t , content , "// NotFoundFile: \" index.html\" " )
72+ // Check that TODO comment is added
73+ assert .Contains (t , content , "// TODO: Migrate to NotFoundHandler (fiber.Handler) - NotFoundFile is deprecated" )
74+ // Check that static migration happened
75+ assert .Contains (t , content , `static.New("", static.Config{` )
76+ assert .Contains (t , content , `FS: os.DirFS("./dist")` )
77+ assert .Contains (t , content , `IndexNames: []string{"index.html"}` )
78+ }
79+
80+ func Test_MigrateFilesystemMiddleware_NotFoundFileIdempotent (t * testing.T ) {
81+ t .Parallel ()
82+
83+ dir , err := os .MkdirTemp ("" , "mfs-idempotent" )
84+ require .NoError (t , err )
85+ defer func () { require .NoError (t , os .RemoveAll (dir )) }()
86+
87+ file := writeTempFile (t , dir , `package main
88+ import (
89+ "github.com/gofiber/fiber/v2/middleware/filesystem"
90+ "net/http"
91+ )
92+ func main() {
93+ app.All("/*", filesystem.New(filesystem.Config{
94+ Root: http.Dir("./dist"),
95+ NotFoundFile: "index.html",
96+ Index: "index.html",
97+ }))
98+ }` )
99+
100+ var buf bytes.Buffer
101+ cmd := newCmd (& buf )
102+
103+ // First migration
104+ require .NoError (t , v3 .MigrateFilesystemMiddleware (cmd , dir , nil , nil ))
105+ firstContent := readFile (t , file )
106+
107+ // Second migration - should be idempotent
108+ buf .Reset ()
109+ require .NoError (t , v3 .MigrateFilesystemMiddleware (cmd , dir , nil , nil ))
110+ secondContent := readFile (t , file )
111+
112+ // Content should be exactly the same after second migration
113+ assert .Equal (t , firstContent , secondContent , "Migration should be idempotent" )
114+
115+ // Verify the TODO comment is only present once
116+ assert .Equal (t , 1 , strings .Count (secondContent , "TODO: Migrate to NotFoundHandler" ))
117+ // Verify the NotFoundFile comment is only present once
118+ assert .Equal (t , 1 , strings .Count (secondContent , "// NotFoundFile:" ))
119+ }
0 commit comments