reactr icon indicating copy to clipboard operation
reactr copied to clipboard

Adopt table tests in Reactr

Open cohix opened this issue 3 years ago • 3 comments

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

cohix avatar Oct 26 '21 13:10 cohix

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.

AYM1607 avatar May 17 '22 03:05 AYM1607

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.

rnpridgeon avatar May 17 '22 09:05 rnpridgeon

Just submitted a PR, I'd appreciate some reviews :)

AYM1607 avatar May 18 '22 06:05 AYM1607