File tree Expand file tree Collapse file tree 2 files changed +24
-30
lines changed Expand file tree Collapse file tree 2 files changed +24
-30
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ package mux
77import (
88 "bufio"
99 "bytes"
10+ "context"
1011 "errors"
1112 "fmt"
1213 "io/ioutil"
@@ -16,6 +17,7 @@ import (
1617 "reflect"
1718 "strings"
1819 "testing"
20+ "time"
1921)
2022
2123func (r * Route ) GoString () string {
@@ -2804,6 +2806,28 @@ func TestSubrouterNotFound(t *testing.T) {
28042806 }
28052807}
28062808
2809+ func TestContextMiddleware (t * testing.T ) {
2810+ withTimeout := func (h http.Handler ) http.Handler {
2811+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
2812+ ctx , cancel := context .WithTimeout (r .Context (), time .Minute )
2813+ defer cancel ()
2814+ h .ServeHTTP (w , r .WithContext (ctx ))
2815+ })
2816+ }
2817+
2818+ r := NewRouter ()
2819+ r .Handle ("/path/{foo}" , withTimeout (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
2820+ vars := Vars (r )
2821+ if vars ["foo" ] != "bar" {
2822+ t .Fatal ("Expected foo var to be set" )
2823+ }
2824+ })))
2825+
2826+ rec := NewRecorder ()
2827+ req := newRequest ("GET" , "/path/bar" )
2828+ r .ServeHTTP (rec , req )
2829+ }
2830+
28072831// mapToPairs converts a string map to a slice of string pairs
28082832func mapToPairs (m map [string ]string ) []string {
28092833 var i int
You can’t perform that action at this time.
0 commit comments