spec/unit/request_pattern_spec.rb in webmock-3.8.3 vs spec/unit/request_pattern_spec.rb in webmock-3.9.0

- old
+ new

@@ -109,10 +109,20 @@ it "should match if uri regexp pattern matches request uri" do expect(WebMock::RequestPattern.new(:get, /.*example.*/)). to match(WebMock::RequestSignature.new(:get, "www.example.com")) end + it "should match if uri proc pattern returning true" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true })). + to match(WebMock::RequestSignature.new(:get, "www.example.com")) + end + + it "should not match if uri proc pattern returns false" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { false })). + not_to match(WebMock::RequestSignature.new(:get, "www.example.com")) + end + it "should match if uri Addressable::Template pattern matches unescaped form of request uri" do expect(WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com/{any_path}"))). to match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path")) end @@ -130,10 +140,16 @@ signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000/1234") uri = Addressable::Template.new("127.0.0.1:3000/{id}") expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) end + it "should match if Addressable::Template pattern that has ip address host without port matches request uri" do + signature = WebMock::RequestSignature.new(:get, "127.0.0.1/1234") + uri = Addressable::Template.new("127.0.0.1/{id}") + expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) + end + it "should match if Addressable::Template pattern host matches request uri" do signature = WebMock::RequestSignature.new(:get, "www.example.com") uri = Addressable::Template.new("{subdomain}.example.com") expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) end @@ -142,10 +158,16 @@ signature = WebMock::RequestSignature.new(:get, "www.bad-example.com") uri = Addressable::Template.new("{subdomain}.example.com") expect(WebMock::RequestPattern.new(:get, uri)).not_to match(signature) end + it "should match if uri Addressable::Template pattern matches request uri without a schema and a path " do + signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000") + uri = Addressable::Template.new("127.0.0.1:3000") + expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) + end + it "should match for uris with same parameters as pattern" do expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")). to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2")) end @@ -234,9 +256,45 @@ to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) end it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do expect(WebMock::RequestPattern.new(:get, /.*example.*/, + query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))). + not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) + end + end + + describe "when uri is described as a proc" do + it "should match request query params" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"a" => ["b", "c"]})). + to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c")) + end + + it "should match request query params if params don't match" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"x" => ["b", "c"]})). + not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c")) + end + + it "should match when query params are declared as HashIncluding matcher matching params" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, + query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))). + to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) + end + + it "should not match when query params are declared as HashIncluding matcher not matching params" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, + query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))). + not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) + end + + it "should match when query params are declared as RSpec HashIncluding matcher matching params" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, + query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))). + to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) + end + + it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do + expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))). not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1")) end end