reactr
reactr copied to clipboard
Adopt table tests in Reactr
Another idea, the tests could be condensed quite a bit, for example:
func TestDefaultRules(t *testing.T) {
rules := defaultHTTPRules()
tests := []struct {
name string
url string
}{
{"http allowed", "http://example.com"},
{"https allowed", "https://example.com"},
{"IP allowed", "http://100.11.12.13"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, test.url, nil)
if err := rules.requestIsAllowed(req); err != nil {
t.Error("error occurred, should not have:", err)
}
})
}
}
Originally posted by @jagger27 in https://github.com/suborbital/reactr/issues/160#issuecomment-951364134
Hi @cohix can I take on this? I was thinking about refactoring the whole https://github.com/suborbital/reactr/blob/main/rcap/http_rulefilter_test.go file first and then see if there's any other tests in the repo that can benefit from the table structure.
We'd love the help! Anything that would make sense as a parameterized test is a great candidate for refactoring into a table-driven test. http_rulefilter_test.go is a great example of such a test.
Just submitted a PR, I'd appreciate some reviews :)