koala 0.9.0 → 1.0.0.beta
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.
- data/CHANGELOG +33 -7
- data/Manifest +6 -14
- data/Rakefile +4 -3
- data/koala.gemspec +13 -8
- data/lib/koala/graph_api.rb +16 -3
- data/lib/koala/http_services.rb +100 -16
- data/lib/koala/rest_api.rb +73 -6
- data/lib/koala/test_users.rb +72 -0
- data/lib/koala.rb +101 -71
- data/readme.md +14 -13
- data/spec/facebook_data.yml +14 -8
- data/spec/koala/api_base_tests.rb +7 -0
- data/spec/koala/assets/beach.jpg +0 -0
- data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +5 -1
- data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +9 -4
- data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +10 -61
- data/spec/koala/graph_api/graph_api_tests.rb +86 -0
- data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +130 -126
- data/spec/koala/graph_api/graph_collection_tests.rb +2 -2
- data/spec/koala/live_testing_data_helper.rb +40 -12
- data/spec/koala/net_http_service_tests.rb +401 -152
- data/spec/koala/oauth/oauth_tests.rb +41 -72
- data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +7 -76
- data/spec/koala/rest_api/rest_api_tests.rb +118 -0
- data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +6 -4
- data/spec/koala/test_users/test_users_tests.rb +208 -0
- data/spec/koala/typhoeus_service_tests.rb +156 -0
- data/spec/koala_spec_helper.rb +43 -4
- data/spec/koala_spec_without_mocks.rb +4 -4
- data/spec/mock_facebook_responses.yml +69 -3
- data/spec/mock_http_service.rb +16 -1
- metadata +49 -23
- data/examples/oauth_playground/Capfile +0 -2
- data/examples/oauth_playground/LICENSE +0 -22
- data/examples/oauth_playground/Rakefile +0 -4
- data/examples/oauth_playground/config/deploy.rb +0 -39
- data/examples/oauth_playground/config/facebook.yml +0 -13
- data/examples/oauth_playground/config.ru +0 -27
- data/examples/oauth_playground/lib/load_facebook.rb +0 -3
- data/examples/oauth_playground/lib/oauth_playground.rb +0 -187
- data/examples/oauth_playground/readme.md +0 -8
- data/examples/oauth_playground/spec/oauth_playground_spec.rb +0 -35
- data/examples/oauth_playground/spec/spec_helper.rb +0 -36
- data/examples/oauth_playground/tmp/restart.txt +0 -0
- data/examples/oauth_playground/views/index.erb +0 -206
- data/examples/oauth_playground/views/layout.erb +0 -39
|
@@ -1,180 +1,429 @@
|
|
|
1
1
|
require 'koala/http_services'
|
|
2
|
-
|
|
3
2
|
class NetHTTPServiceTests < Test::Unit::TestCase
|
|
4
3
|
module Bear
|
|
5
4
|
include Koala::NetHTTPService
|
|
6
5
|
end
|
|
7
|
-
|
|
8
|
-
it "should define a make_request static module method" do
|
|
9
|
-
Bear.respond_to?(:make_request).should be_true
|
|
10
|
-
end
|
|
11
6
|
|
|
12
|
-
describe "
|
|
13
|
-
before
|
|
14
|
-
#
|
|
15
|
-
|
|
16
|
-
@mock_body = stub('Net::HTTPResponse body')
|
|
17
|
-
@http_request_result = [@mock_http_response, @mock_body]
|
|
18
|
-
|
|
19
|
-
@http_yield_mock = mock('Net::HTTP start yielded object')
|
|
20
|
-
|
|
21
|
-
@http_yield_mock.stub(:post).and_return(@http_request_result)
|
|
22
|
-
@http_yield_mock.stub(:get).and_return(@http_request_result)
|
|
23
|
-
|
|
24
|
-
@http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
|
|
25
|
-
@http_mock.stub(:start).and_yield(@http_yield_mock)
|
|
26
|
-
|
|
27
|
-
Net::HTTP.stub(:new).and_return(@http_mock)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it "should use POST if verb is not GET" do
|
|
31
|
-
@http_yield_mock.should_receive(:post).and_return(@mock_http_response)
|
|
32
|
-
@http_mock.should_receive(:start).and_yield(@http_yield_mock)
|
|
33
|
-
|
|
34
|
-
Bear.make_request('anything', {}, 'anything')
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it "should use port 443" do
|
|
38
|
-
Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
|
|
39
|
-
|
|
40
|
-
Bear.make_request('anything', {}, 'anything')
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
it "should use SSL" do
|
|
44
|
-
@http_mock.should_receive('use_ssl=').with(true)
|
|
45
|
-
|
|
46
|
-
Bear.make_request('anything', {}, 'anything')
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
it "should use the graph server by default" do
|
|
50
|
-
Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
|
|
51
|
-
Bear.make_request('anything', {}, 'anything')
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it "should use the REST server if the :rest_api option is true" do
|
|
55
|
-
Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
|
|
56
|
-
Bear.make_request('anything', {}, 'anything', :rest_api => true)
|
|
7
|
+
describe "NetHTTPService module holder class Bear" do
|
|
8
|
+
before :each do
|
|
9
|
+
# reset the always_use_ssl parameter
|
|
10
|
+
Bear.always_use_ssl = nil
|
|
57
11
|
end
|
|
58
|
-
|
|
59
|
-
it "should
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
Bear.make_request('anything', {}, 'anything')
|
|
12
|
+
|
|
13
|
+
it "should define a make_request static module method" do
|
|
14
|
+
Bear.respond_to?(:make_request).should be_true
|
|
63
15
|
end
|
|
64
|
-
|
|
65
|
-
it "should
|
|
66
|
-
|
|
67
|
-
Bear.make_request('anything', {}, 'anything')
|
|
16
|
+
|
|
17
|
+
it "should include the Koala::HTTPService module defining common features" do
|
|
18
|
+
Bear.included_modules.include?(Koala::HTTPService).should be_true
|
|
68
19
|
end
|
|
69
|
-
|
|
70
|
-
describe "
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
20
|
+
|
|
21
|
+
describe "when making a request" do
|
|
22
|
+
before(:each) do
|
|
23
|
+
# Setup stubs for make_request to execute without exceptions
|
|
24
|
+
@mock_http_response = stub('Net::HTTPResponse', :code => 1)
|
|
25
|
+
@mock_body = stub('Net::HTTPResponse body')
|
|
26
|
+
@http_request_result = [@mock_http_response, @mock_body]
|
|
27
|
+
|
|
28
|
+
# to_ary is called in Ruby 1.9 to provide backwards compatibility
|
|
29
|
+
# with the response, body = http.get() syntax we use
|
|
30
|
+
@mock_http_response.stub!(:to_ary).and_return(@http_request_result)
|
|
31
|
+
|
|
32
|
+
@http_yield_mock = mock('Net::HTTP start yielded object')
|
|
33
|
+
|
|
34
|
+
@http_yield_mock.stub(:post).and_return(@http_request_result)
|
|
35
|
+
@http_yield_mock.stub(:get).and_return(@http_request_result)
|
|
36
|
+
|
|
37
|
+
@http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
|
|
38
|
+
@http_mock.stub(:start).and_yield(@http_yield_mock)
|
|
39
|
+
|
|
40
|
+
Net::HTTP.stub(:new).and_return(@http_mock)
|
|
75
41
|
end
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
42
|
+
|
|
43
|
+
describe "the connection" do
|
|
44
|
+
it "should use POST if verb is not GET" do
|
|
45
|
+
@http_yield_mock.should_receive(:post).and_return(@mock_http_response)
|
|
46
|
+
@http_mock.should_receive(:start).and_yield(@http_yield_mock)
|
|
47
|
+
|
|
48
|
+
Bear.make_request('anything', {}, 'anything')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "should use GET if that verb is specified" do
|
|
52
|
+
@http_yield_mock.should_receive(:get).and_return(@mock_http_response)
|
|
53
|
+
@http_mock.should_receive(:start).and_yield(@http_yield_mock)
|
|
54
|
+
|
|
55
|
+
Bear.make_request('anything', {}, 'get')
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should add the method to the arguments if it's not get or post" do
|
|
59
|
+
args = {}
|
|
60
|
+
method = "telekenesis"
|
|
61
|
+
# since the arguments get encoded later, we'll test for merge!
|
|
62
|
+
# even though that's somewhat testing internal implementation
|
|
63
|
+
args.should_receive(:merge!).with(:method => method)
|
|
64
|
+
|
|
65
|
+
Bear.make_request('anything', args, method)
|
|
66
|
+
end
|
|
82
67
|
end
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
68
|
+
|
|
69
|
+
describe "if the request has an access token" do
|
|
70
|
+
before :each do
|
|
71
|
+
@args = {"access_token" => "123"}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "should use SSL" do
|
|
75
|
+
@http_mock.should_receive('use_ssl=').with(true)
|
|
76
|
+
|
|
77
|
+
Bear.make_request('anything', @args, 'anything')
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "should set the port to 443" do
|
|
81
|
+
Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
|
|
82
|
+
|
|
83
|
+
Bear.make_request('anything', @args, 'anything')
|
|
84
|
+
end
|
|
92
85
|
end
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
86
|
+
|
|
87
|
+
describe "if always_use_ssl is true" do
|
|
88
|
+
before :each do
|
|
89
|
+
Bear.always_use_ssl = true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it "should use SSL" do
|
|
93
|
+
@http_mock.should_receive('use_ssl=').with(true)
|
|
94
|
+
|
|
95
|
+
Bear.make_request('anything', {}, 'anything')
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "should set the port to 443" do
|
|
99
|
+
Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
|
|
100
|
+
|
|
101
|
+
Bear.make_request('anything', {}, 'anything')
|
|
102
|
+
end
|
|
100
103
|
end
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
|
|
105
|
+
describe "if the use_ssl option is provided" do
|
|
106
|
+
it "should use SSL" do
|
|
107
|
+
@http_mock.should_receive('use_ssl=').with(true)
|
|
108
|
+
|
|
109
|
+
Bear.make_request('anything', {}, 'anything', :use_ssl => true)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "should set the port to 443" do
|
|
113
|
+
Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
|
|
114
|
+
|
|
115
|
+
Bear.make_request('anything', {}, 'anything', :use_ssl => true)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
describe "if there's no token and always_use_ssl isn't true" do
|
|
120
|
+
it "should not use SSL" do
|
|
121
|
+
@http_mock.should_not_receive('use_ssl=')
|
|
122
|
+
Bear.make_request('anything', {}, 'anything')
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "should not set the port" do
|
|
126
|
+
Net::HTTP.should_receive(:new).with(anything, nil).and_return(@http_mock)
|
|
127
|
+
Bear.make_request('anything', {}, 'anything')
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it "should use the graph server by default" do
|
|
132
|
+
Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
|
|
133
|
+
Bear.make_request('anything', {}, 'anything')
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it "should use the REST server if the :rest_api option is true" do
|
|
137
|
+
Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
|
|
138
|
+
Bear.make_request('anything', {}, 'anything', :rest_api => true)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "should turn off vertificate validaiton warnings" do
|
|
142
|
+
@http_mock.should_receive('verify_mode=').with(OpenSSL::SSL::VERIFY_NONE)
|
|
143
|
+
|
|
144
|
+
Bear.make_request('anything', {}, 'anything')
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it "should start an HTTP connection" do
|
|
148
|
+
@http_mock.should_receive(:start).and_yield(@http_yield_mock)
|
|
149
|
+
Bear.make_request('anything', {}, 'anything')
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
describe "via POST" do
|
|
153
|
+
it "should use Net::HTTP to make a POST request" do
|
|
154
|
+
@http_yield_mock.should_receive(:post).and_return(@http_request_result)
|
|
155
|
+
|
|
156
|
+
Bear.make_request('anything', {}, 'post')
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it "should go to the specified path adding a / if it doesn't exist" do
|
|
160
|
+
path = mock('Path')
|
|
161
|
+
@http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
|
|
162
|
+
|
|
163
|
+
Bear.make_request(path, {}, 'post')
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "should use encoded parameters" do
|
|
167
|
+
args = {}
|
|
168
|
+
params = mock('Encoded parameters')
|
|
169
|
+
Bear.should_receive(:encode_params).with(args).and_return(params)
|
|
170
|
+
|
|
171
|
+
@http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
|
|
172
|
+
|
|
173
|
+
Bear.make_request('anything', args, 'post')
|
|
174
|
+
end
|
|
109
175
|
|
|
110
|
-
|
|
176
|
+
describe "with multipart/form-data" do
|
|
177
|
+
before(:each) do
|
|
178
|
+
Bear.stub(:encode_multipart_params)
|
|
179
|
+
Bear.stub("params_require_multipart?").and_return(true)
|
|
180
|
+
|
|
181
|
+
@multipart_request_stub = stub('Stub Multipart Request')
|
|
182
|
+
Net::HTTP::Post::Multipart.stub(:new).and_return(@multipart_request_stub)
|
|
183
|
+
|
|
184
|
+
@file_stub = stub('fake File', "kind_of?" => true, "path" => 'anypath.jpg')
|
|
185
|
+
|
|
186
|
+
@http_yield_mock.stub(:request).with(@multipart_request_stub).and_return(@http_request_result)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
it "should use multipart/form-data if any parameter is a valid file hash" do
|
|
190
|
+
@http_yield_mock.should_receive(:request).with(@multipart_request_stub).and_return(@http_request_result)
|
|
191
|
+
|
|
192
|
+
Bear.make_request('anything', {}, 'post')
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it "should use the given request path for the request" do
|
|
196
|
+
args = {"file" => @file_stub}
|
|
197
|
+
expected_path = 'expected/path'
|
|
198
|
+
|
|
199
|
+
Net::HTTP::Post::Multipart.should_receive(:new).with(expected_path, anything).and_return(@multipart_request_stub)
|
|
200
|
+
|
|
201
|
+
Bear.make_request(expected_path, {}, 'post')
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "should use multipart encoded arguments for the request" do
|
|
205
|
+
args = {"file" => @file_stub}
|
|
206
|
+
expected_params = stub('Stub Multipart Params')
|
|
207
|
+
|
|
208
|
+
Bear.should_receive(:encode_multipart_params).with(args).and_return(expected_params)
|
|
209
|
+
Net::HTTP::Post::Multipart.should_receive(:new).with(anything, expected_params).and_return(@multipart_request_stub)
|
|
210
|
+
|
|
211
|
+
Bear.make_request('anything', args, 'post')
|
|
212
|
+
end
|
|
213
|
+
end
|
|
111
214
|
end
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
215
|
+
|
|
216
|
+
describe "via GET" do
|
|
217
|
+
it "should use Net::HTTP to make a GET request" do
|
|
218
|
+
@http_yield_mock.should_receive(:get).and_return(@http_request_result)
|
|
219
|
+
|
|
220
|
+
Bear.make_request('anything', {}, 'get')
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "should use the correct path, including arguments" do
|
|
224
|
+
path = mock('Path')
|
|
225
|
+
params = mock('Encoded parameters')
|
|
226
|
+
args = {}
|
|
227
|
+
|
|
228
|
+
Bear.should_receive(:encode_params).with(args).and_return(params)
|
|
229
|
+
@http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
|
|
230
|
+
|
|
231
|
+
Bear.make_request(path, args, 'get')
|
|
232
|
+
end
|
|
117
233
|
end
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
234
|
+
|
|
235
|
+
describe "the returned value" do
|
|
236
|
+
before(:each) do
|
|
237
|
+
@response = Bear.make_request('anything', {}, 'anything')
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "should return a Koala::Response object" do
|
|
241
|
+
@response.class.should == Koala::Response
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
it "should return a Koala::Response with the right status" do
|
|
245
|
+
@response.status.should == @mock_http_response.code
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it "should reutrn a Koala::Response with the right body" do
|
|
249
|
+
@response.body.should == @mock_body
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
|
|
253
|
+
@response.headers.should == @mock_http_response
|
|
254
|
+
end
|
|
255
|
+
end # describe return value
|
|
256
|
+
end # describe when making a request
|
|
257
|
+
|
|
258
|
+
describe "when encoding parameters" do
|
|
259
|
+
it "should return an empty string if param_hash evaluates to false" do
|
|
260
|
+
Bear.encode_params(nil).should == ''
|
|
121
261
|
end
|
|
122
|
-
|
|
123
|
-
it "should
|
|
124
|
-
|
|
262
|
+
|
|
263
|
+
it "should convert values to JSON if the value is not a String" do
|
|
264
|
+
val = 'json_value'
|
|
265
|
+
not_a_string = 'not_a_string'
|
|
266
|
+
not_a_string.stub(:class).and_return('NotAString')
|
|
267
|
+
not_a_string.should_receive(:to_json).and_return(val)
|
|
268
|
+
|
|
269
|
+
string = "hi"
|
|
270
|
+
|
|
271
|
+
args = {
|
|
272
|
+
not_a_string => not_a_string,
|
|
273
|
+
string => string
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
result = Bear.encode_params(args)
|
|
277
|
+
result.split('&').find do |key_and_val|
|
|
278
|
+
key_and_val.match("#{not_a_string}=#{val}")
|
|
279
|
+
end.should be_true
|
|
125
280
|
end
|
|
126
|
-
|
|
127
|
-
it "should
|
|
128
|
-
|
|
281
|
+
|
|
282
|
+
it "should escape all values" do
|
|
283
|
+
args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
|
|
284
|
+
|
|
285
|
+
result = Bear.encode_params(args)
|
|
286
|
+
result.split('&').each do |key_val|
|
|
287
|
+
key, val = key_val.split('=')
|
|
288
|
+
val.should == CGI.escape(args[key])
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
it "should convert all keys to Strings" do
|
|
293
|
+
args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
|
|
294
|
+
|
|
295
|
+
result = Bear.encode_params(args)
|
|
296
|
+
result.split('&').each do |key_val|
|
|
297
|
+
key, val = key_val.split('=')
|
|
298
|
+
key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
|
|
299
|
+
end
|
|
129
300
|
end
|
|
130
|
-
|
|
131
|
-
it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
|
|
132
|
-
@response.headers.should == @mock_http_response
|
|
133
|
-
end
|
|
134
|
-
end # describe return value
|
|
135
|
-
end # describe when making a request
|
|
136
|
-
|
|
137
|
-
describe "when encoding parameters" do
|
|
138
|
-
it "should return an empty string if param_hash evaluates to false" do
|
|
139
|
-
Bear.encode_params(nil).should == ''
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
it "should convert values to JSON if the value is not a String" do
|
|
143
|
-
val = 'json_value'
|
|
144
|
-
not_a_string = 'not_a_string'
|
|
145
|
-
not_a_string.stub(:class).and_return('NotAString')
|
|
146
|
-
not_a_string.should_receive(:to_json).and_return(val)
|
|
147
|
-
|
|
148
|
-
string = "hi"
|
|
149
|
-
|
|
150
|
-
args = {
|
|
151
|
-
not_a_string => not_a_string,
|
|
152
|
-
string => string
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
result = Bear.encode_params(args)
|
|
156
|
-
result.split('&').find do |key_and_val|
|
|
157
|
-
key_and_val.match("#{not_a_string}=#{val}")
|
|
158
|
-
end.should be_true
|
|
159
301
|
end
|
|
160
302
|
|
|
161
|
-
|
|
162
|
-
|
|
303
|
+
describe "when detecting if multipart posting is needed" do
|
|
304
|
+
it "should be true if any parameter value requires multipart post" do
|
|
305
|
+
valid_file_hash = stub("Stub Valid File Hash")
|
|
306
|
+
|
|
307
|
+
Bear.stub!("is_valid_file_hash?").and_return(false)
|
|
308
|
+
Bear.stub!("is_valid_file_hash?").with(valid_file_hash).and_return(true)
|
|
309
|
+
|
|
310
|
+
args = {
|
|
311
|
+
"key1" => "val",
|
|
312
|
+
"key2" => "val",
|
|
313
|
+
"key3" => valid_file_hash,
|
|
314
|
+
"key4" => "val"
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
Bear.params_require_multipart?(args).should be_true
|
|
318
|
+
end
|
|
163
319
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
320
|
+
describe "and looking at individual values" do
|
|
321
|
+
before(:each) do
|
|
322
|
+
@valid_hash = {
|
|
323
|
+
"content_type" => 1,
|
|
324
|
+
"path" => 1
|
|
325
|
+
}
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
it "should only accept hashes" do
|
|
329
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_true
|
|
330
|
+
|
|
331
|
+
@valid_hash.stub!("kind_of?").with(Hash).and_return(false)
|
|
332
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_false
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
it "should always require a content_type key" do
|
|
336
|
+
@valid_hash.delete("content_type")
|
|
337
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_false
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
it "should always require the path key" do
|
|
341
|
+
@valid_hash.delete("path")
|
|
342
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_false
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
describe "with file IOs" do
|
|
346
|
+
before :each do
|
|
347
|
+
@stub_file = stub('Stub IO File')
|
|
348
|
+
@valid_hash["file"] = @stub_file
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
it "should accept hashes with the file object that responds to read" do
|
|
352
|
+
@stub_file.should_receive("respond_to?").with(:read).and_return(true)
|
|
353
|
+
|
|
354
|
+
@valid_hash["file"] = @stub_file
|
|
355
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_true
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
it "should not accept hashes with a file object that does not respond to read" do
|
|
359
|
+
@stub_file.should_receive("respond_to?").with(:read).and_return(false)
|
|
360
|
+
|
|
361
|
+
@valid_hash["file"] = @stub_file
|
|
362
|
+
Bear.is_valid_file_hash?(@valid_hash).should be_false
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
end
|
|
168
366
|
end
|
|
169
367
|
end
|
|
170
368
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
369
|
+
describe "when encoding multipart/form-data params" do
|
|
370
|
+
it "should replace valid file hashes with file objects with UploadIO objects" do
|
|
371
|
+
file_hash_stub = {
|
|
372
|
+
"path" => "Fake File Name",
|
|
373
|
+
"content_type" => "Fake Content Type"
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
# UploadIO should be created
|
|
377
|
+
uploadio_stub = stub("UploadIO Shell Stub")
|
|
378
|
+
UploadIO.should_receive("new").with(file_hash_stub["path"], file_hash_stub["content_type"]).and_return(uploadio_stub)
|
|
379
|
+
|
|
380
|
+
# Ruby 1.9 test compatibility
|
|
381
|
+
content_stub = "UploadIOContent Stub"
|
|
382
|
+
uploadio_stub.stub(:to_ary).and_return([content_stub])
|
|
383
|
+
|
|
384
|
+
args = {
|
|
385
|
+
"not_a_file" => "not a file",
|
|
386
|
+
"file" => file_hash_stub
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
# Check that is_valid_file_hash is called on the file_hash_stub
|
|
390
|
+
Bear.stub!("is_valid_file_hash?").and_return(false)
|
|
391
|
+
Bear.should_receive("is_valid_file_hash?").with(file_hash_stub).and_return(true)
|
|
392
|
+
|
|
393
|
+
result = Bear.encode_multipart_params(args)
|
|
394
|
+
|
|
395
|
+
result["not_a_file"] == args["not_a_file"]
|
|
396
|
+
result["file"] == content_stub
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
it "should replace valid file hashes with file objects with UploadIO objects" do
|
|
400
|
+
file_hash_stub = {
|
|
401
|
+
"file" => "Fake File IO",
|
|
402
|
+
"path" => "Fake File Name",
|
|
403
|
+
"content_type" => "Fake Content Type"
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
# UploadIO should be created
|
|
407
|
+
uploadio_stub = stub("UploadIO Shell Stub")
|
|
408
|
+
UploadIO.should_receive("new").with(file_hash_stub["file"], file_hash_stub["content_type"], file_hash_stub["path"]).and_return(uploadio_stub)
|
|
409
|
+
|
|
410
|
+
# Ruby 1.9 test compatibility
|
|
411
|
+
content_stub = "UploadIOContent Stub"
|
|
412
|
+
uploadio_stub.stub(:to_ary).and_return([content_stub])
|
|
413
|
+
|
|
414
|
+
args = {
|
|
415
|
+
"not_a_file" => "not a file",
|
|
416
|
+
"file" => file_hash_stub
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
# Check that is_valid_file_hash is called on the file_hash_stub
|
|
420
|
+
Bear.stub!("is_valid_file_hash?").and_return(false)
|
|
421
|
+
Bear.should_receive("is_valid_file_hash?").with(file_hash_stub).and_return(true)
|
|
422
|
+
|
|
423
|
+
result = Bear.encode_multipart_params(args)
|
|
424
|
+
|
|
425
|
+
result["not_a_file"] == args["not_a_file"]
|
|
426
|
+
result["file"] == content_stub
|
|
178
427
|
end
|
|
179
428
|
end
|
|
180
429
|
end
|