Skip to content

Commit 30a20be

Browse files
123hurrayh2non
authored andcommitted
add request Options (#49)
1 parent 308f12f commit 30a20be

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

_examples/reply_error/reply_error_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"net/http"
66
"testing"
77

8-
"github.com/h2non/gock"
98
"github.com/nbio/st"
9+
"gopkg.in/h2non/gock.v1"
1010
)
1111

1212
func TestReplyError(t *testing.T) {

matchers.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ func MatchHost(req *http.Request, ereq *Request) (bool, error) {
5858
if strings.EqualFold(url.Host, req.URL.Host) {
5959
return true, nil
6060
}
61-
return regexp.MatchString(url.Host, req.URL.Host)
61+
if !ereq.Options.DisableRegexpHost {
62+
return regexp.MatchString(url.Host, req.URL.Host)
63+
}
64+
return false, nil
6265
}
6366

6467
// MatchPath matches the HTTP URL path of the given request.

matchers_test.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ func TestMatchScheme(t *testing.T) {
5454

5555
func TestMatchHost(t *testing.T) {
5656
cases := []struct {
57-
value string
58-
url string
59-
matches bool
57+
value string
58+
url string
59+
matches bool
60+
matchesNonRegexp bool
6061
}{
61-
{"foo.com", "foo.com", true},
62-
{"FOO.com", "foo.com", true},
63-
{"foo.net", "foo.com", false},
64-
{"foo", "foo.com", true},
65-
{"(.*).com", "foo.com", true},
66-
{"127.0.0.1", "127.0.0.1", true},
67-
{"127.0.0.2", "127.0.0.1", false},
68-
{"127.0.0.*", "127.0.0.1", true},
69-
{"127.0.0.[0-9]", "127.0.0.7", true},
62+
{"foo.com", "foo.com", true, true},
63+
{"FOO.com", "foo.com", true, true},
64+
{"foo.net", "foo.com", false, false},
65+
{"foo.bar.net", "foo-bar.net", true, false},
66+
{"foo", "foo.com", true, false},
67+
{"(.*).com", "foo.com", true, false},
68+
{"127.0.0.1", "127.0.0.1", true, true},
69+
{"127.0.0.2", "127.0.0.1", false, false},
70+
{"127.0.0.*", "127.0.0.1", true, false},
71+
{"127.0.0.[0-9]", "127.0.0.7", true, false},
7072
}
7173

7274
for _, test := range cases {
@@ -75,6 +77,10 @@ func TestMatchHost(t *testing.T) {
7577
matches, err := MatchHost(req, ereq)
7678
st.Expect(t, err, nil)
7779
st.Expect(t, matches, test.matches)
80+
ereq.WithOptions(Options{DisableRegexpHost: true})
81+
matches, err = MatchHost(req, ereq)
82+
st.Expect(t, err, nil)
83+
st.Expect(t, matches, test.matchesNonRegexp)
7884
}
7985
}
8086

options.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package gock
2+
3+
// Options represents customized option for gock
4+
type Options struct {
5+
// DisableRegexpHost stores if the host is only a plain string rather than regular expression,
6+
// if DisableRegexpHost is true, host sets in gock.New(...) will be treated as plain string
7+
DisableRegexpHost bool
8+
}

request.go

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Request struct {
3333
// Persisted stores if the current mock should be always active.
3434
Persisted bool
3535

36+
// Options stores options for current Request.
37+
Options Options
38+
3639
// URLStruct stores the parsed URL as *url.URL struct.
3740
URLStruct *url.URL
3841

@@ -251,6 +254,12 @@ func (r *Request) Persist() *Request {
251254
return r
252255
}
253256

257+
// WithOptions sets the options for the request.
258+
func (r *Request) WithOptions(options Options) *Request {
259+
r.Options = options
260+
return r
261+
}
262+
254263
// Times defines the number of times that the current HTTP mock should remain active.
255264
func (r *Request) Times(num int) *Request {
256265
r.Counter = num

0 commit comments

Comments
 (0)