koala 0.10.0 → 1.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/CHANGELOG +33 -7
  2. data/Manifest +8 -1
  3. data/Rakefile +4 -4
  4. data/koala.gemspec +10 -8
  5. data/lib/koala/graph_api.rb +188 -123
  6. data/lib/koala/http_services.rb +93 -17
  7. data/lib/koala/rest_api.rb +73 -6
  8. data/lib/koala/test_users.rb +18 -5
  9. data/lib/koala/uploadable_io.rb +115 -0
  10. data/lib/koala.rb +95 -72
  11. data/readme.md +18 -12
  12. data/spec/facebook_data.yml +9 -3
  13. data/spec/koala/assets/beach.jpg +0 -0
  14. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +5 -1
  15. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +8 -3
  16. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +12 -61
  17. data/spec/koala/graph_api/graph_api_tests.rb +85 -0
  18. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +167 -123
  19. data/spec/koala/http_services/http_service_tests.rb +51 -0
  20. data/spec/koala/http_services/net_http_service_tests.rb +339 -0
  21. data/spec/koala/http_services/typhoeus_service_tests.rb +162 -0
  22. data/spec/koala/live_testing_data_helper.rb +1 -1
  23. data/spec/koala/oauth/oauth_tests.rb +35 -64
  24. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +5 -74
  25. data/spec/koala/rest_api/rest_api_tests.rb +118 -0
  26. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +5 -3
  27. data/spec/koala/test_users/test_users_tests.rb +49 -48
  28. data/spec/koala/uploadable_io/uploadable_io_tests.rb +246 -0
  29. data/spec/koala_spec_helper.rb +30 -4
  30. data/spec/koala_spec_without_mocks.rb +3 -3
  31. data/spec/mock_facebook_responses.yml +41 -18
  32. data/spec/mock_http_service.rb +16 -3
  33. metadata +38 -8
  34. data/spec/koala/net_http_service_tests.rb +0 -186
