Permalink
Browse files

Allow Session.base_url to be overriden for specific requests

Uses URI.join to put together the base_url and the request url. Under the
hood, this does the right thing:

    URI.join("http://example.com:123", "http://foo.example.com:1234/foo/bar").to_s
    #=> "http://foo.example.com:1234/foo/bar"

    URI.join("http://example.com:123", "/foo/bar").to_s
    #=> "http://example.com:123/foo/bar"
  • Loading branch information...
1 parent 411cd7c commit 9bbca67cb7ef2de3e0ce0e08770cac2126b821ec @jwilger jwilger committed Mar 26, 2012
Showing with 9 additions and 3 deletions.
  1. +4 −3 lib/patron/session.rb
  2. +5 −0 spec/session_spec.rb
View
@@ -210,14 +210,15 @@ def request(action, url, headers, options = {})
req.upload_data = options[:data]
req.file_name = options[:file]
- url = self.base_url.to_s + url.to_s
- uri = URI.parse(url)
+ base_url = self.base_url.to_s
+ url = url.to_s
+ raise ArgumentError, "Empty URL" if base_url.empty? && url.empty?
+ uri = URI.join(base_url, url)
query = uri.query.to_s.split('&')
query += options[:query].is_a?(Hash) ? Util.build_query_pairs_from_hash(options[:query]) : options[:query].to_s.split('&')
uri.query = query.join('&')
uri.query = nil if uri.query.empty?
url = uri.to_s
- raise ArgumentError, "Empty URL" if url.empty?
req.url = url
handle_request(req)
View
@@ -56,6 +56,11 @@
body.request_method.should == "GET"
end
+ it 'should ignore #base_url when a full URL is provided' do
+ @session.base_url = "http://example.com:123"
+ lambda { @session.get("http://localhost:9001/test") }.should_not raise_error(URI::InvalidURIError)
+ end
+
it "should download content with :get and a file path" do
tmpfile = "/tmp/patron_test.yaml"
response = @session.get_file "/test", tmpfile

0 comments on commit 9bbca67

Please sign in to comment.