Browse Source
* Remove context helpers in context.go * Update request context funcs to take concrete types * Move TestNativeContextMiddleware to mux_test.go * Clarify KeepContext Go 1.7+ comment Mux doesn't build on Go < 1.7 so the comment doesn't really need to clarify anymore.pull/463/merge
5 changed files with 37 additions and 59 deletions
@ -1,18 +0,0 @@ |
|||||||
package mux |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"net/http" |
|
||||||
) |
|
||||||
|
|
||||||
func contextGet(r *http.Request, key interface{}) interface{} { |
|
||||||
return r.Context().Value(key) |
|
||||||
} |
|
||||||
|
|
||||||
func contextSet(r *http.Request, key, val interface{}) *http.Request { |
|
||||||
if val == nil { |
|
||||||
return r |
|
||||||
} |
|
||||||
|
|
||||||
return r.WithContext(context.WithValue(r.Context(), key, val)) |
|
||||||
} |
|
||||||
@ -1,30 +0,0 @@ |
|||||||
package mux |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"net/http" |
|
||||||
"testing" |
|
||||||
"time" |
|
||||||
) |
|
||||||
|
|
||||||
func TestNativeContextMiddleware(t *testing.T) { |
|
||||||
withTimeout := func(h http.Handler) http.Handler { |
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), time.Minute) |
|
||||||
defer cancel() |
|
||||||
h.ServeHTTP(w, r.WithContext(ctx)) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
r := NewRouter() |
|
||||||
r.Handle("/path/{foo}", withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
||||||
vars := Vars(r) |
|
||||||
if vars["foo"] != "bar" { |
|
||||||
t.Fatal("Expected foo var to be set") |
|
||||||
} |
|
||||||
}))) |
|
||||||
|
|
||||||
rec := NewRecorder() |
|
||||||
req := newRequest("GET", "/path/bar") |
|
||||||
r.ServeHTTP(rec, req) |
|
||||||
} |
|
||||||
Loading…
Reference in new issue