Skip to content

Commit ab22889

Browse files
committed
Fix other mocks to return correct result size
1 parent 56b4dd1 commit ab22889

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

spec/cases/graph_api_batch_spec.rb

+27-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'json' unless Hash.respond_to?(:to_json)
33

44
describe "Koala::Facebook::GraphAPI in batch mode" do
5+
DEFAULT_RESPONSE = [{code: 200, headers: [{name: "Content-Type", value: "text/javascript; charset=UTF-8"}], body: "{\"id\":\"1234\"}"}]
56

67
before :each do
78
@api = Koala::Facebook::API.new(@token)
@@ -270,15 +271,15 @@
270271

271272
describe "GraphAPI batch interface" do
272273
it "returns nothing for a batch operation" do
273-
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, "[]", {}))
274+
allow(Koala).to receive(:make_request).and_return(Koala::HTTPService::Response.new(200, DEFAULT_RESPONSE.to_json, {}))
274275
@api.batch do |batch_api|
275276
expect(batch_api.get_object('me')).to be_nil
276277
end
277278
end
278279

279280
describe "#batch" do
280281
before :each do
281-
@fake_response = Koala::HTTPService::Response.new(200, "[]", {})
282+
@fake_response = Koala::HTTPService::Response.new(200, DEFAULT_RESPONSE.to_json, {})
282283
allow(Koala).to receive(:make_request).and_return(@fake_response)
283284
end
284285

@@ -296,7 +297,11 @@
296297

297298
it "includes the first operation's access token as the main one in the args" do
298299
access_token = "foo"
299-
expect(Koala).to receive(:make_request).with(anything, hash_including("access_token" => access_token), anything, anything).and_return(@fake_response)
300+
expect(Koala).to receive(:make_request).with(anything, hash_including("access_token" => access_token), anything, anything) do |_request, args, _verb, _options|
301+
request_count = JSON.parse(args['batch']).length
302+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * request_count).to_json, {})
303+
end
304+
300305
Koala::Facebook::API.new(access_token).batch do |batch_api|
301306
batch_api.get_object('me')
302307
batch_api.get_object('me', {}, {'access_token' => 'bar'})
@@ -308,7 +313,11 @@
308313
access_token = "foo"
309314
app_secret = "baz"
310315
app_secret_digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), app_secret, access_token)
311-
expect(Koala).to receive(:make_request).with(anything, hash_including("access_token" => access_token, "appsecret_proof" => app_secret_digest), anything, anything).and_return(@fake_response)
316+
expect(Koala).to receive(:make_request).with(anything, hash_including("access_token" => access_token, "appsecret_proof" => app_secret_digest), anything, anything)do |_request, args, _verb, _options|
317+
request_count = JSON.parse(args['batch']).length
318+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * request_count).to_json, {})
319+
end
320+
312321
Koala::Facebook::API.new(access_token, app_secret).batch do |batch_api|
313322
batch_api.get_object('me')
314323
batch_api.get_object('me', {}, {'access_token' => 'bar'})
@@ -324,7 +333,11 @@
324333

325334
# two requests should generate two batch operations
326335
expected = JSON.dump([op.to_batch_params(access_token, nil), op.to_batch_params(access_token, nil)])
327-
expect(Koala).to receive(:make_request).with(anything, hash_including("batch" => expected), anything, anything).and_return(@fake_response)
336+
expect(Koala).to receive(:make_request).with(anything, hash_including("batch" => expected), anything, anything) do |_request, args, _verb, _options|
337+
request_count = JSON.parse(args['batch']).length
338+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * request_count).to_json, {})
339+
end
340+
328341
Koala::Facebook::API.new(access_token).batch do |batch_api|
329342
batch_api.get_object('me')
330343
batch_api.get_object('me')
@@ -338,6 +351,8 @@
338351
@key = "file0_0"
339352
@uploadable_io = double("UploadableIO")
340353
batch_op = double("Koala Batch Operation", :files => {@key => @uploadable_io}, :to_batch_params => {}, :access_token => "foo")
354+
allow(batch_op).to receive(:post_processing).and_return(nil)
355+
allow(batch_op).to receive(:http_options).and_return({})
341356
allow(Koala::Facebook::GraphBatchAPI::BatchOperation).to receive(:new).and_return(batch_op)
342357

343358
expect(Koala).to receive(:make_request).with(anything, hash_including(@key => @uploadable_io), anything, anything).and_return(@fake_response)
@@ -352,7 +367,8 @@
352367
expect(Koala).to receive(:make_request) do |url, args, method, options|
353368
# test the batch operations to make sure they appear in the right order
354369
expect((args ||= {})["batch"]).to match(/.*me\/farglebarg.*otheruser\/bababa/)
355-
@fake_response
370+
request_count = JSON.parse(args['batch']).length
371+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * request_count).to_json, {})
356372
end
357373
Koala::Facebook::API.new(access_token).batch do |batch_api|
358374
batch_api.get_connections('me', "farglebarg")
@@ -447,7 +463,10 @@
447463
first_count = 20
448464
second_count = 10
449465

450-
allow(Koala).to receive(:make_request).and_return(@fake_response)
466+
allow(Koala).to receive(:make_request) do |_request, args, _verb, _options|
467+
request_count = JSON.parse(args['batch']).length
468+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * request_count).to_json, {})
469+
end
451470

452471
thread1 = Thread.new do
453472
@api.batch do |batch_api|
@@ -478,13 +497,12 @@
478497
@random = Random.new
479498
end
480499
before :each do
481-
payload = [{code: 200, headers: [{name: "Content-Type", value: "text/javascript; charset=UTF-8"}], body: "{\"id\":\"1234\"}"}]
482500
allow(Koala).to receive(:make_request) do |_request, args, _verb, _options|
483501
request_count = JSON.parse(args['batch']).length
484502
expect(request_count).to be <= 50 # check FB's limit
485503
# simulate FB's result truncation
486504
response_count = request_count > 35 ? request_count - @random.rand(15) : request_count
487-
Koala::HTTPService::Response.new(200, (payload * response_count).to_json, {})
505+
Koala::HTTPService::Response.new(200, (DEFAULT_RESPONSE * response_count).to_json, {})
488506
end
489507
end
490508

0 commit comments

Comments
 (0)