Skip to content

Commit 04da6ef

Browse files
committed
Move TestNativeContextMiddleware to mux_test.go
1 parent faac3f3 commit 04da6ef

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

context_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

mux_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mux
77
import (
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

2123
func (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
28082832
func mapToPairs(m map[string]string) []string {
28092833
var i int

0 commit comments

Comments
 (0)