-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
[refactor] Test endpoints through ServeHTTP, not by raw handler #11396
Comments
Hi @kisunji, I’m interested in attempting to work on this issue. I just have a couple questions as I am working on it.
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, tt.code, resp.Code) then I removed tests := []struct {
name string
method, url string
body io.Reader
code int
init tokens
raw tokens
effective tokens
}{
{
name: "bad token name",
method: "PUT",
url: "nope?token=root",
body: body("X"),
code: http.StatusNotFound,
},
{
name: "bad JSON",
method: "PUT",
url: "acl_token?token=root",
body: badJSON(),
code: http.StatusBadRequest,
}, Now for this case it is checking for a specific internal error consul/agent/agent_endpoint_test.go Lines 5435 to 5436 in ab425e3
Is it ok that I’m no longer checking for specific internal errors and only checking for the response code? If not, how could I approach checking the internal error while also calling Here is another case where I’m not exactly sure how I could handle if I were to change to using consul/agent/agent_endpoint_test.go Lines 66 to 70 in ab425e3
|
Hi @Mathew-Estafanous, thank you for your interest in contributing! Given that the scope of this issue is fairly large, we encourage you to start with one or two tests to keep the PR small (helps the team during review as well).
Regarding your last point about tests that check for typed objects, here is an example of a struct consul/agent/agent_endpoint_test.go Lines 1499 to 1502 in 01e9740
Minor caveatOur team is discussing the nature of these HTTP-handler tests and what they should assert; there are some shortcomings in converting the json response back to a typed go struct that we need to address internally. There is a chance our goalposts may change throughout the process but we welcome your PR to help kickstart that conversation. |
Background
In many places in our tests, when we want to make API calls to handlers, we tend to use the raw handler (e.g.
a.srv.AgentToken(resp, req)
).This is problematic because it skips the middleware that real HTTP requests go through, which includes (but is not limited to) multiplexing based on URL patterns, blocking not-allowed HTTP methods, and translating application-level errors to HTTP error codes and messages.
This means that developer errors like this may not get caught by the test suite:
AgentJoin
is not responsible for validating the request's URL or method and may fail in unpredictable ways, wasting developer time or misleading devs to make incorrect test assertions.Solution
The proposed solution is to start using
Agent.srv.h.ServeHTTP(resp, req)
to handle requests generically instead of specifying a typed handler.See examples here.
Related PRs
#11445 (thanks @Mathew-Estafanous)
The text was updated successfully, but these errors were encountered: