Skip to content

Commit

Permalink
Dispatchers now correctly handle query strings. [#7 status:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Mar 27, 2009
1 parent 5a99a1c commit 7b8663b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/twitter_auth/dispatcher/basic.rb
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/twitter_auth/dispatcher/oauth.rb
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions 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
Expand Down
26 changes: 26 additions & 0 deletions 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

0 comments on commit 7b8663b

Please sign in to comment.