mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-06 10:21:17 +08:00
Compare commits
5 Commits
fcd4da8cf9
...
383ddedb3e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
383ddedb3e | ||
|
|
fad706f121 | ||
|
|
7dbe3741fc | ||
|
|
8ef5a66294 | ||
|
|
670895b7b2 |
2
go.mod
2
go.mod
@ -7,7 +7,7 @@ require (
|
||||
github.com/gin-contrib/sse v1.1.0
|
||||
github.com/go-playground/validator/v10 v10.28.0
|
||||
github.com/goccy/go-json v0.10.2
|
||||
github.com/goccy/go-yaml v1.18.0
|
||||
github.com/goccy/go-yaml v1.19.0
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/mattn/go-isatty v0.0.20
|
||||
github.com/modern-go/reflect2 v1.0.2
|
||||
|
||||
4
go.sum
4
go.sum
@ -24,8 +24,8 @@ github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0
|
||||
github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/goccy/go-yaml v1.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE=
|
||||
github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
|
||||
@ -223,6 +223,10 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
|
||||
}
|
||||
|
||||
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
|
||||
f, err := fs.Open(file)
|
||||
if err != nil {
|
||||
|
||||
@ -5,7 +5,9 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -646,6 +648,43 @@ func TestRouterStaticFSNotFound(t *testing.T) {
|
||||
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) {
|
||||
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