mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Forward context in fasthttpadaptor (#720)
* forward context in fasthttpadaptor * run go fmt
This commit is contained in:
committed by
Erik Dubbelboer
parent
16c53d6b04
commit
8d8443d77c
@@ -82,7 +82,7 @@ func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
|
||||
r.URL = rURL
|
||||
|
||||
var w netHTTPResponseWriter
|
||||
h.ServeHTTP(&w, &r)
|
||||
h.ServeHTTP(&w, r.WithContext(ctx))
|
||||
|
||||
ctx.SetStatusCode(w.StatusCode())
|
||||
for k, vv := range w.Header() {
|
||||
|
||||
@@ -32,6 +32,8 @@ func TestNewFastHTTPHandler(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
expectedContextKey := "contextKey"
|
||||
expectedContextValue := "contextValue"
|
||||
|
||||
callsCount := 0
|
||||
nethttpH := func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -74,6 +76,9 @@ func TestNewFastHTTPHandler(t *testing.T) {
|
||||
if !reflect.DeepEqual(r.URL, expectedURL) {
|
||||
t.Fatalf("unexpected URL: %#v. Expecting %#v", r.URL, expectedURL)
|
||||
}
|
||||
if r.Context().Value(expectedContextKey) != expectedContextValue {
|
||||
t.Fatalf("unexpected context value for key %q. Expecting %q", expectedContextKey, expectedContextValue)
|
||||
}
|
||||
|
||||
for k, expectedV := range expectedHeader {
|
||||
v := r.Header.Get(k)
|
||||
@@ -88,6 +93,7 @@ func TestNewFastHTTPHandler(t *testing.T) {
|
||||
fmt.Fprintf(w, "request body is %q", body)
|
||||
}
|
||||
fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH))
|
||||
fasthttpH = setContextValueMiddleware(fasthttpH, expectedContextKey, expectedContextValue)
|
||||
|
||||
var ctx fasthttp.RequestCtx
|
||||
var req fasthttp.Request
|
||||
@@ -128,3 +134,10 @@ func TestNewFastHTTPHandler(t *testing.T) {
|
||||
t.Fatalf("unexpected response body %q. Expecting %q", resp.Body(), expectedResponseBody)
|
||||
}
|
||||
}
|
||||
|
||||
func setContextValueMiddleware(next fasthttp.RequestHandler, key string, value interface{}) fasthttp.RequestHandler {
|
||||
return func(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetUserValue(key, value)
|
||||
next(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user