You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
GIN's gin.Context implements Go context.Context interface, so we can do something like
ctx, cancel:=context.WithCancel(ginCtx)
and pass this ctx around.
Can we make changes in the GIN's context Value(key interface{}) interface{} method to return the original GIN's context?
Expectations
package demo
import (
"context""github.com/gin-gonic/gin"
)
funcAwesomeFun(ginCtx*gin.Context) {
ctx, cancel:=context.WithCancel(ginCtx)
defercancel()
test(ctx)
}
functest(ctx context.Context) {
ginCtx, ok:=ctx.Value(gin.ContextKey).(*gin.Context)
if!ok {
return
}
// We now have the GIN context in ginCtx var
}
The text was updated successfully, but these errors were encountered:
ifaisalalam
changed the title
Option to get gin.Context from context.Context
[Enhancement]: Option to get *gin.Context from context.Context using .Value()Aug 14, 2021
ifaisalalam
changed the title
[Enhancement]: Option to get *gin.Context from context.Context using .Value()
[Enhancement]: Option to get *gin.Context from context.ContextAug 14, 2021
For anyone trying to test a gin handler by manually cancelling a context, this code snippet might help:
req:=httptest.NewRequest("POST", "/my_api", bytes.NewReader(body))
resp:=httptest.NewRecorder()
tc, engine:=gin.CreateTestContext(resp)
engine.ContextWithFallback=truenewReqCtx, cancel:=context.WithCancel(req.Context())
req=req.WithContext(newReqCtx)
tc.Request=reqhandlers.HandleMyApi(tc) // This implements /my_apicancel() // Call this when you want to simulate client disconnect - `tc` will be context canceled
This is helpful to simulate a client disconnecting in the middle of a request in a unit/functional test.
Description
GIN's
gin.Context
implements Gocontext.Context
interface, so we can do something likeand pass this
ctx
around.Can we make changes in the GIN's context
Value(key interface{}) interface{}
method to return the original GIN's context?Expectations
The text was updated successfully, but these errors were encountered: