diff --git a/lib/twitter_auth/dispatcher/basic.rb b/lib/twitter_auth/dispatcher/basic.rb index bcfbb08..749321d 100644 --- a/lib/twitter_auth/dispatcher/basic.rb +++ b/lib/twitter_auth/dispatcher/basic.rb @@ -13,7 +13,7 @@ def initialize(user) end def request(http_method, path, body=nil, *arguments) - path << '.json' unless path.match(/\.(:?xml|json)\z/i) + path = append_extension_to(path) response = TwitterAuth.net.start{ |http| req = "Net::HTTP::#{http_method.to_s.capitalize}".constantize.new(path, *arguments) diff --git a/lib/twitter_auth/dispatcher/oauth.rb b/lib/twitter_auth/dispatcher/oauth.rb index ca18742..859d2ae 100644 --- a/lib/twitter_auth/dispatcher/oauth.rb +++ b/lib/twitter_auth/dispatcher/oauth.rb @@ -14,7 +14,8 @@ def initialize(user) end def request(http_method, path, *arguments) - path << '.json' unless path.match(/\.(:?xml|json)\z/i) + path = append_extension_to(path) + response = super handle_response(response) diff --git a/lib/twitter_auth/dispatcher/shared.rb b/lib/twitter_auth/dispatcher/shared.rb index 4bc36a2..a777c3e 100644 --- a/lib/twitter_auth/dispatcher/shared.rb +++ b/lib/twitter_auth/dispatcher/shared.rb @@ -1,6 +1,12 @@ module TwitterAuth module Dispatcher module Shared + def append_extension_to(path) + path, query_string = *(path.split("?")) + path << '.json' unless path.match(/\.(:?xml|json)\z/i) + "#{path}#{"?#{query_string}" if query_string}" + end + def handle_response(response) case response when Net::HTTPOK diff --git a/spec/twitter_auth/dispatcher/shared_spec.rb b/spec/twitter_auth/dispatcher/shared_spec.rb new file mode 100644 index 0000000..af31555 --- /dev/null +++ b/spec/twitter_auth/dispatcher/shared_spec.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe TwitterAuth::Dispatcher::Shared do + include TwitterAuth::Dispatcher::Shared + + describe '#append_extension_to' do + it 'should leave extensions alone if they exist' do + append_extension_to('/fake.json').should == '/fake.json' + append_extension_to('/fake.xml').should == '/fake.xml' + end + + it 'should append .json if no extension is provided' do + append_extension_to('/fake').should == '/fake.json' + append_extension_to('/verify/fake').should == '/verify/fake.json' + end + + it 'should leave extensions alone even with query strings' do + append_extension_to('/fake.json?since_id=123').should == '/fake.json?since_id=123' + append_extension_to('/fake.xml?since_id=123').should == '/fake.xml?since_id=123' + end + + it 'should add an extension even with query strings' do + append_extension_to('/fake?since_id=123').should == '/fake.json?since_id=123' + end + end +end