diff --git a/mux_test.go b/mux_test.go index bac758bc..a1ce7f10 100644 --- a/mux_test.go +++ b/mux_test.go @@ -2546,13 +2546,13 @@ func TestMethodsSubrouterPathVariable(t *testing.T) { } } -func ExampleSetURLVars() { +func TestExampleSetURLVars(t *testing.T) { req, _ := http.NewRequest("GET", "/foo", nil) - req = SetURLVars(req, map[string]string{"foo": "bar"}) + req = SetURLVars(t, req, map[string]string{"foo": "bar"}) - fmt.Println(Vars(req)["foo"]) - - // Output: bar + if val := Vars(req)["foo"]; val != "bar" { + t.Errorf("Expected URL var 'foo' to be 'bar', but got %q", val) + } } // testMethodsSubrouter runs an individual methodsSubrouterTest. diff --git a/test_helpers.go b/test_helpers.go index 5f5c496d..73e883b0 100644 --- a/test_helpers.go +++ b/test_helpers.go @@ -4,7 +4,10 @@ package mux -import "net/http" +import ( + "net/http" + "testing" +) // SetURLVars sets the URL variables for the given request, to be accessed via // mux.Vars for testing route behaviour. Arguments are not modified, a shallow @@ -14,6 +17,7 @@ import "net/http" // inject variables into the request context. Alternatively, URL variables // can be set by making a route that captures the required variables, // starting a server and sending the request to that server. -func SetURLVars(r *http.Request, val map[string]string) *http.Request { +func SetURLVars(t *testing.T, r *http.Request, val map[string]string) *http.Request { + t.Helper() return requestWithVars(r, val) }