You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
690 B
Go
33 lines
690 B
Go
7 years ago
|
// +build go1.7
|
||
|
|
||
|
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)
|
||
|
}
|