mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-12 05:51:12 +08:00
Compare commits
6 Commits
383ddedb3e
...
08b95dd0c0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08b95dd0c0 | ||
|
|
2a794cd0b0 | ||
|
|
b917b14ff9 | ||
|
|
7dbe3741fc | ||
|
|
8ef5a66294 | ||
|
|
670895b7b2 |
@ -300,6 +300,11 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
func setWithProperType(val string, value reflect.Value, field reflect.StructField) error {
|
||||
// If it is a string type, no spaces are removed, and the user data is not modified here
|
||||
if value.Kind() != reflect.String {
|
||||
val = strings.TrimSpace(val)
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Int:
|
||||
return setIntField(val, 0, value)
|
||||
@ -404,6 +409,11 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
||||
timeFormat = time.RFC3339
|
||||
}
|
||||
|
||||
if val == "" {
|
||||
value.Set(reflect.ValueOf(time.Time{}))
|
||||
return nil
|
||||
}
|
||||
|
||||
switch tf := strings.ToLower(timeFormat); tf {
|
||||
case "unix", "unixmilli", "unixmicro", "unixnano":
|
||||
tv, err := strconv.ParseInt(val, 10, 64)
|
||||
@ -427,11 +437,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
||||
return nil
|
||||
}
|
||||
|
||||
if val == "" {
|
||||
value.Set(reflect.ValueOf(time.Time{}))
|
||||
return nil
|
||||
}
|
||||
|
||||
l := time.Local
|
||||
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
|
||||
l = time.UTC
|
||||
@ -475,6 +480,10 @@ func setSlice(vals []string, value reflect.Value, field reflect.StructField) err
|
||||
}
|
||||
|
||||
func setTimeDuration(val string, value reflect.Value) error {
|
||||
if val == "" {
|
||||
val = "0"
|
||||
}
|
||||
|
||||
d, err := time.ParseDuration(val)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -226,7 +226,35 @@ func TestMappingTime(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
type bindTestData struct {
|
||||
need any
|
||||
got any
|
||||
in map[string][]string
|
||||
}
|
||||
|
||||
func TestMappingTimeUnixNano(t *testing.T) {
|
||||
type needFixUnixNanoEmpty struct {
|
||||
CreateTime time.Time `form:"createTime" time_format:"unixNano"`
|
||||
}
|
||||
|
||||
// ok
|
||||
tests := []bindTestData{
|
||||
{need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{" "}}},
|
||||
{need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{}}},
|
||||
}
|
||||
|
||||
for _, v := range tests {
|
||||
err := mapForm(v.got, v.in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, v.need, v.got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingTimeDuration(t *testing.T) {
|
||||
type needFixDurationEmpty struct {
|
||||
Duration time.Duration `form:"duration"`
|
||||
}
|
||||
|
||||
var s struct {
|
||||
D time.Duration
|
||||
}
|
||||
@ -236,6 +264,17 @@ func TestMappingTimeDuration(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 5*time.Second, s.D)
|
||||
|
||||
// ok
|
||||
tests := []bindTestData{
|
||||
{need: &needFixDurationEmpty{}, got: &needFixDurationEmpty{}, in: formSource{"duration": []string{" "}}},
|
||||
{need: &needFixDurationEmpty{}, got: &needFixDurationEmpty{}, in: formSource{"duration": []string{}}},
|
||||
}
|
||||
|
||||
for _, v := range tests {
|
||||
err := mapForm(v.got, v.in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, v.need, v.got)
|
||||
}
|
||||
// error
|
||||
err = mappingByPtr(&s, formSource{"D": {"wrong"}}, "form")
|
||||
require.Error(t, err)
|
||||
|
||||
2
debug.go
2
debug.go
@ -13,7 +13,7 @@ import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const ginSupportMinGoVer = 23
|
||||
const ginSupportMinGoVer = 24
|
||||
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
|
||||
@ -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