1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
package gock
import (
"net/http"
"net/url"
"testing"
"github.com/nbio/st"
)
func TestRegisteredMatchers(t *testing.T) {
st.Expect(t, len(MatchersHeader), 7)
st.Expect(t, len(MatchersBody), 1)
}
func TestNewMatcher(t *testing.T) {
matcher := NewMatcher()
// Funcs are not comparable, checking slice length as it's better than nothing
// See https://golang.org/pkg/reflect/#DeepEqual
st.Expect(t, len(matcher.Matchers), len(Matchers))
st.Expect(t, len(matcher.Get()), len(Matchers))
}
func TestNewBasicMatcher(t *testing.T) {
matcher := NewBasicMatcher()
// Funcs are not comparable, checking slice length as it's better than nothing
// See https://golang.org/pkg/reflect/#DeepEqual
st.Expect(t, len(matcher.Matchers), len(MatchersHeader))
st.Expect(t, len(matcher.Get()), len(MatchersHeader))
}
func TestNewEmptyMatcher(t *testing.T) {
matcher := NewEmptyMatcher()
st.Expect(t, len(matcher.Matchers), 0)
st.Expect(t, len(matcher.Get()), 0)
}
func TestMatcherAdd(t *testing.T) {
matcher := NewMatcher()
st.Expect(t, len(matcher.Matchers), len(Matchers))
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return true, nil
})
st.Expect(t, len(matcher.Get()), len(Matchers)+1)
}
func TestMatcherSet(t *testing.T) {
matcher := NewMatcher()
matchers := []MatchFunc{}
st.Expect(t, len(matcher.Matchers), len(Matchers))
matcher.Set(matchers)
st.Expect(t, matcher.Matchers, matchers)
st.Expect(t, len(matcher.Get()), 0)
}
func TestMatcherGet(t *testing.T) {
matcher := NewMatcher()
matchers := []MatchFunc{}
matcher.Set(matchers)
st.Expect(t, matcher.Get(), matchers)
}
func TestMatcherFlush(t *testing.T) {
matcher := NewMatcher()
st.Expect(t, len(matcher.Matchers), len(Matchers))
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return true, nil
})
st.Expect(t, len(matcher.Get()), len(Matchers)+1)
matcher.Flush()
st.Expect(t, len(matcher.Get()), 0)
}
func TestMatcherClone(t *testing.T) {
matcher := DefaultMatcher.Clone()
st.Expect(t, len(matcher.Get()), len(DefaultMatcher.Get()))
}
func TestMatcher(t *testing.T) {
cases := []struct {
method string
url string
matches bool
}{
{"GET", "http://foo.com/bar", true},
{"GET", "http://foo.com/baz", true},
{"GET", "http://foo.com/foo", false},
{"POST", "http://foo.com/bar", false},
{"POST", "http://bar.com/bar", false},
{"GET", "http://foo.com", false},
}
matcher := NewMatcher()
matcher.Flush()
st.Expect(t, len(matcher.Matchers), 0)
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.Method == "GET", nil
})
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.URL.Host == "foo.com", nil
})
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.URL.Path == "/baz" || req.URL.Path == "/bar", nil
})
for _, test := range cases {
u, _ := url.Parse(test.url)
req := &http.Request{Method: test.method, URL: u}
matches, err := matcher.Match(req, nil)
st.Expect(t, err, nil)
st.Expect(t, matches, test.matches)
}
}
func TestMatchMock(t *testing.T) {
t.Skip("Flaky test")
cases := []struct {
method string
url string
matches bool
}{
{"GET", "http://foo.com/bar", true},
{"GET", "http://foo.com/baz", true},
{"GET", "http://foo.com/foo", false},
{"POST", "http://foo.com/bar", false},
{"POST", "http://bar.com/bar", false},
{"GET", "http://foo.com", false},
}
matcher := DefaultMatcher
matcher.Flush()
st.Expect(t, len(matcher.Matchers), 0)
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.Method == "GET", nil
})
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.URL.Host == "foo.com", nil
})
matcher.Add(func(req *http.Request, ereq *Request) (bool, error) {
return req.URL.Path == "/baz" || req.URL.Path == "/bar", nil
})
for _, test := range cases {
Flush()
mock := New(test.url).method(test.method, "").Mock
u, _ := url.Parse(test.url)
req := &http.Request{Method: test.method, URL: u}
match, err := MatchMock(req)
st.Expect(t, err, nil)
if test.matches {
st.Expect(t, match, mock)
} else {
st.Expect(t, match, nil)
}
}
DefaultMatcher.Matchers = Matchers
}
|