checker_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package checker
  2. import (
  3. "errors"
  4. "net/http"
  5. "testing"
  6. )
  7. // Mock implements the Impl interface for testing.
  8. type Mock struct {
  9. result bool
  10. err error
  11. hash string
  12. }
  13. func (m Mock) Check(r *http.Request) (bool, error) { return m.result, m.err }
  14. func (m Mock) Hash() string { return m.hash }
  15. func TestListCheck_AndSemantics(t *testing.T) {
  16. req, _ := http.NewRequest(http.MethodGet, "http://example.com", nil)
  17. tests := []struct {
  18. name string
  19. list List
  20. want bool
  21. wantErr bool
  22. }{
  23. {
  24. name: "all true",
  25. list: List{Mock{true, nil, "a"}, Mock{true, nil, "b"}},
  26. want: true,
  27. },
  28. {
  29. name: "one false",
  30. list: List{Mock{true, nil, "a"}, Mock{false, nil, "b"}},
  31. want: false,
  32. },
  33. {
  34. name: "error propagates",
  35. list: List{Mock{true, nil, "a"}, Mock{true, errors.New("boom"), "b"}},
  36. want: false,
  37. wantErr: true,
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. got, err := tt.list.Check(req)
  43. if (err != nil) != tt.wantErr {
  44. t.Fatalf("unexpected error state: %v", err)
  45. }
  46. if got != tt.want {
  47. t.Fatalf("expected %v, got %v", tt.want, got)
  48. }
  49. })
  50. }
  51. }