{"action":"opened","issue":{"url":"https://api.github.com/repos/pressly/chi/issues/134","repository_url":"https://api.github.com/repos/pressly/chi","labels_url":"https://api.github.com/repos/pressly/chi/issues/134/labels{/name}","comments_url":"https://api.github.com/repos/pressly/chi/issues/134/comments","events_url":"https://api.github.com/repos/pressly/chi/issues/134/events","html_url":"https://github.com/pressly/chi/issues/134","id":199455960,"number":134,"title":"proposal: test context builder (test helper function)","user":{"login":"kpurdon","id":5130441,"avatar_url":"https://avatars.githubusercontent.com/u/5130441?v=3","gravatar_id":"","url":"https://api.github.com/users/kpurdon","html_url":"https://github.com/kpurdon","followers_url":"https://api.github.com/users/kpurdon/followers","following_url":"https://api.github.com/users/kpurdon/following{/other_user}","gists_url":"https://api.github.com/users/kpurdon/gists{/gist_id}","starred_url":"https://api.github.com/users/kpurdon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kpurdon/subscriptions","organizations_url":"https://api.github.com/users/kpurdon/orgs","repos_url":"https://api.github.com/users/kpurdon/repos","events_url":"https://api.github.com/users/kpurdon/events{/privacy}","received_events_url":"https://api.github.com/users/kpurdon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-01-08T23:33:35Z","updated_at":"2017-01-08T23:33:35Z","closed_at":null,"body":"I've switched a personal project over to chi and have been working on converting my integration tests. I've found myself repeating this chunk of code in tests quite often. Each test case (table tests) provides a list of params `tc.params` which is a `map[string]interface{}` that I load into the URLParams context. This allows me to test functionality based on different param values passed to a handler.\r\n\r\n```\r\nreq, err := http.NewRequest(\"METHOD\", \"/path/:variable\", nil)\r\nassert.NoError(err, tc.description)\r\n\r\nrctx := chi.NewRouteContext()\r\nfor k, v := range tc.params {\r\n\trctx.URLParams.Add(k, v)\r\n}\r\nctx := context.WithValue(context.Background(), chi.RouteCtxKey, rctx)\r\n\r\nw := httptest.NewRecorder()\r\nSomeHTTPHandler(w, req.WithContext(ctx))\r\n```\r\n\r\nWhile I could certainly extract the building of a `context.Context` in my own project, I think it would be a nice addition to chi. I imaging something like:\r\n\r\n```\r\nfunc NewTestContext(ps map[string]interface{}) context.Context {\r\n\trctx := NewRouteContext()\r\n\tfor k, v := range ps {\r\n\t\trctx.URLParams.Add(k, v)\r\n\t}\r\n\r\n\treturn context.WithValue(context.Background(), RouteCtxKey, rctx)\r\n}\r\n```\r\n\r\nThis would change my code snippet to:\r\n\r\n```\r\nreq, err := http.NewRequest(\"METHOD\", \"/path/:variable\", nil)\r\nassert.NoError(err, tc.description)\r\n\r\nw := httptest.NewRecorder()\r\nSomeHTTPHandler(w, req.WithContext(chi.NewTestContext(tc.params)))\r\n```\r\n\r\nI would be happy to provide a PR for this addition if your open too it. Thanks!"}}
{"actor":{"display_login":"kpurdon"}}