Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: static file routing path rewrite. #1538

Merged
merged 4 commits into from Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions app_test.go
Expand Up @@ -735,6 +735,15 @@ func Test_App_Static_Trailing_Slash(t *testing.T) {
req := httptest.NewRequest(MethodGet, "/john/", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
utils.AssertEqual(t, MIMETextHTMLCharsetUTF8, resp.Header.Get(HeaderContentType))

app.Static("/john_without_index", "./.github/testdata/fs/css")

req = httptest.NewRequest(MethodGet, "/john_without_index/", nil)
resp, err = app.Test(req)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
utils.AssertEqual(t, MIMETextPlainCharsetUTF8, resp.Header.Get(HeaderContentType))
Expand Down
7 changes: 5 additions & 2 deletions router.go
Expand Up @@ -327,8 +327,11 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
if len(path) >= prefixLen {
if isStar && app.getString(path[0:prefixLen]) == prefix {
path = append(path[0:0], '/')
} else if len(path) > 0 && path[len(path)-1] != '/' {
path = append(path[prefixLen:], '/')
} else {
path = path[prefixLen:]
if len(path) == 0 || path[len(path)-1] != '/' {
path = append(path, '/')
}
Comment on lines +331 to +334
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go test -v -run=^Test_Route_Static
=== RUN   Test_Route_Static_Root
--- PASS: Test_Route_Static_Root (0.01s)
=== RUN   Test_Route_Static_HasPrefix
--- PASS: Test_Route_Static_HasPrefix (0.00s)
PASS
ok      github.com/gofiber/fiber/v2     0.077s

}
}
if len(path) > 0 && path[0] != '/' {
Expand Down
79 changes: 79 additions & 0 deletions router_test.go
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"

"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -323,6 +324,84 @@ func Test_Router_Handler_Catch_Error(t *testing.T) {
utils.AssertEqual(t, StatusInternalServerError, c.Response.Header.StatusCode())
}

func Test_Route_Static_Root(t *testing.T) {
dir := "./.github/testdata/fs/css"
app := New()
app.Static("/", dir, Static{
Browse: true,
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))

app = New()
app.Static("/", dir)

resp, err = app.Test(httptest.NewRequest(MethodGet, "/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
}

func Test_Route_Static_HasPrefix(t *testing.T) {
dir := "./.github/testdata/fs/css"
app := New()
app.Static("/static", dir, Static{
Browse: true,
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "/static", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/static/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/static/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))

app = New()
app.Static("/static", dir)

resp, err = app.Test(httptest.NewRequest(MethodGet, "/static", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/static/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")

resp, err = app.Test(httptest.NewRequest(MethodGet, "/static/style.css", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")

body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
}

//////////////////////////////////////////////
///////////////// BENCHMARKS /////////////////
//////////////////////////////////////////////
Expand Down