1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 11:03:48 +00:00
fiber/middleware/filesystem/filesystem_test.go

207 lines
4.8 KiB
Go
Raw Normal View History

2020-09-13 11:20:11 +02:00
package filesystem
import (
"net/http"
"net/http/httptest"
2020-09-13 11:20:11 +02:00
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
2020-09-13 11:20:11 +02:00
)
// go test -run Test_FileSystem
func Test_FileSystem(t *testing.T) {
app := fiber.New()
app.Use("/test", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
}))
app.Use("/dir", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
Browse: true,
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Use("/spatest", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
2020-09-17 12:56:11 +08:00
Index: "index.html",
2020-09-13 11:20:11 +02:00
NotFoundFile: "index.html",
}))
app.Use("/prefix", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
PathPrefix: "img",
}))
2020-09-13 11:20:11 +02:00
tests := []struct {
name string
url string
statusCode int
contentType string
modifiedTime string
}{
{
name: "Should be returns status 200 with suitable content-type",
url: "/test/index.html",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200 with suitable content-type",
url: "/test",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should be returns status 200 with suitable content-type",
url: "/test/css/style.css",
statusCode: 200,
contentType: "text/css",
},
{
name: "Should be returns status 404",
url: "/test/nofile.js",
statusCode: 404,
},
{
name: "Should be returns status 404",
url: "/test/nofile",
statusCode: 404,
},
{
name: "Should be returns status 200",
url: "/",
statusCode: 200,
contentType: "text/plain; charset=utf-8",
},
{
name: "Should be returns status 403",
url: "/test/img",
statusCode: 403,
},
{
name: "Should list the directory contents",
url: "/dir/img",
statusCode: 200,
contentType: "text/html",
},
{
name: "Should list the directory contents",
url: "/dir/img/",
statusCode: 200,
contentType: "text/html",
},
2020-09-13 11:20:11 +02:00
{
name: "Should be returns status 200",
url: "/dir/img/fiber.png",
statusCode: 200,
contentType: "image/png",
},
{
name: "Should be return status 200",
url: "/spatest/doesnotexist",
statusCode: 200,
contentType: "text/html",
},
{
name: "PathPrefix should be applied",
url: "/prefix/fiber.png",
statusCode: 200,
contentType: "image/png",
},
2020-09-13 11:20:11 +02:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2020-09-17 12:56:11 +08:00
resp, err := app.Test(httptest.NewRequest("GET", tt.url, nil))
2020-09-13 11:20:11 +02:00
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, tt.statusCode, resp.StatusCode)
if tt.contentType != "" {
ct := resp.Header.Get("Content-Type")
utils.AssertEqual(t, tt.contentType, ct)
}
})
}
}
// go test -run Test_FileSystem_Next
func Test_FileSystem_Next(t *testing.T) {
app := fiber.New()
app.Use(New(Config{
Root: http.Dir("../../.github/testdata/fs"),
Next: func(_ *fiber.Ctx) bool {
return true
},
}))
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
}
2020-09-17 12:56:11 +08:00
func Test_FileSystem_NonGetAndHead(t *testing.T) {
app := fiber.New()
app.Use("/test", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
}))
resp, err := app.Test(httptest.NewRequest(fiber.MethodPost, "/test", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}
func Test_FileSystem_Head(t *testing.T) {
app := fiber.New()
app.Use("/test", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
}))
req, _ := http.NewRequest(fiber.MethodHead, "/test", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}
func Test_FileSystem_NoRoot(t *testing.T) {
defer func() {
utils.AssertEqual(t, "filesystem: Root cannot be nil", recover())
}()
app := fiber.New()
app.Use(New())
_, _ = app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
}
2020-10-28 11:44:14 -03:00
func Test_FileSystem_UsingParam(t *testing.T) {
app := fiber.New()
app.Use("/:path", func(c *fiber.Ctx) error {
return SendFile(c, http.Dir("../../.github/testdata/fs"), c.Params("path")+".html")
})
req, _ := http.NewRequest(fiber.MethodHead, "/index", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}
func Test_FileSystem_UsingParam_NonFile(t *testing.T) {
app := fiber.New()
app.Use("/:path", func(c *fiber.Ctx) error {
return SendFile(c, http.Dir("../../.github/testdata/fs"), c.Params("path")+".html")
})
req, _ := http.NewRequest(fiber.MethodHead, "/template", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}