@@ -1,186 +0,0 @@
1
- require 'koala/http_services'
2
- class NetHTTPServiceTests < Test::Unit::TestCase
3
- module Bear
4
- include Koala::NetHTTPService
5
- end
6
-
7
- describe "NetHTTPService module holder class Bear" do
8
- it "should define a make_request static module method" do
9
- Bear.respond_to?(:make_request).should be_true
10
- end
11
-
12
- describe "when making a request" do
13
- before(:each) do
14
- # Setup stubs for make_request to execute without exceptions
15
- @mock_http_response = stub('Net::HTTPResponse', :code => 1)
16
- @mock_body = stub('Net::HTTPResponse body')
17
- @http_request_result = [@mock_http_response, @mock_body]
18
-
19
- # to_ary is called in Ruby 1.9 to provide backwards compatibility
20
- # with the response, body = http.get() syntax we use
21
- @mock_http_response.stub!(:to_ary).and_return(@http_request_result)
22
-
23
- @http_yield_mock = mock('Net::HTTP start yielded object')
24
-
25
- @http_yield_mock.stub(:post).and_return(@http_request_result)
26
- @http_yield_mock.stub(:get).and_return(@http_request_result)
27
-
28
- @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
29
- @http_mock.stub(:start).and_yield(@http_yield_mock)
30
-
31
- Net::HTTP.stub(:new).and_return(@http_mock)
32
- end
33
-
34
- it "should use POST if verb is not GET" do
35
- @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
36
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
37
-
38
- Bear.make_request('anything', {}, 'anything')
39
- end
40
-
41
- it "should use port 443" do
42
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
43
-
44
- Bear.make_request('anything', {}, 'anything')
45
- end
46
-
47
- it "should use SSL" do
48
- @http_mock.should_receive('use_ssl=').with(true)
49
-
50
- Bear.make_request('anything', {}, 'anything')
51
- end
52
-
53
- it "should use the graph server by default" do
54
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
55
- Bear.make_request('anything', {}, 'anything')
56
- end
57
-
58
- it "should use the REST server if the :rest_api option is true" do
59
- Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
60
- Bear.make_request('anything', {}, 'anything', :rest_api => true)
61
- end
62
-
63
- it "should turn off vertificate validaiton warnings" do
64
- @http_mock.should_receive('verify_mode=').with(OpenSSL::SSL::VERIFY_NONE)
65
-
66
- Bear.make_request('anything', {}, 'anything')
67
- end
68
-
69
- it "should start an HTTP connection" do
70
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
71
- Bear.make_request('anything', {}, 'anything')
72
- end
73
-
74
- describe "via POST" do
75
- it "should use Net::HTTP to make a POST request" do
76
- @http_yield_mock.should_receive(:post).and_return(@http_request_result)
77
-
78
- Bear.make_request('anything', {}, 'post')
79
- end
80
-
81
- it "should go to the specified path adding a / if it doesn't exist" do
82
- path = mock('Path')
83
- @http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
84
-
85
- Bear.make_request(path, {}, 'post')
86
- end
87
-
88
- it "should use encoded parameters" do
89
- args = {}
90
- params = mock('Encoded parameters')
91
- Bear.should_receive(:encode_params).with(args).and_return(params)
92
-
93
- @http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
94
-
95
- Bear.make_request('anything', args, 'post')
96
- end
97
- end
98
-
99
- describe "via GET" do
100
- it "should use Net::HTTP to make a GET request" do
101
- @http_yield_mock.should_receive(:get).and_return(@http_request_result)
102
-
103
- Bear.make_request('anything', {}, 'get')
104
- end
105
-
106
- it "should use the correct path, including arguments" do
107
- path = mock('Path')
108
- params = mock('Encoded parameters')
109
- args = {}
110
-
111
- Bear.should_receive(:encode_params).with(args).and_return(params)
112
- @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
113
-
114
- Bear.make_request(path, args, 'get')
115
- end
116
- end
117
-
118
- describe "the returned value" do
119
- before(:each) do
120
- @response = Bear.make_request('anything', {}, 'anything')
121
- end
122
-
123
- it "should return a Koala::Response object" do
124
- @response.class.should == Koala::Response
125
- end
126
-
127
- it "should return a Koala::Response with the right status" do
128
- @response.status.should == @mock_http_response.code
129
- end
130
-
131
- it "should reutrn a Koala::Response with the right body" do
132
- @response.body.should == @mock_body
133
- end
134
-
135
- it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
136
- @response.headers.should == @mock_http_response
137
- end
138
- end # describe return value
139
- end # describe when making a request
140
-
141
- describe "when encoding parameters" do
142
- it "should return an empty string if param_hash evaluates to false" do
143
- Bear.encode_params(nil).should == ''
144
- end
145
-
146
- it "should convert values to JSON if the value is not a String" do
147
- val = 'json_value'
148
- not_a_string = 'not_a_string'
149
- not_a_string.stub(:class).and_return('NotAString')
150
- not_a_string.should_receive(:to_json).and_return(val)
151
-
152
- string = "hi"
153
-
154
- args = {
155
- not_a_string => not_a_string,
156
- string => string
157
- }
158
-
159
- result = Bear.encode_params(args)
160
- result.split('&').find do |key_and_val|
161
- key_and_val.match("#{not_a_string}=#{val}")
162
- end.should be_true
163
- end
164
-
165
- it "should escape all values" do
166
- args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
167
-
168
- result = Bear.encode_params(args)
169
- result.split('&').each do |key_val|
170
- key, val = key_val.split('=')
171
- val.should == CGI.escape(args[key])
172
- end
173
- end
174
-
175
- it "should convert all keys to Strings" do
176
- args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
177
-
178
- result = Bear.encode_params(args)
179
- result.split('&').each do |key_val|
180
- key, val = key_val.split('=')
181
- key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
182
- end
183
- end
184
- end
185
- end
186
- end