1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Fixed that base_uri was not always being normalized. [#13 state:resolved]

This commit is contained in:
John Nunemaker 2008-12-01 09:44:30 -05:00
parent 82dd6159db
commit ab17bb371d
6 changed files with 44 additions and 29 deletions

View file

@ -1,3 +1,8 @@
== 0.1.8 2008-11-30
* 1 major enhancement
* Moved base_uri normalization into request class and out of httparty module, fixing
the problem where base_uri was not always being normalized.
== 0.1.7 2008-11-30
* 1 major enhancement
* fixed multiple class definitions overriding each others options

View file

@ -68,7 +68,7 @@ module HTTParty
def base_uri(uri=nil)
return default_options[:base_uri] unless uri
default_options[:base_uri] = normalize_base_uri(uri)
default_options[:base_uri] = uri
end
def basic_auth(u, p)
@ -112,13 +112,5 @@ module HTTParty
def perform_request(http_method, path, options) #:nodoc:
Request.new(http_method, path, default_options.merge(options)).perform
end
# Makes it so uri is sure to parse stuff like google.com without the http
def normalize_base_uri(url) #:nodoc:
use_ssl = (url =~ /^https/) || url.include?(':443')
url.chop! if url.ends_with?('/')
url.gsub!(/^https?:\/\//i, '')
"http#{'s' if use_ssl}://#{url}"
end
end
end

View file

@ -1,5 +1,13 @@
module HTTParty
class Request
# Makes it so uri is sure to parse stuff like google.com without the http
def self.normalize_base_uri(url) #:nodoc:
use_ssl = (url =~ /^https/) || url.include?(':443')
url.chop! if url.ends_with?('/')
url.gsub!(/^https?:\/\//i, '')
"http#{'s' if use_ssl}://#{url}"
end
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
attr_accessor :http_method, :path, :options
@ -18,6 +26,7 @@ module HTTParty
end
def uri
options[:base_uri] = self.class.normalize_base_uri(options[:base_uri]) unless options[:base_uri].nil?
uri = path.relative? ? URI.parse("#{options[:base_uri]}#{path}") : path
uri.query = query_string(uri)
uri

View file

@ -2,7 +2,7 @@ module HTTParty
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 7
TINY = 8
STRING = [MAJOR, MINOR, TINY].join('.')
end

View file

@ -5,6 +5,30 @@ describe HTTParty::Request do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml)
end
describe "#normalize_base_uri" do
it "should add http if not present for non ssl requests" do
uri = HTTParty::Request.normalize_base_uri('api.foobar.com')
uri.should == 'http://api.foobar.com'
end
it "should add https if not present for ssl requests" do
uri = HTTParty::Request.normalize_base_uri('api.foo.com/v1:443')
uri.should == 'https://api.foo.com/v1:443'
end
it "should not remove https for ssl requests" do
uri = HTTParty::Request.normalize_base_uri('https://api.foo.com/v1:443')
uri.should == 'https://api.foo.com/v1:443'
end
end
describe "uri" do
it "should be normalized" do
request = HTTParty::Request.new(Net::HTTP::Get, '', :base_uri => 'api.foo.com')
request.uri.to_s.should == 'http://api.foo.com'
end
end
describe 'http' do
it "should use ssl for port 443" do
@request.send(:http, URI.parse('https://api.foo.com/v1:443')).use_ssl?.should == true

View file

@ -25,28 +25,13 @@ describe HTTParty do
end
it "should have reader" do
Foo.base_uri.should == 'http://api.foo.com/v1'
Foo.base_uri.should == 'api.foo.com/v1'
end
it 'should have writer' do
Foo.base_uri('http://api.foobar.com')
Foo.base_uri.should == 'http://api.foobar.com'
end
it "should add http if not present for non ssl requests" do
Foo.base_uri('api.foobar.com')
Foo.base_uri.should == 'http://api.foobar.com'
end
it "should add https if not present for ssl requests" do
Foo.base_uri('api.foo.com/v1:443')
Foo.base_uri.should == 'https://api.foo.com/v1:443'
end
it "should not remove https for ssl requests" do
Foo.base_uri('https://api.foo.com/v1:443')
Foo.base_uri.should == 'https://api.foo.com/v1:443'
end
end
describe "headers" do
@ -127,8 +112,8 @@ describe HTTParty do
describe "with multiple class definitions" do
it "should not run over each others options" do
HRest.default_options.should == {:base_uri => 'http://hrest.com', :default_params => {:two => 'three'}}
GRest.default_options.should == {:base_uri => 'http://grest.com', :default_params => {:one => 'two'}}
HRest.default_options.should == {:base_uri => 'hrest.com', :default_params => {:two => 'three'}}
GRest.default_options.should == {:base_uri => 'grest.com', :default_params => {:one => 'two'}}
end
end
end