Forward context in fasthttpadaptor (#720)

* forward context in fasthttpadaptor

* run go fmt
This commit is contained in:
Lovro Mažgon
2020-01-10 16:41:16 +01:00
committed by Erik Dubbelboer
parent 16c53d6b04
commit 8d8443d77c
2 changed files with 14 additions and 1 deletions
+1 -1
View File
@@ -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() {
+13
View File
@@ -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)
}
}