unit testing of a router provider in gosford

If you want to test that the router is returning expected handler (vs test behaviour), you can do something like the following:

r := mux.NewRouter()
r.HandleFunc("/a", handlerA).Methods("GET")
r.HandleFunc("/b", handlerB).Methods("GET")

req, err := httptest.NewRequest("GET", "http://example.com/a", nil)
require.NoError(err, "create request")

m := &mux.RouteMatch{}
require.True(r.Match(req, m), "no match")

v1 := reflect.ValueOf(m.Handler)
v2 := reflect.ValueOf(handlerA)
require.Equal(v1.Pointer(), v2.Pointer(), "wrong handler")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top