Skip to content

Commit

Permalink
HTTParty.baseuri should not modify the parameters.
Browse files Browse the repository at this point in the history
HTTParty.baseuri is modifying the passed parameters.  This can cause
unexpected "side-effects".  For example: if you pass a value from a
configuration hash, the value in the configuratiuon hash is changed.

Signed-off-by: John Nunemaker <nunemaker@gmail.com>
  • Loading branch information
Matt Scilipoti authored and jnunemaker committed Mar 30, 2009
1 parent 0b067f7 commit a9d89d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,14 @@ def process_cookies(options) #:nodoc:
end

def self.normalize_base_uri(url) #:nodoc:
use_ssl = (url =~ /^https/) || url.include?(':443')
ends_with_slash = url =~ /\/$/
normalized_url = url.dup
use_ssl = (normalized_url =~ /^https/) || normalized_url.include?(':443')
ends_with_slash = normalized_url =~ /\/$/

url.chop! if ends_with_slash
url.gsub!(/^https?:\/\//i, '')
normalized_url.chop! if ends_with_slash
normalized_url.gsub!(/^https?:\/\//i, '')

"http#{'s' if use_ssl}://#{url}"
"http#{'s' if use_ssl}://#{normalized_url}"
end

class Basement #:nodoc:
Expand Down
12 changes: 12 additions & 0 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
@klass.base_uri('http://api.foobar.com')
@klass.base_uri.should == 'http://api.foobar.com'
end

it 'should not modify the parameter during assignment' do
uri = 'http://api.foobar.com'
@klass.base_uri(uri)
uri.should == 'http://api.foobar.com'
end
end

describe "#normalize_base_uri" do
Expand All @@ -36,6 +42,12 @@
uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
uri.should == 'https://api.foo.com/v1:443'
end

it 'should not modify the parameter' do
uri = 'http://api.foobar.com'
HTTParty.normalize_base_uri(uri)
uri.should == 'http://api.foobar.com'
end
end

describe "headers" do
Expand Down

0 comments on commit a9d89d9

Please sign in to comment.