Skip to content
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

Help testing middleware #51

Closed
raphael opened this issue May 8, 2015 · 9 comments
Closed

Help testing middleware #51

raphael opened this issue May 8, 2015 · 9 comments
Assignees

Comments

@raphael
Copy link

raphael commented May 8, 2015

echo currently makes it hard/impossible to initialize a context outside of an actual HTTP request handling (the "response" field is private for example). This makes testing echo middleware tricky as one cannot just mock a context. It would be good if echo exposed a way to create arbitrary contexts. To make things more concrete here is an example of what I'm trying to achieve:

// Create dummy echo.Context with request for tests
// Note: echo makes it impossible to initialize the context response :(
func dummyContext() *echo.Context {
    req, _ := http.NewRequest("POST", "http://example.com", strings.NewReader("foo"))
    ctx := echo.Context{Request: req}
    ctx.String(200, "ok") // BLOWS UP because "response" not initialized
    return &ctx
}
@vishr vishr closed this as completed in 04cdefb May 8, 2015
@vishr
Copy link
Member

vishr commented May 8, 2015

Let me know if this helps.

@raphael
Copy link
Author

raphael commented May 8, 2015

Thanks for the quick response! Something else that doesn't work though is then trying to use c.Set() and c.Get() in tests as it tries to write to a nil hashmap.

@vishr
Copy link
Member

vishr commented May 8, 2015

I think providing a NewContext function should solve this?

@raphael
Copy link
Author

raphael commented May 8, 2015

Yup that would be perfect!

@vishr
Copy link
Member

vishr commented May 8, 2015

Please verify b416efc

@raphael
Copy link
Author

raphael commented May 8, 2015

Thank you this helps. One last thing that is still not possible is to specify the response status and size because they are private fields of the Response struct. I'm using these fields in the middleware so being able to set them would help testing.

@vishr
Copy link
Member

vishr commented May 8, 2015

Check https://github.com/labstack/echo/blob/master/context_test.go#L26. WriteHeader and Write should set status and size on response.

@raphael
Copy link
Author

raphael commented May 8, 2015

Yup calling c.Response.Write() worked, thanks! in case you're curious: https://github.com/rightscale/go_middleware

@vishr
Copy link
Member

vishr commented May 8, 2015

Great!

@vishr vishr self-assigned this May 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants