Permalink
Browse files

Fix excon adapter to handle query specified as a hash.

Closes #73.
  • Loading branch information...
1 parent b604625 commit a2169840520d4d847bc9d1015fbc229bbb6b94a4 @myronmarston committed Jul 3, 2011
View
@@ -3,6 +3,7 @@
[Full Changelog](http://github.com/myronmarston/vcr/compare/v1.10.0...master)
* Fix typo in error message [Bradley](https://github.com/bradleyisotope).
+* Fix excon adapter to properly handle queries specified as a hash.
## 1.10.0 (May 18, 2011)
@@ -153,10 +153,27 @@ def record_interaction
end
def uri
- @uri ||= begin
- uri = "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}"
- uri << "?#{params[:query]}" if params[:query]
- uri
+ @uri ||= "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}#{query}"
+ end
+
+ def query
+ @query ||= case params[:query]
+ when String
+ "?#{params[:query]}"
+ when Hash
+ qry = '?'
+ for key, values in params[:query]
+ if values.nil?
+ qry << key.to_s << '&'
+ else
+ for value in [*values]
+ qry << key.to_s << '=' << CGI.escape(value.to_s) << '&'
+ end
+ end
+ end
+ qry.chop! # remove trailing '&'
+ else
+ ''
end
end
@@ -6,6 +6,10 @@ class SinatraApp < ::Sinatra::Base
"GET to root"
end
+ get '/search' do
+ "query: #{params[:q]}"
+ end
+
get '/localhost_test' do
"Localhost response"
end
@@ -19,5 +19,21 @@
Excon::VERSION = version
end
end
+
+ context "when the query is specified as a hash option" do
+ let(:excon) { ::Excon.new("http://localhost:#{VCR::SinatraApp.port}/search") }
+
+ it 'properly records and plays back the response' do
+ described_class.http_connections_allowed = true
+ recorded, played_back = [1, 2].map do
+ VCR.use_cassette('excon_query', :record => :once) do
+ excon.request(:method => :get, :query => { :q => 'Tolkien' }).body
+ end
+ end
+
+ recorded.should == played_back
+ recorded.should == 'query: Tolkien'
+ end
+ end
end

0 comments on commit a216984

Please sign in to comment.