mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-11 07:58:14 +08:00
Compare commits
4 Commits
348db71a60
...
4ec5e481ea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ec5e481ea | ||
|
|
d9e5cdf9c6 | ||
|
|
53410d2e07 | ||
|
|
deb81c1d09 |
10
context.go
10
context.go
@ -386,6 +386,11 @@ func (c *Context) GetDuration(key any) time.Duration {
|
||||
return getTyped[time.Duration](c, key)
|
||||
}
|
||||
|
||||
// GetError returns the value associated with the key as an error.
|
||||
func (c *Context) GetError(key any) error {
|
||||
return getTyped[error](c, key)
|
||||
}
|
||||
|
||||
// GetIntSlice returns the value associated with the key as a slice of integers.
|
||||
func (c *Context) GetIntSlice(key any) []int {
|
||||
return getTyped[[]int](c, key)
|
||||
@ -451,6 +456,11 @@ func (c *Context) GetStringSlice(key any) []string {
|
||||
return getTyped[[]string](c, key)
|
||||
}
|
||||
|
||||
// GetErrorSlice returns the value associated with the key as a slice of errors.
|
||||
func (c *Context) GetErrorSlice(key any) []error {
|
||||
return getTyped[[]error](c, key)
|
||||
}
|
||||
|
||||
// GetStringMap returns the value associated with the key as a map of interfaces.
|
||||
func (c *Context) GetStringMap(key any) map[string]any {
|
||||
return getTyped[map[string]any](c, key)
|
||||
|
||||
@ -516,6 +516,14 @@ func TestContextGetDuration(t *testing.T) {
|
||||
assert.Equal(t, time.Second, c.GetDuration("duration"))
|
||||
}
|
||||
|
||||
func TestContextGetError(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
key := "error"
|
||||
value := errors.New("test error")
|
||||
c.Set(key, value)
|
||||
assert.Equal(t, value, c.GetError(key))
|
||||
}
|
||||
|
||||
func TestContextGetIntSlice(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
key := "int-slice"
|
||||
@ -618,6 +626,14 @@ func TestContextGetStringSlice(t *testing.T) {
|
||||
assert.Equal(t, []string{"foo"}, c.GetStringSlice("slice"))
|
||||
}
|
||||
|
||||
func TestContextGetErrorSlice(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
key := "error-slice"
|
||||
value := []error{errors.New("error1"), errors.New("error2")}
|
||||
c.Set(key, value)
|
||||
assert.Equal(t, value, c.GetErrorSlice(key))
|
||||
}
|
||||
|
||||
func TestContextGetStringMap(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
m := make(map[string]any)
|
||||
|
||||
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.19.0
|
||||
github.com/goccy/go-yaml v1.19.1
|
||||
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.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE=
|
||||
github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/goccy/go-yaml v1.19.1 h1:3rG3+v8pkhRqoQ/88NYNMHYVGYztCOCIZ7UQhu7H+NE=
|
||||
github.com/goccy/go-yaml v1.19.1/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=
|
||||
|
||||
2
tree.go
2
tree.go
@ -823,7 +823,7 @@ walk: // Outer loop for walking the tree
|
||||
return nil
|
||||
}
|
||||
|
||||
n = n.children[0]
|
||||
n = n.children[len(n.children)-1]
|
||||
switch n.nType {
|
||||
case param:
|
||||
// Find param end (either '/' or path end)
|
||||
|
||||
@ -751,6 +751,8 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
|
||||
"/w/𠜎", // 4 byte
|
||||
"/w/𠜏/", // 4 byte
|
||||
longPath,
|
||||
"/param/same/:id",
|
||||
"/param/same/1",
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
@ -844,6 +846,7 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
|
||||
{"/w/𠜎/", "/w/𠜎", true, true},
|
||||
{"/w/𠜏", "/w/𠜏/", true, true},
|
||||
{lOngPath, longPath, true, true},
|
||||
{"/param/same/prefix/noexist", "", false, false},
|
||||
}
|
||||
// With fixTrailingSlash = true
|
||||
for _, test := range tests {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user