1 require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3 describe "Twitter::Client" do
5 @init_hash = { :login => 'user', :password => 'pass' }
8 it ".new should accept login and password as initializer hash keys and set the values to instance values" do
11 client = Twitter::Client.new(@init_hash)
12 end.should_not raise_error
13 client.send(:login).should eql(@init_hash[:login])
14 client.send(:password).should eql(@init_hash[:password])
18 describe Twitter::Client, "#inspect" do
20 @client = Twitter::Client.new(:login => "NippleEquality", :password => "3rdnipple")
23 it "should block out password attribute values" do
24 @client.inspect.should_not match(/@password="3rdnipple"/)
28 describe Twitter::Client, "#http_header" do
31 @application_name = @user_agent
32 @application_version = '1.2.3'
33 @application_url = 'http://myapp.url'
34 Twitter::Client.configure do |conf|
35 conf.user_agent = @user_agent
36 conf.application_name = @application_name
37 conf.application_version = @application_version
38 conf.application_url = @application_url
41 'Accept' => 'text/x-json',
42 'X-Twitter-Client' => @application_name,
43 'X-Twitter-Client-Version' => @application_version,
44 'X-Twitter-Client-URL' => @application_url,
45 'User-Agent' => "Twitter4R v#{Twitter::Version.to_version} [#{@user_agent}]",
47 @twitter = client_context
48 # reset @@http_header class variable in Twitter::Client class
49 Twitter::Client.class_eval("@@http_header = nil")
52 it "should always return expected HTTP headers" do
53 headers = @twitter.send(:http_header)
54 headers.should === @expected_headers
57 it "should cache HTTP headers Hash in class variable after first invocation" do
58 cache = Twitter::Client.class_eval("@@http_header")
60 @twitter.send(:http_header)
61 cache = Twitter::Client.class_eval("@@http_header")
62 cache.should_not be_nil
63 cache.should === @expected_headers
67 nilize(@user_agent, @application_name, @application_version, @application_url, @twitter, @expected_headers)
71 describe Twitter::Client, "#create_http_get_request" do
74 @expected_get_request = mock(Net::HTTP::Get)
75 @twitter = client_context
76 @default_header = @twitter.send(:http_header)
79 it "should create new Net::HTTP::Get object with expected initialization arguments" do
80 Net::HTTP::Get.should_receive(:new).with(@uri, @default_header).and_return(@expected_get_request)
81 @twitter.send(:create_http_get_request, @uri)
85 nilize(@twitter, @uri, @expected_get_request, @default_header)
89 describe Twitter::Client, "#create_http_post_request" do
92 @expected_post_request = mock(Net::HTTP::Post)
93 @twitter = client_context
94 @default_header = @twitter.send(:http_header)
97 it "should create new Net::HTTP::Post object with expected initialization arguments" do
98 Net::HTTP::Post.should_receive(:new).with(@uri, @default_header).and_return(@expected_post_request)
99 @twitter.send(:create_http_post_request, @uri)
103 nilize(@twitter, @uri, @expected_post_request, @default_header)
107 describe Twitter::Client, "#create_http_delete_request" do
109 @uri = '/a/stupid/path/that/is/not/restful/since/twitter.com/cannot/do/consistent/restful/apis'
110 @expected_delete_request = mock(Net::HTTP::Delete)
111 @twitter = client_context
112 @default_header = @twitter.send(:http_header)
115 it "should create new Net::HTTP::Delete object with expected initialization arguments" do
116 Net::HTTP::Delete.should_receive(:new).with(@uri, @default_header).and_return(@expected_delete_request)
117 @twitter.send(:create_http_delete_request, @uri)
121 nilize(@twitter, @uri, @expected_delete_request, @default_header)
125 describe Twitter::Client, "#http_connect" do
127 @request = mas_net_http_get(:basic_auth => nil)
128 @good_response = mas_net_http_response(:success)
129 @bad_response = mas_net_http_response(:server_error)
130 @http_stubs = {:is_a? => true}
131 @block = Proc.new do |conn|
132 conn.is_a?(Net::HTTP).should be(true)
136 @twitter = client_context
140 def generate_bad_response
141 @http = mas_net_http(@bad_response, @http_stubs)
142 Net::HTTP.stub!(:new).and_return(@http)
145 def generate_good_response
146 @http = mas_net_http(@good_response, @http_stubs)
147 Net::HTTP.stub!(:new).and_return(@http)
150 it "should yield HTTP connection when response is good" do
151 generate_good_response
152 @http.should_receive(:is_a?).with(Net::HTTP).and_return(true)
154 @twitter.send(:http_connect, &@block)
155 end.should_not raise_error
156 @has_yielded.should be(true)
159 it "should yield HTTP connection when response is bad" do
160 generate_bad_response
161 @http.should_receive(:is_a?).with(Net::HTTP).and_return(true)
163 @twitter.send(:http_connect, &@block)
164 }.should raise_error(Twitter::RESTError)
165 @has_yielded.should be(true)
169 nilize(@good_response, @bad_response, @http)
173 describe Twitter::Client, "#bless_model" do
175 @twitter = client_context
176 @model = Twitter::User.new
179 it "should recieve #client= message on given model to self" do
180 @model.should_receive(:client=).with(@twitter)
181 model = @twitter.send(:bless_model, @model)
184 it "should set client attribute on given model to self" do
185 model = @twitter.send(:bless_model, @model)
186 model.client.should eql(@twitter)
189 # if model is nil, it doesn't not necessarily signify an exceptional case for this method's usage.
190 it "should return nil when receiving nil and not raise any exceptions" do
191 model = @twitter.send(:bless_model, nil)
195 # needed to alert developer that the model needs to respond to #client= messages appropriately.
196 it "should raise an error if passing in a non-nil object that doesn't not respond to the :client= message" do
198 @twitter.send(:bless_model, Object.new)
199 }.should raise_error(NoMethodError)
207 describe Twitter::Client, "#bless_models" do
209 @twitter = client_context
211 Twitter::Status.new(:text => 'message #1'),
212 Twitter::Status.new(:text => 'message #2'),
216 it "should set client attributes for each model in given Array to self" do
217 models = @twitter.send(:bless_models, @models)
218 models.each {|model| model.client.should eql(@twitter) }
221 it "should set client attribute for singular model given to self" do
222 model = @twitter.send(:bless_models, @models[0])
223 model.client.should eql(@twitter)
226 it "should delegate to bless_model for singular model case" do
228 @twitter.should_receive(:bless_model).with(model).and_return(model)
229 @twitter.send(:bless_models, model)
232 it "should return nil when receiving nil and not raise any exceptions" do
234 value = @twitter.send(:bless_models, nil)
236 }.should_not raise_error
240 nilize(@twitter, @models)