mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
inject Request object into context
This commit is contained in:
@ -78,6 +78,12 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
||||
Response: newResponse(s, w),
|
||||
EnterTime: gtime.TimestampMilli(),
|
||||
}
|
||||
// Inject Request object into context.
|
||||
request.context = context.WithValue(
|
||||
request.Context(),
|
||||
ctxKeyForRequest,
|
||||
request,
|
||||
)
|
||||
request.Cookie = GetCookie(request)
|
||||
request.Session = s.sessionManager.New(
|
||||
r.Context(),
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"net/http"
|
||||
"reflect"
|
||||
@ -130,9 +129,7 @@ func (m *middleware) callHandlerFunc(funcInfo handlerFuncInfo) {
|
||||
funcInfo.Func(m.request)
|
||||
} else {
|
||||
var inputValues = []reflect.Value{
|
||||
reflect.ValueOf(context.WithValue(
|
||||
m.request.Context(), ctxKeyForRequest, m.request,
|
||||
)),
|
||||
reflect.ValueOf(m.request.Context()),
|
||||
}
|
||||
if funcInfo.Type.NumIn() == 2 {
|
||||
var (
|
||||
|
||||
@ -77,6 +77,6 @@ func Test_Router_Handler_Extended_Handler_WithObject(t *testing.T) {
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
t.Assert(client.GetContent("/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
|
||||
t.Assert(client.GetContent("/test/error"), `{"code":50,"message":"error","data":null}`)
|
||||
t.Assert(client.GetContent("/test/error"), `{"code":52,"message":"error","data":null}`)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user