Added Twitter::Client#inspect and relevant spec
[twitter4r-core.git] / spec / twitter / client / base_spec.rb
blobba09fb7c7b000395c87478dede6efbc4b25ef432
1 require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3 describe "Twitter::Client" do
4   before(:each) do
5     @init_hash = { :login => 'user', :password => 'pass' }
6   end
8   it ".new should accept login and password as initializer hash keys and set the values to instance values" do
9     client = nil
10     lambda 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])
15   end  
16 end
18 describe Twitter::Client, "#inspect" do
19   before(:each) do
20     @client = Twitter::Client.new(:login => "NippleEquality", :password => "3rdnipple")
21   end
23   it "should block out password attribute values" do
24     @client.inspect.should_not match(/@password="3rdnipple"/)
25   end
26 end
28 describe Twitter::Client, "#http_header" do
29   before(:each) do
30     @user_agent = 'myapp'
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
39     end
40     @expected_headers = {
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}]",
46     }
47     @twitter = client_context
48     # reset @@http_header class variable in Twitter::Client class
49     Twitter::Client.class_eval("@@http_header = nil")
50   end
51   
52   it "should always return expected HTTP headers" do
53     headers = @twitter.send(:http_header)
54     headers.should === @expected_headers
55   end
56   
57   it "should cache HTTP headers Hash in class variable after first invocation" do
58     cache = Twitter::Client.class_eval("@@http_header")
59     cache.should be_nil
60     @twitter.send(:http_header)
61     cache = Twitter::Client.class_eval("@@http_header")
62     cache.should_not be_nil
63     cache.should === @expected_headers
64   end
65   
66   after(:each) do
67     nilize(@user_agent, @application_name, @application_version, @application_url, @twitter, @expected_headers)
68   end
69 end
71 describe Twitter::Client, "#create_http_get_request" do
72   before(:each) do
73         @uri = '/some/path'
74         @expected_get_request = mock(Net::HTTP::Get)
75     @twitter = client_context
76         @default_header = @twitter.send(:http_header)
77   end
78   
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)
82   end
83   
84   after(:each) do
85     nilize(@twitter, @uri, @expected_get_request, @default_header)
86   end
87 end
89 describe Twitter::Client, "#create_http_post_request" do
90   before(:each) do
91         @uri = '/some/path'
92         @expected_post_request = mock(Net::HTTP::Post)
93         @twitter = client_context
94         @default_header = @twitter.send(:http_header)
95   end
96     
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)
100   end
101   
102   after(:each) do
103     nilize(@twitter, @uri, @expected_post_request, @default_header)    
104   end
107 describe Twitter::Client, "#create_http_delete_request" do
108   before(:each) 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)
113   end
114   
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)
118   end
119   
120   after(:each) do
121     nilize(@twitter, @uri, @expected_delete_request, @default_header)
122   end
125 describe Twitter::Client, "#http_connect" do
126   before(:each) 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)
133       @has_yielded = true
134       @request
135     end
136     @twitter = client_context
137     @has_yielded = false
138   end
139   
140   def generate_bad_response
141     @http = mas_net_http(@bad_response, @http_stubs)
142     Net::HTTP.stub!(:new).and_return(@http)
143   end
144   
145   def generate_good_response
146     @http = mas_net_http(@good_response, @http_stubs)
147     Net::HTTP.stub!(:new).and_return(@http)
148   end
149   
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)
153     lambda do
154       @twitter.send(:http_connect, &@block)
155     end.should_not raise_error
156     @has_yielded.should be(true)
157   end
158   
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)
162     lambda {
163       @twitter.send(:http_connect, &@block)
164     }.should raise_error(Twitter::RESTError)
165     @has_yielded.should be(true)
166   end
167   
168   after(:each) do
169     nilize(@good_response, @bad_response, @http)
170   end
173 describe Twitter::Client, "#bless_model" do
174   before(:each) do
175     @twitter = client_context
176     @model = Twitter::User.new
177   end
178   
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)
182   end
183   
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)
187   end
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)
192     model.should be_nil
193   end
194   
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
197     lambda {
198       @twitter.send(:bless_model, Object.new)      
199     }.should raise_error(NoMethodError)
200   end
201   
202   after(:each) do
203     nilize(@twitter)
204   end
207 describe Twitter::Client, "#bless_models" do
208   before(:each) do
209     @twitter = client_context
210     @models = [
211         Twitter::Status.new(:text => 'message #1'),
212         Twitter::Status.new(:text => 'message #2'),
213     ]
214   end
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) }
219   end
220   
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)
224   end
225   
226   it "should delegate to bless_model for singular model case" do
227     model = @models[0]
228     @twitter.should_receive(:bless_model).with(model).and_return(model)
229     @twitter.send(:bless_models, model)
230   end
231   
232   it "should return nil when receiving nil and not raise any exceptions" do
233     lambda {
234       value = @twitter.send(:bless_models, nil)
235       value.should be_nil
236     }.should_not raise_error
237   end
238   
239   after(:each) do
240     nilize(@twitter, @models)
241   end