Skip to content

Commit

Permalink
When checking options also consider Request#options.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshasselberg committed Nov 20, 2012
1 parent 394421e commit 093422e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 98 deletions.
9 changes: 7 additions & 2 deletions lib/typhoeus/expectation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ def and_return(response)
#
# @api private
def matches?(request)
url_match?(request.url) &&
(options ? options.all?{ |k,v| request.original_options[k] == v } : true)
url_match?(request.url) && options_match?(request)
end

# Return canned responses.
Expand Down Expand Up @@ -152,6 +151,12 @@ def response

private

# Check wether the options matches the request options.
# I checks options and original options.
def options_match?(request)
(options ? options.all?{ |k,v| request.original_options[k] == v || request.options[k] == v } : true)
end

# Check wether the url matches the request url.
# The url can be a string, regex or nil. String and
# regexp were checked, nil is always true. Else false.
Expand Down
191 changes: 95 additions & 96 deletions spec/typhoeus/expectation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,124 +115,123 @@
end
end

describe "#matches" do
let(:request_url) { "www.example.com" }
let(:request_options) { {} }
let(:request) { Typhoeus::Request.new(request_url, request_options) }

context "when url" do
context "when string" do
context "when match" do
it "returns true" do
expect(expectation.matches?(request)).to be_true
end

context "when options" do
context "when match" do
let(:options) { { :a => 1 } }
let(:request_options) { options }

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end

context "when options are a subset from request_options" do
let(:options) { { :a => 1 } }
let(:request_options) { { :a => 1, :b => 2 } }

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end

context "when options are nested" do
let(:options) { { :a => { :b => 1 } } }
let(:request_options) { options }

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end

context "when options contains an array" do
let(:options) { { :a => [1, 2] } }
let(:request_options) { options }

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end

context "when no match" do
let(:options) { { :a => 1 } }

it "returns false" do
expect(expectation.matches?(request)).to be_false
end
end
end
end
describe "#matches?" do
let(:request) { stub(url: nil) }

context "when regexp" do
context "when match" do
let(:url) { /example/ }
it "calls url_match?" do
expectation.should_receive(:url_match?)
expectation.matches?(request)
end

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end
it "calls options_match?" do
expectation.should_receive(:url_match?).and_return(true)
expectation.should_receive(:options_match?)
expectation.matches?(request)
end
end

context "when no match" do
let(:url) { /nomatch/ }
describe "#url_match?" do
let(:request_url) { "www.example.com" }
let(:request) { Typhoeus::Request.new(request_url) }
let(:url_match) { expectation.method(:url_match?).call(request.url) }

it "returns false" do
expect(expectation.matches?(request)).to be_false
end
end
context "when string" do
context "when match" do
it "returns true" do
expect(url_match).to be_true
end
end

context "when no match" do
let(:request_url) { "www.different.com" }
let(:url) { "no_match" }

it "returns false" do
expect(expectation.matches?(request)).to be_false
expect(url_match).to be_false
end
end
end

context "when options" do
context "when match" do
let(:options) { { :a => 1 } }
let(:request_options) { options }
context "when regexp" do
context "when match" do
let(:url) { /example/ }

it "returns false" do
expect(expectation.matches?(request)).to be_false
end
end
it "returns true" do
expect(url_match).to be_true
end
end

context "when no match" do
let(:url) { /nomatch/ }

it "returns false" do
expect(url_match).to be_false
end
end
end

context "when no url" do
context "when nil" do
let(:url) { nil }

context "when options" do
context "when match" do
let(:options) { { :a => 1 } }
let(:request_options) { options }
it "returns true" do
expect(url_match).to be_true
end
end

it "returns true" do
expect(expectation.matches?(request)).to be_true
end
end
context "when not string, regexp, nil" do
let(:url) { 1 }

context "when no match" do
let(:options) { { :a => 1 } }
it "returns false" do
expect(url_match).to be_false
end
end
end

it "returns false" do
expect(expectation.matches?(request)).to be_false
end
end
describe "options_match?" do
let(:request_options) { {} }
let(:request) { Typhoeus::Request.new(nil, request_options) }
let(:options_match) { expectation.method(:options_match?).call(request) }

context "when match" do
let(:options) { { :a => 1 } }
let(:request_options) { options }

it "returns true" do
expect(options_match).to be_true
end
end

context "when options are a subset from request_options" do
let(:options) { { :a => 1 } }
let(:request_options) { { :a => 1, :b => 2 } }

it "returns true" do
expect(options_match).to be_true
end
end

context "when options are nested" do
let(:options) { { :a => { :b => 1 } } }
let(:request_options) { options }

it "returns true" do
expect(options_match).to be_true
end
end

context "when options contains an array" do
let(:options) { { :a => [1, 2] } }
let(:request_options) { options }

it "returns true" do
expect(options_match).to be_true
end
end

context "when no match" do
let(:options) { { :a => 1 } }

it "returns false" do
expect(options_match).to be_false
end
end
end
Expand Down

0 comments on commit 093422e

Please sign in to comment.