-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 server in sub app with mount (#3104) #3132
Open
yinheli
wants to merge
1
commit into
gofiber:v2
Choose a base branch
from
yinheli:bugfix/static-server-in-sub-app
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
// 📃 Github Repository: https://github.com/gofiber/fiber | ||
// 📌 API Documentation: https://docs.gofiber.io | ||
|
||
//nolint:bodyclose // Much easier to just ignore memory leaks in tests | ||
//nolint:bodyclose,goconst // Much easier to just ignore memory leaks in tests and constant variables in tests | ||
package fiber | ||
|
||
import ( | ||
|
@@ -471,6 +471,53 @@ func Test_Route_Static_HasPrefix(t *testing.T) { | |
body, err = io.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("/css", dir) | ||
|
||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/css/style.css", nil)) | ||
utils.AssertEqual(t, nil, err, "app.Test(req)") | ||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code") | ||
|
||
body, err = io.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_SubApp(t *testing.T) { | ||
t.Parallel() | ||
|
||
dir := "./.github/testdata/fs/css" | ||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
app := New() | ||
|
||
// subapp | ||
subApp := New() | ||
subApp.Static("/css", dir) | ||
app.Mount("/sub", subApp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a testcase for nested mounting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested mounting is not working. I'll try to fix it. |
||
|
||
// nested subapp | ||
nestApp := New() | ||
nestApp.Static("/css", dir) | ||
subApp.Mount("/nest", nestApp) | ||
|
||
// test subapp | ||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/sub/css/style.css", nil)) | ||
utils.AssertEqual(t, nil, err, "app.Test(req)") | ||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code") | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
utils.AssertEqual(t, nil, err, "app.Test(req)") | ||
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color")) | ||
|
||
// test nested subapp | ||
|
||
resp, err = app.Test(httptest.NewRequest(MethodGet, "/sub/nest/css/style.css", nil)) | ||
utils.AssertEqual(t, nil, err, "app.Test(req)") | ||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code") | ||
|
||
body, err = io.ReadAll(resp.Body) | ||
utils.AssertEqual(t, nil, err, "app.Test(req)") | ||
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color")) | ||
} | ||
|
||
func Test_Router_NotFound(t *testing.T) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ReneWerner87 do you think we should use full-mount path for mounting? I think this might be a bug related to MountPath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MountPath can only see that of subApp. Currently, the app cannot know how the upper layer is mounted. So I introduce a reference to Parent here. Do you have any good suggestions?
My current thinking is that when PathRewrite in registerStatic can replace the mounted part of the path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@efectn @yinheli yes it should have something to do with the path
don't know if we necessarily need the complete parent reference for this
in the bug you said that the path is public/public, then we should start there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, perhaps we can do it during the path setting https://github.com/gofiber/fiber/blob/main/mount.go#L52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have investigated this. However, here only the path of the subapp can be obtained. And
registerStatic
initializes the fileHandler when registering. So I chose the current implementation and added FullMountPath. I think that is easy to implement and has a lower cost.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok i will check it tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the problem occur with v3 @yinheli ? I've just given a try with v3, and seems to work well:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't investigated yet. I'm a bit busy during this period.