Skip to content

Commit

Permalink
add request Options
Browse files Browse the repository at this point in the history
  • Loading branch information
123hurray committed Apr 11, 2019
1 parent 090e9d9 commit 9ba4934
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion _examples/reply_error/reply_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net/http"
"testing"

"github.com/h2non/gock"
"github.com/nbio/st"
"gopkg.in/h2non/gock.v1"
)

func TestReplyError(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func MatchHost(req *http.Request, ereq *Request) (bool, error) {
if strings.EqualFold(url.Host, req.URL.Host) {
return true, nil
}
return regexp.MatchString(url.Host, req.URL.Host)
if !ereq.Options.DisableRegexpHost {
return regexp.MatchString(url.Host, req.URL.Host)
}
return false, nil
}

// MatchPath matches the HTTP URL path of the given request.
Expand Down
30 changes: 18 additions & 12 deletions matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ func TestMatchScheme(t *testing.T) {

func TestMatchHost(t *testing.T) {
cases := []struct {
value string
url string
matches bool
value string
url string
matches bool
matchesNonRegexp bool
}{
{"foo.com", "foo.com", true},
{"FOO.com", "foo.com", true},
{"foo.net", "foo.com", false},
{"foo", "foo.com", true},
{"(.*).com", "foo.com", true},
{"127.0.0.1", "127.0.0.1", true},
{"127.0.0.2", "127.0.0.1", false},
{"127.0.0.*", "127.0.0.1", true},
{"127.0.0.[0-9]", "127.0.0.7", true},
{"foo.com", "foo.com", true, true},
{"FOO.com", "foo.com", true, true},
{"foo.net", "foo.com", false, false},
{"foo.bar.net", "foo-bar.net", true, false},
{"foo", "foo.com", true, false},
{"(.*).com", "foo.com", true, false},
{"127.0.0.1", "127.0.0.1", true, true},
{"127.0.0.2", "127.0.0.1", false, false},
{"127.0.0.*", "127.0.0.1", true, false},
{"127.0.0.[0-9]", "127.0.0.7", true, false},
}

for _, test := range cases {
Expand All @@ -75,6 +77,10 @@ func TestMatchHost(t *testing.T) {
matches, err := MatchHost(req, ereq)
st.Expect(t, err, nil)
st.Expect(t, matches, test.matches)
ereq.WithOptions(Options{DisableRegexpHost: true})
matches, err = MatchHost(req, ereq)
st.Expect(t, err, nil)
st.Expect(t, matches, test.matchesNonRegexp)
}
}

Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gock

// Options represents customized option for gock
type Options struct {
// DisableRegexpHost stores if the host is only a plain string rather than regular expression,
// if DisableRegexpHost is true, host sets in gock.New(...) will be treated as plain string
DisableRegexpHost bool
}
9 changes: 9 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Request struct {
// Persisted stores if the current mock should be always active.
Persisted bool

// Options stores options for current Request.
Options Options

// URLStruct stores the parsed URL as *url.URL struct.
URLStruct *url.URL

Expand Down Expand Up @@ -251,6 +254,12 @@ func (r *Request) Persist() *Request {
return r
}

// WithOptions sets the options for the request.
func (r *Request) WithOptions(options Options) *Request {
r.Options = options
return r
}

// Times defines the number of times that the current HTTP mock should remain active.
func (r *Request) Times(num int) *Request {
r.Counter = num
Expand Down

0 comments on commit 9ba4934

Please sign in to comment.