-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to see the vars when calling from a unit test #373
Comments
I changed my tests to set the vars directly like in #342 and it passes: package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
"testing"
)
func TestMetricsHandler(t *testing.T) {
tt := []struct {
routeVariable string
shouldPass bool
}{
{"goroutines", true},
{"heap", true},
{"counters", true},
{"queries", true},
{"adhadaeqm3k", false},
}
for _, tc := range tt {
path := "whocares"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
t.Fatal(err)
}
req = mux.SetURLVars(req, map[string]string{
"type": tc.routeVariable,
})
rr := httptest.NewRecorder()
/* Appears we can call it directly if we want?
handler := http.HandlerFunc(MetricsHandler)
handler.ServeHTTP(rr, req)*/
MetricsHandler(rr, req)
expected := fmt.Sprintf("Type: %v", tc.routeVariable)
if rr.Body.String() != expected {
t.Errorf("handler should have failed on routeVariable %s: got %v want %v",
tc.routeVariable, rr.Body.String(), expected)
}
}
} Can you elaborate on comments in #342? My understanding is "yes you can use Thanks! |
I’m just reading this off my phone so I could be wrong, but it seems in your test you never actually instantiate a mux, you’re just hanging the handler off the http recorder.
… On May 12, 2018, at 10:04, Sean Walberg ***@***.***> wrote:
I changed my tests to set the vars directly like in #342 and it passes:
package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
"testing"
)
func TestMetricsHandler(t *testing.T) {
tt := []struct {
routeVariable string
shouldPass bool
}{
{"goroutines", true},
{"heap", true},
{"counters", true},
{"queries", true},
{"adhadaeqm3k", false},
}
for _, tc := range tt {
path := "whocares"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
t.Fatal(err)
}
req = mux.SetURLVars(req, map[string]string{
"type": tc.routeVariable,
})
rr := httptest.NewRecorder()
/* Appears we can call it directly if we want?
handler := http.HandlerFunc(MetricsHandler)
handler.ServeHTTP(rr, req)*/
MetricsHandler(rr, req)
expected := fmt.Sprintf("Type: %v", tc.routeVariable)
if rr.Body.String() != expected {
t.Errorf("handler should have failed on routeVariable %s: got %v want %v",
tc.routeVariable, rr.Body.String(), expected)
}
}
}
Can you elaborate on comments in #342? My understanding is "yes you can use mux.SetURLVars but you should really do it the way I've shown" which I prefer, but can't get to work.
Thanks!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Mirrors what I’m seeing.
mux.SetURLVars uses the request context directly, and thus doesn’t rely on
a Router instance.
The URL parsing into vars does (as expected) require a Router instance.
On Sat, May 12, 2018 at 11:57 AM Kamil Kisiel <notifications@github.com>
wrote:
… I’m just reading this off my phone so I could be wrong, but it seems in
your test you never actually instantiate a mux, you’re just hanging the
handler off the http recorder.
> On May 12, 2018, at 10:04, Sean Walberg ***@***.***>
wrote:
>
> I changed my tests to set the vars directly like in #342 and it passes:
>
> package main
>
> import (
> "fmt"
> "github.com/gorilla/mux"
> "net/http"
> "net/http/httptest"
> "testing"
> )
>
> func TestMetricsHandler(t *testing.T) {
> tt := []struct {
> routeVariable string
> shouldPass bool
> }{
> {"goroutines", true},
> {"heap", true},
> {"counters", true},
> {"queries", true},
> {"adhadaeqm3k", false},
> }
>
> for _, tc := range tt {
> path := "whocares"
> req, err := http.NewRequest("GET", path, nil)
> if err != nil {
> t.Fatal(err)
> }
> req = mux.SetURLVars(req, map[string]string{
> "type": tc.routeVariable,
> })
> rr := httptest.NewRecorder()
> /* Appears we can call it directly if we want?
> handler := http.HandlerFunc(MetricsHandler)
> handler.ServeHTTP(rr, req)*/
> MetricsHandler(rr, req)
>
> expected := fmt.Sprintf("Type: %v", tc.routeVariable)
>
> if rr.Body.String() != expected {
> t.Errorf("handler should have failed on routeVariable %s: got %v want
%v",
> tc.routeVariable, rr.Body.String(), expected)
> }
> }
> }
> Can you elaborate on comments in #342? My understanding is "yes you can
use mux.SetURLVars but you should really do it the way I've shown" which I
prefer, but can't get to work.
>
> Thanks!
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub, or mute the thread.
>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#373 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcOws8EcHTZQ7EWvl11cBOIFBWC9aks5txzB9gaJpZM4T8cnN>
.
|
swalberg
added a commit
to swalberg/mux
that referenced
this issue
May 13, 2018
The example in the README does not pass the request through a mux therefore the request variables from the path are never populated. Update the sample to create a minimum viable router to use. Fixes gorilla#373
elithrar
pushed a commit
that referenced
this issue
May 13, 2018
The example in the README does not pass the request through a mux therefore the request variables from the path are never populated. Update the sample to create a minimum viable router to use. Fixes #373
@swalberg Thanks a lot for your response, man. Your help fixed the problem I was having the whole afternoon. Thanks a lot! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What version of Go are you running?
go version go1.10.2 darwin/amd64
What version of gorilla/mux are you at? 5e55a4a
Describe your problem (and what you have tried so far)
I'm trying to test a route with a path variable but the handler sees an empty map when called from a unit test. It works fine with cURL against a running instance.
Paste a minimal, runnable, reproduction of your issue below (use backticks to format it)
I have slightly altered the example from the README.
The text was updated successfully, but these errors were encountered: