Skip to content

Commit

Permalink
Add specs for Client class
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 31, 2011
1 parent 00e94d7 commit da3455c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/twitter/connection.rb
Expand Up @@ -26,7 +26,7 @@ def connection(options={})
:ssl => {:verify => false},
:url => options.fetch(:endpoint, endpoint),
}
Faraday.new(connection_options.deep_merge(default_options)) do |builder|
Faraday.new(default_options.deep_merge(connection_options)) do |builder|
builder.use Twitter::Request::Phoenix if options[:phoenix]
builder.use Twitter::Request::MultipartWithFile
builder.use Twitter::Request::TwitterOAuth, credentials if credentials?
Expand Down
87 changes: 80 additions & 7 deletions spec/twitter/client_spec.rb
@@ -1,21 +1,94 @@
require 'helper'

describe Twitter::Client do
it "should connect using the endpoint configuration" do
client = Twitter::Client.new
endpoint = URI.parse(client.endpoint).to_s.gsub(/\/$/, '')
connection = client.send(:connection).build_url(nil).to_s
connection.should == endpoint
before do
@keys = Twitter::Config::VALID_OPTIONS_KEYS
end

context "with module configuration" do

before do
Twitter.configure do |config|
@keys.each do |key|
config.send("#{key}=", key)
end
end
end

after do
Twitter.reset
end

it "should inherit module configuration" do
api = Twitter::Client.new
@keys.each do |key|
api.send(key).should == key
end
end

context "with class configuration" do

before do
@configuration = {
:consumer_key => 'CK',
:consumer_secret => 'CS',
:oauth_token => 'OT',
:oauth_token_secret => 'OS',
:adapter => :typhoeus,
:endpoint => 'http://tumblr.com/',
:gateway => 'apigee-1111.apigee.com',
:proxy => 'http://erik:sekret@proxy.example.com:8080',
:search_endpoint => 'http://google.com/',
:media_endpoint => 'https://upload.twitter.com/',
:user_agent => 'Custom User Agent',
:connection_options => {:timeout => 10},
}
end

context "during initialization" do
it "should override module configuration" do
api = Twitter::Client.new(@configuration)
@keys.each do |key|
api.send(key).should == @configuration[key]
end
end
end

context "after initilization" do
it "should override module configuration after initialization" do
api = Twitter::Client.new
@configuration.each do |key, value|
api.send("#{key}=", value)
end
@keys.each do |key|
api.send(key).should == @configuration[key]
end
end
end

end
end

it "should not cache the screen name across clients" do
stub_get("/1/account/verify_credentials.json").
to_return(:body => fixture("sferik.json"), :headers => {:content_type => "application/json; charset=utf-8"})
client1 = Twitter::Client.new(:oauth_token => 'ot1', :oauth_token_secret => 'ots1')
client1 = Twitter::Client.new
client1.current_user.screen_name.should == 'sferik'
stub_get("/1/account/verify_credentials.json").
to_return(:body => fixture("pengwynn.json"), :headers => {:content_type => "application/json; charset=utf-8"})
client2 = Twitter::Client.new(:oauth_token => 'ot2', :oauth_token_secret => 'ots2')
client2 = Twitter::Client.new
client2.current_user.screen_name.should == 'pengwynn'
end

it "should recursively merge connection options" do
stub_get("/1/statuses/user_timeline.json").
with(:query => {:screen_name => "sferik"}, :headers => {"User-Agent" => "Custom User Agent"}).
to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
client = Twitter::Client.new(:connection_options => {:headers => {:user_agent => 'Custom User Agent'}})
client.user_timeline("sferik")
a_get("/1/statuses/user_timeline.json").
with(:query => {:screen_name => "sferik"}, :headers => {"User-Agent" => "Custom User Agent"}).
should have_been_made
end

end
1 change: 1 addition & 0 deletions spec/twitter_spec.rb
Expand Up @@ -87,4 +87,5 @@
end
end
end

end

0 comments on commit da3455c

Please sign in to comment.