Skip to content

Commit

Permalink
Restore original URL after sending file (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvc committed Oct 1, 2021
1 parent a7d5f2b commit 26c29e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ctx.go
Expand Up @@ -1074,6 +1074,9 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
file += "/"
}
}
// Restore the original requested URL
originalURL := c.OriginalURL()
defer c.fasthttp.Request.SetRequestURI(originalURL)
// Set new URI for fileHandler
c.fasthttp.Request.SetRequestURI(file)
// Save status code
Expand Down
15 changes: 15 additions & 0 deletions ctx_test.go
Expand Up @@ -1656,6 +1656,21 @@ func Test_Ctx_SendFile_Immutable(t *testing.T) {
utils.AssertEqual(t, StatusOK, resp.StatusCode)
}

// go test -race -run Test_Ctx_SendFile_RestoreOriginalURL
func Test_Ctx_SendFile_RestoreOriginalURL(t *testing.T) {
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) error {
originalURL := c.OriginalURL()
err := c.SendFile("ctx.go")
utils.AssertEqual(t, originalURL, c.OriginalURL())
return err
})

_, err := app.Test(httptest.NewRequest("GET", "/?test=true", nil))
utils.AssertEqual(t, nil, err)
}

// go test -run Test_Ctx_JSON
func Test_Ctx_JSON(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 26c29e2

Please sign in to comment.