Skip to content

Commit

Permalink
Fix: static file routing path rewrite. (#1538)
Browse files Browse the repository at this point in the history
* Fix: static file routing path rewrite.

* Add: static file routing test cases.

* Update: change os.CreateTemp to ioutil.TempFile for go1.14

* Update: optimize test cases.
  • Loading branch information
fufuok committed Oct 1, 2021
1 parent 35e38db commit b94870d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
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, '/')
}
}
}
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

0 comments on commit b94870d

Please sign in to comment.