-
Notifications
You must be signed in to change notification settings - Fork 2
/
common_test.go
40 lines (35 loc) · 1.13 KB
/
common_test.go
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
package gock_test
import (
"errors"
"net/http"
"net/url"
"github.com/h2non/gock"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var errAny = errors.New("any error")
// NewFooMatcher creates a special foo matcher.
func NewFooMatcher() *gock.MockMatcher {
matcher := gock.NewEmptyMatcher()
matcher.Add(func(req *http.Request, _ *gock.Request) (bool, error) {
if req.URL.Scheme == "https" {
return true, errAny
}
return true, nil
})
matcher.Add(func(req *http.Request, _ *gock.Request) (bool, error) {
return req.URL.Host == "foo.com", nil
})
matcher.Add(func(req *http.Request, _ *gock.Request) (bool, error) {
return req.URL.Path == "/baz" || req.URL.Path == "/bar", nil
})
return matcher
}
// NewRoundTripperError creates the same round trip error usually returned in
// case of a transport error. This method is used for validating tests that are
// replacing the transport against the error `RoundTripper` via
// `NewErrorRoundTripper`.
func NewRoundTripperError(method string, _url string, err error) error {
op := cases.Title(language.Und).String(method)
return &url.Error{Op: op, URL: _url, Err: err}
}