{"action":"created","issue":{"url":"https://api.github.com/repos/labstack/echo/issues/659","repository_url":"https://api.github.com/repos/labstack/echo","labels_url":"https://api.github.com/repos/labstack/echo/issues/659/labels{/name}","comments_url":"https://api.github.com/repos/labstack/echo/issues/659/comments","events_url":"https://api.github.com/repos/labstack/echo/issues/659/events","html_url":"https://github.com/labstack/echo/issues/659","id":176506433,"number":659,"title":"How to test my handlers with middleware","user":{"login":"heynemann","id":60965,"avatar_url":"https://avatars.githubusercontent.com/u/60965?v=3","gravatar_id":"","url":"https://api.github.com/users/heynemann","html_url":"https://github.com/heynemann","followers_url":"https://api.github.com/users/heynemann/followers","following_url":"https://api.github.com/users/heynemann/following{/other_user}","gists_url":"https://api.github.com/users/heynemann/gists{/gist_id}","starred_url":"https://api.github.com/users/heynemann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heynemann/subscriptions","organizations_url":"https://api.github.com/users/heynemann/orgs","repos_url":"https://api.github.com/users/heynemann/repos","events_url":"https://api.github.com/users/heynemann/events{/privacy}","received_events_url":"https://api.github.com/users/heynemann/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-12T23:00:05Z","updated_at":"2016-09-13T01:11:20Z","closed_at":"2016-09-13T01:11:20Z","body":"### Description\r\nSorry for all the questions, but I'm very new to Echo. Right now I'm writing a test that validates my handler.\r\n\r\nThe test is passing, but it does not invoke middleware. This is an issue in my scenario because my middleware authenticates that user and store the user in the context.\r\n\r\nI'm using the way described in the docs. I've also tried using httptest.NewServer, but echo is not a valid http.Handler (ServeHTTP has wrong type of arguments).\r\n\r\nAny ideas on how to do this?"},"comment":{"url":"https://api.github.com/repos/labstack/echo/issues/comments/246543783","html_url":"https://github.com/labstack/echo/issues/659#issuecomment-246543783","issue_url":"https://api.github.com/repos/labstack/echo/issues/659","id":246543783,"user":{"login":"heynemann","id":60965,"avatar_url":"https://avatars.githubusercontent.com/u/60965?v=3","gravatar_id":"","url":"https://api.github.com/users/heynemann","html_url":"https://github.com/heynemann","followers_url":"https://api.github.com/users/heynemann/followers","following_url":"https://api.github.com/users/heynemann/following{/other_user}","gists_url":"https://api.github.com/users/heynemann/gists{/gist_id}","starred_url":"https://api.github.com/users/heynemann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heynemann/subscriptions","organizations_url":"https://api.github.com/users/heynemann/orgs","repos_url":"https://api.github.com/users/heynemann/repos","events_url":"https://api.github.com/users/heynemann/events{/privacy}","received_events_url":"https://api.github.com/users/heynemann/received_events","type":"User","site_admin":false},"created_at":"2016-09-13T01:11:20Z","updated_at":"2016-09-13T01:11:20Z","body":"If anyone wants to do this I figured it out:\r\n```\r\n//Get from server\r\nfunc Get(app *api.App, url string) (int, string) {\r\n\treturn doRequest(app, \"GET\", url, auth)\r\n}\r\n\r\nfunc doRequest(app *api.App, method, url string) (int, string) {\r\n\tapp.Engine.SetHandler(app.Echo)\r\n\tts := httptest.NewServer(app.Engine.(*standard.Server))\r\n\tdefer ts.Close()\r\n\r\n\treq, err := http.NewRequest(\"GET\", fmt.Sprintf(\"%s%s\", ts.URL, url), nil)\r\n\tgomega.Expect(err).NotTo(gomega.HaveOccurred())\r\n\r\n\tclient := &http.Client{}\r\n\tres, err := client.Do(req)\r\n\tgomega.Expect(err).NotTo(gomega.HaveOccurred())\r\n\r\n\tif res != nil {\r\n\t\tdefer res.Body.Close()\r\n\t}\r\n\r\n\tb, err := ioutil.ReadAll(res.Body)\r\n\tgomega.Expect(err).NotTo(gomega.HaveOccurred())\r\n\r\n\treturn res.StatusCode, string(b)\r\n}\r\n```\r\nApp is a struct I have that has both the Engine I'm running Echo on, as well as the Echo struct.\r\n\r\nIt's worth noting that this approach only works if you are using the standard engine. But IMHO it's good enough for tests.\r\n\r\nUsing it is as simple as:\r\n\r\n```\r\nstatus, body := Get(app, \"/experiments\", \"test@test.com\")\r\nExpect(status).To(Equal(http.StatusOK))\r\nExpect(body).NotTo(BeEmpty())\r\n```\r\n\r\nI simplified the assertions just to show how it's used. Ideally you would assert the contents of the body (JSON in my scenario).\r\n\r\nAs this can be easily accomplished I'm closing this issue. I do believe that having a stronger testing infrastructure would benefit echo, though.\r\n"}}
{"actor":{"display_login":"heynemann"}}