Skip to content

Commit

Permalink
fix unstub for regex params
Browse files Browse the repository at this point in the history
closes #815
  • Loading branch information
geemus committed Feb 3, 2023
1 parent 462b62e commit 3bc5e42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/excon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ def stub_for(request_params={})
headers_match = !stub.has_key?(:headers) || stub[:headers].keys.all? do |key|
case value = stub[:headers][key]
when Regexp
if (match = value.match(request_params[:headers][key]))
captures[:headers][key] = match.captures
case request_params[:headers][key]
when String
if (match = value.match(request_params[:headers][key]))
captures[:headers][key] = match.captures
end
when Regexp # for unstub on regex params
match = (value == request_params[:headers][key])
end
match
else
Expand All @@ -209,8 +214,13 @@ def stub_for(request_params={})
non_headers_match = (stub.keys - [:headers]).all? do |key|
case value = stub[key]
when Regexp
if (match = value.match(request_params[key]))
captures[key] = match.captures
case request_params[key]
when String
if (match = value.match(request_params[key]))
captures[key] = match.captures
end
when Regexp # for unstub on regex params
match = (value == request_params[key])
end
match
else
Expand Down
14 changes: 14 additions & 0 deletions tests/middlewares/mock_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@
Excon.stubs.clear
end

tests("unstub({path: %r{/path}})") do
connection = nil

tests("unstub({path: %r{/path}})").returns([{path: %r{/path}, captures: {headers: {}}}, {}]) do
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
request_params, response_params = { path: %r{/path} }, {}
Excon.stub(request_params, response_params)

Excon.unstub(request_params)
end

Excon.stubs.clear
end

tests("global stubs") do
connection = Excon.new('http://127.0.0.1:9292', :mock => true)
Excon.stub({}, {:body => '1'})
Expand Down

0 comments on commit 3bc5e42

Please sign in to comment.