Skip to content

Commit 506a800

Browse files
committed
🩹 Fix: static server in sub app with mount (#3104)
1 parent 6e74114 commit 506a800

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

router.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fiber
66

77
import (
8+
"bytes"
89
"fmt"
910
"html"
1011
"sort"
@@ -357,6 +358,12 @@ func (app *App) registerStatic(prefix, root string, config ...Static) {
357358
IndexNames: []string{"index.html"},
358359
PathRewrite: func(fctx *fasthttp.RequestCtx) []byte {
359360
path := fctx.Path()
361+
mountPath := app.MountPath()
362+
if n := len(mountPath); n > 0 {
363+
if bytes.Equal(path[:n], utils.UnsafeBytes(mountPath)) {
364+
path = path[n:]
365+
}
366+
}
360367
if len(path) >= prefixLen {
361368
if isStar && app.getString(path[0:prefixLen]) == prefix {
362369
path = append(path[0:0], '/')

router_test.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func Test_Router_Handler_Catch_Error(t *testing.T) {
354354
func Test_Route_Static_Root(t *testing.T) {
355355
t.Parallel()
356356

357-
dir := "./.github/testdata/fs/css"
357+
dir := "./.github/testdata/fs/css" //nolint:goconst // ignore lint
358358
app := New()
359359
app.Static("/", dir, Static{
360360
Browse: true,
@@ -471,6 +471,37 @@ func Test_Route_Static_HasPrefix(t *testing.T) {
471471
body, err = io.ReadAll(resp.Body)
472472
utils.AssertEqual(t, nil, err, "app.Test(req)")
473473
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
474+
475+
app = New()
476+
app.Static("/css", dir)
477+
478+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/css/style.css", nil))
479+
utils.AssertEqual(t, nil, err, "app.Test(req)")
480+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
481+
482+
body, err = io.ReadAll(resp.Body)
483+
utils.AssertEqual(t, nil, err, "app.Test(req)")
484+
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
485+
}
486+
487+
func Test_Route_Static_SubApp(t *testing.T) {
488+
t.Parallel()
489+
490+
dir := "./.github/testdata/fs/css"
491+
app := New()
492+
493+
subApp := New()
494+
subApp.Static("/css", dir)
495+
496+
app.Mount("/sub", subApp)
497+
498+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/sub/css/style.css", nil))
499+
utils.AssertEqual(t, nil, err, "app.Test(req)")
500+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
501+
502+
body, err := io.ReadAll(resp.Body)
503+
utils.AssertEqual(t, nil, err, "app.Test(req)")
504+
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
474505
}
475506

476507
func Test_Router_NotFound(t *testing.T) {

0 commit comments

Comments
 (0)