mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-13 13:12:17 +08:00
Merge 7dbe3741fc111e3cf0daa26c2190d006052321cb into 19b877fa50cbbb9282763099fb177a1e5cc5c850
This commit is contained in:
commit
f271fd835b
@ -223,6 +223,10 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
|||||||
}
|
}
|
||||||
|
|
||||||
file := c.Param("filepath")
|
file := c.Param("filepath")
|
||||||
|
file = path.Clean("/" + file)[1:]
|
||||||
|
if file == "" {
|
||||||
|
file = "."
|
||||||
|
}
|
||||||
// Check if file exists and/or if we have permission to access it
|
// Check if file exists and/or if we have permission to access it
|
||||||
f, err := fs.Open(file)
|
f, err := fs.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -5,7 +5,9 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
@ -646,6 +648,43 @@ func TestRouterStaticFSNotFound(t *testing.T) {
|
|||||||
assert.Equal(t, "non existent", w.Body.String())
|
assert.Equal(t, "non existent", w.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed testdata/embed
|
||||||
|
var embeddedFolder embed.FS
|
||||||
|
|
||||||
|
const embeddedPath = "testdata/embed"
|
||||||
|
|
||||||
|
func TestRouteStaticFSCleansPath(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
subFS, err := fs.Sub(embeddedFolder, embeddedPath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
fs := &OnlyFilesFS{
|
||||||
|
FileSystem: http.FS(subFS),
|
||||||
|
}
|
||||||
|
router.StaticFS("/", fs)
|
||||||
|
router.NoRoute(func(c *Context) {
|
||||||
|
c.String(http.StatusNotFound, "non existent")
|
||||||
|
})
|
||||||
|
|
||||||
|
w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/")
|
||||||
|
assert.Contains(t, w.Body.String(), "This is another simple embedded page.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRouteStaticFSNestedJsFile(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
subFS, err := fs.Sub(embeddedFolder, embeddedPath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
fs := &OnlyFilesFS{
|
||||||
|
FileSystem: http.FS(subFS),
|
||||||
|
}
|
||||||
|
router.StaticFS("/", fs)
|
||||||
|
router.NoRoute(func(c *Context) {
|
||||||
|
c.String(http.StatusNotFound, "non existent")
|
||||||
|
})
|
||||||
|
|
||||||
|
w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/main.js")
|
||||||
|
assert.Contains(t, w.Body.String(), "console.log(\"This is a simple embedded JavaScript file.\");")
|
||||||
|
}
|
||||||
|
|
||||||
func TestRouterStaticFSFileNotFound(t *testing.T) {
|
func TestRouterStaticFSFileNotFound(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
|
|
||||||
|
|||||||
2
testdata/embed/index.html
vendored
Normal file
2
testdata/embed/index.html
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
Hello embedded world!
|
||||||
2
testdata/embed/tutorials/making-gin/index.html
vendored
Normal file
2
testdata/embed/tutorials/making-gin/index.html
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
This is another simple embedded page.
|
||||||
1
testdata/embed/tutorials/making-gin/main.js
vendored
Normal file
1
testdata/embed/tutorials/making-gin/main.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
console.log("This is a simple embedded JavaScript file.");
|
||||||
Loading…
x
Reference in New Issue
Block a user