Permalink
Browse files

Encode get params in Easy#params= instad of in Hydra#get_easy_object

  • Loading branch information...
1 parent dd300a7 commit 09d8c6f465c920a8000782f339e8dd0cfb96d59b @whoahbot committed Jun 15, 2012
Showing with 14 additions and 11 deletions.
  1. +2 −2 lib/typhoeus/hydra.rb
  2. +7 −4 lib/typhoeus/request.rb
  3. +1 −1 spec/typhoeus/hydra_spec.rb
  4. +4 −4 spec/typhoeus/request_spec.rb
View
@@ -162,13 +162,13 @@ def get_easy_object(request)
easy.url = request.url
easy.method = request.method
- easy.params = request.params unless request.params.nil?
+ easy.params = request.params unless request.method == :get || request.params.nil?
if request.headers
easy.headers = {'User-Agent' => Typhoeus::USER_AGENT}.merge(request.headers)
else
easy.headers = {'User-Agent' => Typhoeus::USER_AGENT}
end
- easy.request_body = request.body unless request.method == :get || request.body.nil?
+ easy.request_body = request.body unless request.body.nil?
easy.timeout = request.timeout if request.timeout
easy.connect_timeout = request.connect_timeout if request.connect_timeout
easy.interface = request.interface if request.interface
View
@@ -117,12 +117,15 @@ def user_agent
end
def url
- if [:post, :put, :delete].include?(@method)
- @url
- else
+ if @method == :get
url = "#{@url}?#{params_string}"
- url += "&#{URI.escape(@body)}" if @body
+ if @body
+ url += "&#{URI.escape(@body)}"
+ @body = nil
+ end
url.gsub("?&", "?").gsub(/\?$/, '')
+ else
+ @url
end
end
@@ -41,7 +41,7 @@ def set(key, object, timeout = 0)
end
context "when params are supplied" do
- [:post, :put, :delete, :get, :head, :patch, :options, :trace, :connect].each do |method|
+ [:post, :put, :delete, :head, :patch, :options, :trace, :connect].each do |method|
it "should not delete the params if the request is a #{method.upcase}" do
request = Typhoeus::Request.new("fubar", :method => method, :params => {:coffee => 'black'})
hydra.send(:get_easy_object, request).params.should == {:coffee => 'black'}
@@ -408,13 +408,13 @@
describe "#url" do
context "for a GET request" do
it "should encode the body of the request as URL params if they are supplied" do
- Typhoeus::Request.new("http://google.com", {:method => :get, :body => "a=b"}).url.
- should == "http://google.com?a=b"
+ Typhoeus::Request.new("http://google.com", {:method => :get, :params => {'c' => 9}, :body => "a=b"}).url.
+ should == "http://google.com?c=9&a=b"
end
end
- context "for a PUT, POST or DELETE request" do
- [:put, :post, :delete].each do |method|
+ context "for all other requests" do
+ [:put, :post, :delete, :patch].each do |method|
it "should not encode the body of the request as URL params when the method is #{method.upcase}" do
Typhoeus::Request.new("http://google.com", {:method => method, :body => "a=b"}).url.
should_not match /\?a=b/

0 comments on commit 09d8c6f

Please sign in to comment.