@@ -28,13 +28,31 @@ func TestMain(m *testing.M) {
2828 tonic .SetErrorHook (errorHook )
2929
3030 g := gin .Default ()
31+
32+ // for context test
33+ g .Use (func (c * gin.Context ) {
34+ if c .FullPath () == "/context" {
35+ if val , ok := c .GetQuery ("param" ); ok {
36+ c .Set ("param" , val )
37+ }
38+ if val , ok := c .GetQuery ("param-optional" ); ok {
39+ c .Set ("param-optional" , val )
40+ }
41+ if val , ok := c .GetQuery ("param-optional-validated" ); ok {
42+ c .Set ("param-optional-validated" , val )
43+ }
44+ }
45+ c .Next ()
46+ })
47+
3148 g .GET ("/simple" , tonic .Handler (simpleHandler , 200 ))
3249 g .GET ("/scalar" , tonic .Handler (scalarHandler , 200 ))
3350 g .GET ("/error" , tonic .Handler (errorHandler , 200 ))
3451 g .GET ("/path/:param" , tonic .Handler (pathHandler , 200 ))
3552 g .GET ("/query" , tonic .Handler (queryHandler , 200 ))
3653 g .GET ("/query-old" , tonic .Handler (queryHandlerOld , 200 ))
3754 g .POST ("/body" , tonic .Handler (bodyHandler , 200 ))
55+ g .GET ("/context" , tonic .Handler (contextHandler , 200 ))
3856
3957 r = g
4058
@@ -130,6 +148,17 @@ func TestBody(t *testing.T) {
130148 tester .Run ()
131149}
132150
151+ func TestContext (t * testing.T ) {
152+ tester := iffy .NewTester (t , r )
153+
154+ tester .AddCall ("context" , "GET" , "/context?param=foo" , `` ).Checkers (iffy .ExpectStatus (200 ), expectString ("param" , "foo" ))
155+ tester .AddCall ("context" , "GET" , "/context" , `` ).Checkers (iffy .ExpectStatus (400 ))
156+ tester .AddCall ("context" , "GET" , "/context?param=foo¶m-optional=bar" , `` ).Checkers (iffy .ExpectStatus (200 ), expectString ("param-optional" , "bar" ))
157+ tester .AddCall ("context" , "GET" , "/context?param=foo¶m-optional-validated=foo" , `` ).Checkers (iffy .ExpectStatus (200 ), expectString ("param-optional-validated" , "foo" ))
158+
159+ tester .Run ()
160+ }
161+
133162func errorHandler (c * gin.Context ) error {
134163 return errors .New ("error" )
135164}
@@ -199,6 +228,17 @@ func bodyHandler(c *gin.Context, in *bodyIn) (*bodyIn, error) {
199228 return in , nil
200229}
201230
231+ type ContextIn struct {
232+ Param string `context:"param" json:"param" validate:"required"`
233+ ParamInt int `context:"param-int" json:"param-int"`
234+ ParamOptional string `context:"param-optional" json:"param-optional"`
235+ ValidatedParamOptional string `context:"param-optional-validated" json:"param-optional-validated" validate:"eq=|eq=foo|gt=10"`
236+ }
237+
238+ func contextHandler (c * gin.Context , in * ContextIn ) (* ContextIn , error ) {
239+ return in , nil
240+ }
241+
202242func expectEmptyBody (r * http.Response , body string , obj interface {}) error {
203243 if len (body ) != 0 {
204244 return fmt .Errorf ("Body '%s' should be empty" , body )
0 commit comments