Kevin this is brilliant. It Works <3 <3

On Saturday, 4 December 2021 at 04:29:57 UTC+5 Kevin Mathew S wrote:

> This works perfectly for me
>
> func newReq(method, path, body string, vars map[string]string) 
> *http.Request {
> r := httptest.NewRequest(method, path, strings.NewReader(body))
> return mux.SetURLVars(r, vars)
> }
>
>
> On Tuesday, May 12, 2015 at 7:34:21 PM UTC+5:30 al...@getloopd.com wrote:
>
>> Awesome! This works great Thomas :)
>>
>>
>> On Monday, August 25, 2014 at 3:21:08 PM UTC+8, Thomas Bruyelle wrote:
>>>
>>> I found a solution that doesn't require you to manually add the mux Vars 
>>> in your tests or use the mux Context.
>>>
>>> The key is to create your mux router in a separate function like that
>>>
>>> func Router() *mux.Router {
>>>   r := mux.Router()
>>>   r.HandleFunc("/employees/{1}", employeeHandler)
>>>   (...)
>>>   return r
>>> }
>>>
>>> func init() {
>>> http.Handle("/", Router())
>>> }
>>>
>>> Then in your tests, you can use directly the Router(), just like that 
>>>
>>> func TestEmployeeHandler(t *testing.T) {
>>>   r := http.NewRequest("GET", "employees/1 
>>> <http://localhost:3000/employees/1>", nil)
>>>   w := httptest.NewRecorder()
>>>
>>>  Router().ServeHTTP(w, r)
>>>
>>>   ... 
>>>
>>> }
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/01bc729e-abe0-4bb5-a076-6bd38dab050an%40googlegroups.com.

Reply via email to