matcher.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package gock
  2. import "net/http"
  3. // MatchersHeader exposes an slice of HTTP header specific mock matchers.
  4. var MatchersHeader = []MatchFunc{
  5. MatchMethod,
  6. MatchScheme,
  7. MatchHost,
  8. MatchPath,
  9. MatchHeaders,
  10. MatchQueryParams,
  11. }
  12. // MatchersBody exposes an slice of HTTP body specific built-in mock matchers.
  13. var MatchersBody = []MatchFunc{
  14. MatchBody,
  15. }
  16. // Matchers stores all the built-in mock matchers.
  17. var Matchers = append(MatchersHeader, MatchersBody...)
  18. // DefaultMatcher stores the default Matcher instance used to match mocks.
  19. var DefaultMatcher = NewMatcher()
  20. // MatchFunc represents the required function
  21. // interface implemented by matchers.
  22. type MatchFunc func(*http.Request, *Request) (bool, error)
  23. // Matcher represents the required interface implemented by mock matchers.
  24. type Matcher interface {
  25. // Get returns a slice of registered function matchers.
  26. Get() []MatchFunc
  27. // Add adds a new matcher function.
  28. Add(MatchFunc)
  29. // Set sets the matchers functions stack.
  30. Set([]MatchFunc)
  31. // Flush flushes the current matchers function stack.
  32. Flush()
  33. // Match matches the given http.Request with a mock Request.
  34. Match(*http.Request, *Request) (bool, error)
  35. }
  36. // MockMatcher implements a mock matcher
  37. type MockMatcher struct {
  38. Matchers []MatchFunc
  39. }
  40. // NewMatcher creates a new mock matcher
  41. // using the default matcher functions.
  42. func NewMatcher() *MockMatcher {
  43. return &MockMatcher{Matchers: Matchers}
  44. }
  45. // NewBasicMatcher creates a new matcher with header only mock matchers.
  46. func NewBasicMatcher() *MockMatcher {
  47. return &MockMatcher{Matchers: MatchersHeader}
  48. }
  49. // NewEmptyMatcher creates a new empty matcher with out default amtchers.
  50. func NewEmptyMatcher() *MockMatcher {
  51. return &MockMatcher{Matchers: []MatchFunc{}}
  52. }
  53. // Get returns a slice of registered function matchers.
  54. func (m *MockMatcher) Get() []MatchFunc {
  55. return m.Matchers
  56. }
  57. // Add adds a new function matcher.
  58. func (m *MockMatcher) Add(fn MatchFunc) {
  59. m.Matchers = append(m.Matchers, fn)
  60. }
  61. // Set sets a new stack of matchers functions.
  62. func (m *MockMatcher) Set(stack []MatchFunc) {
  63. m.Matchers = stack
  64. }
  65. // Flush flushes the current matcher
  66. func (m *MockMatcher) Flush() {
  67. m.Matchers = []MatchFunc{}
  68. }
  69. // Match matches the given http.Request with a mock request
  70. // returning true in case that the request matches, otherwise false.
  71. func (m *MockMatcher) Match(req *http.Request, ereq *Request) (bool, error) {
  72. for _, matcher := range m.Matchers {
  73. matches, err := matcher(req, ereq)
  74. if err != nil {
  75. return false, err
  76. }
  77. if !matches {
  78. return false, nil
  79. }
  80. }
  81. return true, nil
  82. }
  83. // MatchMock is a helper function that matches the given http.Request
  84. // in the list of registered mocks, returning it if matches or error if it fails.
  85. func MatchMock(req *http.Request) (Mock, error) {
  86. for _, mock := range GetAll() {
  87. matches, err := mock.Match(req)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if matches {
  92. return mock, nil
  93. }
  94. }
  95. return nil, nil
  96. }