Skip to content

Commit

Permalink
Merge pull request #153 from dpiddy/excon-stub-url
Browse files Browse the repository at this point in the history
Excon.stub will break out items from :url if supplied.
  • Loading branch information
danp committed Oct 5, 2012
2 parents 6ad2e69 + 2104e33 commit acdff27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/excon.rb
Expand Up @@ -103,6 +103,20 @@ def new(url, params = {})
# @param [Hash<Symbol, >] request params to match against, omitted params match all
# @param [Hash<Symbol, >] response params to return from matched request or block to call with params
def stub(request_params, response_params = nil)
if url = request_params.delete(:url)
uri = URI.parse(url)
request_params.update(
:host => uri.host,
:path => uri.path,
:port => uri.port.to_s,
:query => uri.query,
:scheme => uri.scheme
)
if uri.user || uri.password
request_params[:headers] ||= {}
request_params[:headers]['Authorization'] ||= 'Basic ' << ['' << uri.user.to_s << ':' << uri.password.to_s].pack('m').delete(Excon::CR_NL)
end
end
if block_given?
if response_params
raise(ArgumentError.new("stub requires either response_params OR a block"))
Expand Down
10 changes: 10 additions & 0 deletions tests/stub_tests.rb
Expand Up @@ -148,6 +148,16 @@

Excon.stubs.clear

tests("stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})") do
Excon.stub({:url => 'https://user:pass@foo.bar.com:9999/baz?quux=true'}, {:status => 200})

tests("get(:expects => 200)") do
Excon.new("https://user:pass@foo.bar.com:9999/baz?quux=true", :mock => true).get(:expects => 200)
end
end

Excon.stubs.clear

tests("stub({}, {:status => 404, :body => 'Not Found'}") do

connection = Excon.new('http://127.0.0.1:9292', :mock => true)
Expand Down

0 comments on commit acdff27

Please sign in to comment.