2020-09-13 11:20:11 +02:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-09-16 09:44:05 +08:00
|
|
|
"net/http/httptest"
|
2020-09-13 11:20:11 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2020-09-14 12:12:29 +02:00
|
|
|
"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",
|
|
|
|
}))
|
|
|
|
|
2021-05-12 07:58:01 +01:00
|
|
|
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",
|
|
|
|
},
|
2021-09-30 01:53:25 +08:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2021-05-12 07:58:01 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 09:44:05 +08:00
|
|
|
|
|
|
|
// go test -run Test_FileSystem_Next
|
|
|
|
func Test_FileSystem_Next(t *testing.T) {
|
2020-09-17 13:41:06 +08:00
|
|
|
app := fiber.New()
|
2020-09-16 09:44:05 +08:00
|
|
|
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)
|
|
|
|
}
|