Skip to content

Commit f395758

Browse files
fharding1elithrar
authored andcommitted
Remove/cleanup request context helpers (#525)
* 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.
1 parent ff4e71f commit f395758

File tree

5 files changed

+37
-59
lines changed

5 files changed

+37
-59
lines changed

context.go

-18
This file was deleted.

context_test.go

-30
This file was deleted.

mux.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package mux
66

77
import (
8+
"context"
89
"errors"
910
"fmt"
1011
"net/http"
@@ -58,8 +59,7 @@ type Router struct {
5859

5960
// If true, do not clear the request context after handling the request.
6061
//
61-
// Deprecated: No effect when go1.7+ is used, since the context is stored
62-
// on the request itself.
62+
// Deprecated: No effect, since the context is stored on the request itself.
6363
KeepContext bool
6464

6565
// Slice of middlewares to be called after a match is found
@@ -195,8 +195,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
195195
var handler http.Handler
196196
if r.Match(req, &match) {
197197
handler = match.Handler
198-
req = setVars(req, match.Vars)
199-
req = setCurrentRoute(req, match.Route)
198+
req = requestWithVars(req, match.Vars)
199+
req = requestWithRoute(req, match.Route)
200200
}
201201

202202
if handler == nil && match.MatchErr == ErrMethodMismatch {
@@ -426,7 +426,7 @@ const (
426426

427427
// Vars returns the route variables for the current request, if any.
428428
func Vars(r *http.Request) map[string]string {
429-
if rv := contextGet(r, varsKey); rv != nil {
429+
if rv := r.Context().Value(varsKey); rv != nil {
430430
return rv.(map[string]string)
431431
}
432432
return nil
@@ -438,18 +438,20 @@ func Vars(r *http.Request) map[string]string {
438438
// after the handler returns, unless the KeepContext option is set on the
439439
// Router.
440440
func CurrentRoute(r *http.Request) *Route {
441-
if rv := contextGet(r, routeKey); rv != nil {
441+
if rv := r.Context().Value(routeKey); rv != nil {
442442
return rv.(*Route)
443443
}
444444
return nil
445445
}
446446

447-
func setVars(r *http.Request, val interface{}) *http.Request {
448-
return contextSet(r, varsKey, val)
447+
func requestWithVars(r *http.Request, vars map[string]string) *http.Request {
448+
ctx := context.WithValue(r.Context(), varsKey, vars)
449+
return r.WithContext(ctx)
449450
}
450451

451-
func setCurrentRoute(r *http.Request, val interface{}) *http.Request {
452-
return contextSet(r, routeKey, val)
452+
func requestWithRoute(r *http.Request, route *Route) *http.Request {
453+
ctx := context.WithValue(r.Context(), routeKey, route)
454+
return r.WithContext(ctx)
453455
}
454456

455457
// ----------------------------------------------------------------------------

mux_test.go

+24
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

test_helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ import "net/http"
1515
// can be set by making a route that captures the required variables,
1616
// starting a server and sending the request to that server.
1717
func SetURLVars(r *http.Request, val map[string]string) *http.Request {
18-
return setVars(r, val)
18+
return requestWithVars(r, val)
1919
}

0 commit comments

Comments
 (0)