-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
7,648 additions
and
29 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package gctx | ||
|
||
import ( | ||
"github.com/ecodeclub/ekit" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Context struct { | ||
*gin.Context | ||
} | ||
|
||
func (c *Context) Param(key string) ekit.AnyValue { | ||
return ekit.AnyValue{ | ||
Val: c.Context.Param(key), | ||
} | ||
} | ||
|
||
func (c *Context) Query(key string) ekit.AnyValue { | ||
return ekit.AnyValue{ | ||
Val: c.Context.Query(key), | ||
} | ||
} | ||
|
||
func (c *Context) Cookie(key string) ekit.AnyValue { | ||
val, err := c.Context.Cookie(key) | ||
return ekit.AnyValue{ | ||
Val: val, | ||
Err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package gctx | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContext_Query(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
req func(t *testing.T) *http.Request | ||
key string | ||
wantErr error | ||
wantVal any | ||
}{ | ||
{ | ||
name: "获得数据", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, "http://localhost/abc?name=123&age=18", nil) | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "name", | ||
wantVal: "123", | ||
}, | ||
{ | ||
name: "没有数据", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, "http://localhost/abc?name=123&age=18", nil) | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "nickname", | ||
wantVal: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := &Context{Context: &gin.Context{ | ||
Request: tc.req(t), | ||
}} | ||
name := ctx.Query(tc.key) | ||
val, err := name.String() | ||
assert.Equal(t, tc.wantErr, err) | ||
assert.Equal(t, tc.wantVal, val) | ||
}) | ||
} | ||
} | ||
|
||
func TestContext_Param(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
req func(t *testing.T) *http.Request | ||
key string | ||
wantErr error | ||
wantVal any | ||
}{ | ||
{ | ||
name: "获得数据", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, "http://localhost/hello?name=123&age=18", nil) | ||
req.Form = url.Values{} | ||
req.Form.Set("name", "world") | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "name", | ||
wantVal: "world", | ||
}, | ||
{ | ||
name: "没有数据", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodPost, "http://localhost/hello?name=123&age=18", nil) | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "nickname", | ||
wantVal: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := gin.Default() | ||
server.POST("/hello", func(context *gin.Context) { | ||
ctx := &Context{Context: context} | ||
name := ctx.Param(tc.key) | ||
val, err := name.String() | ||
assert.Equal(t, tc.wantErr, err) | ||
assert.Equal(t, tc.wantVal, val) | ||
}) | ||
recorder := httptest.NewRecorder() | ||
server.ServeHTTP(recorder, tc.req(t)) | ||
}) | ||
} | ||
} | ||
|
||
func TestContext_Cookie(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
req func(t *testing.T) *http.Request | ||
key string | ||
wantErr error | ||
wantVal any | ||
}{ | ||
{ | ||
name: "有cookie", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodGet, "http://localhost/hello?name=123&age=18", nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: "name", | ||
Value: "world", | ||
}) | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "name", | ||
wantVal: "world", | ||
}, | ||
{ | ||
name: "没有 cookie", | ||
req: func(t *testing.T) *http.Request { | ||
req, err := http.NewRequest(http.MethodPost, "http://localhost/hello?name=123&age=18", nil) | ||
require.NoError(t, err) | ||
return req | ||
}, | ||
key: "nickname", | ||
wantVal: "", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := gin.Default() | ||
server.POST("/hello", func(context *gin.Context) { | ||
ctx := &Context{Context: context} | ||
name := ctx.Param(tc.key) | ||
val, err := name.String() | ||
assert.Equal(t, tc.wantErr, err) | ||
assert.Equal(t, tc.wantVal, val) | ||
}) | ||
recorder := httptest.NewRecorder() | ||
server.ServeHTTP(recorder, tc.req(t)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package errs | ||
|
||
import "errors" | ||
|
||
var ErrUnauthorized = errors.New("未授权") | ||
var ErrSessionKeyNotFound = errors.New("session 中没找到对应的 key") |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.