|
2 | 2 | require 'json' unless Hash.respond_to?(:to_json)
|
3 | 3 |
|
4 | 4 | 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\"}"}] |
5 | 6 |
|
6 | 7 | before :each do
|
7 | 8 | @api = Koala::Facebook::API.new(@token)
|
|
270 | 271 |
|
271 | 272 | describe "GraphAPI batch interface" do
|
272 | 273 | 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, {})) |
274 | 275 | @api.batch do |batch_api|
|
275 | 276 | expect(batch_api.get_object('me')).to be_nil
|
276 | 277 | end
|
277 | 278 | end
|
278 | 279 |
|
279 | 280 | describe "#batch" do
|
280 | 281 | before :each do
|
281 |
| - @fake_response = Koala::HTTPService::Response.new(200, "[]", {}) |
| 282 | + @fake_response = Koala::HTTPService::Response.new(200, DEFAULT_RESPONSE.to_json, {}) |
282 | 283 | allow(Koala).to receive(:make_request).and_return(@fake_response)
|
283 | 284 | end
|
284 | 285 |
|
|
296 | 297 |
|
297 | 298 | it "includes the first operation's access token as the main one in the args" do
|
298 | 299 | 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 | + |
300 | 305 | Koala::Facebook::API.new(access_token).batch do |batch_api|
|
301 | 306 | batch_api.get_object('me')
|
302 | 307 | batch_api.get_object('me', {}, {'access_token' => 'bar'})
|
|
308 | 313 | access_token = "foo"
|
309 | 314 | app_secret = "baz"
|
310 | 315 | 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 | + |
312 | 321 | Koala::Facebook::API.new(access_token, app_secret).batch do |batch_api|
|
313 | 322 | batch_api.get_object('me')
|
314 | 323 | batch_api.get_object('me', {}, {'access_token' => 'bar'})
|
|
324 | 333 |
|
325 | 334 | # two requests should generate two batch operations
|
326 | 335 | 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 | + |
328 | 341 | Koala::Facebook::API.new(access_token).batch do |batch_api|
|
329 | 342 | batch_api.get_object('me')
|
330 | 343 | batch_api.get_object('me')
|
|
338 | 351 | @key = "file0_0"
|
339 | 352 | @uploadable_io = double("UploadableIO")
|
340 | 353 | 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({}) |
341 | 356 | allow(Koala::Facebook::GraphBatchAPI::BatchOperation).to receive(:new).and_return(batch_op)
|
342 | 357 |
|
343 | 358 | expect(Koala).to receive(:make_request).with(anything, hash_including(@key => @uploadable_io), anything, anything).and_return(@fake_response)
|
|
352 | 367 | expect(Koala).to receive(:make_request) do |url, args, method, options|
|
353 | 368 | # test the batch operations to make sure they appear in the right order
|
354 | 369 | 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, {}) |
356 | 372 | end
|
357 | 373 | Koala::Facebook::API.new(access_token).batch do |batch_api|
|
358 | 374 | batch_api.get_connections('me', "farglebarg")
|
|
447 | 463 | first_count = 20
|
448 | 464 | second_count = 10
|
449 | 465 |
|
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 |
451 | 470 |
|
452 | 471 | thread1 = Thread.new do
|
453 | 472 | @api.batch do |batch_api|
|
|
478 | 497 | @random = Random.new
|
479 | 498 | end
|
480 | 499 | before :each do
|
481 |
| - payload = [{code: 200, headers: [{name: "Content-Type", value: "text/javascript; charset=UTF-8"}], body: "{\"id\":\"1234\"}"}] |
482 | 500 | allow(Koala).to receive(:make_request) do |_request, args, _verb, _options|
|
483 | 501 | request_count = JSON.parse(args['batch']).length
|
484 | 502 | expect(request_count).to be <= 50 # check FB's limit
|
485 | 503 | # simulate FB's result truncation
|
486 | 504 | 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, {}) |
488 | 506 | end
|
489 | 507 | end
|
490 | 508 |
|
|
0 commit comments