|
242 | 242 | }) |
243 | 243 | expect(@client.channels).to eq({ |
244 | 244 | :channels => { |
245 | | - "channel1" => {}, |
246 | | - "channel2" => {} |
| 245 | + :channel1 => {}, |
| 246 | + :channel2 => {} |
247 | 247 | } |
248 | 248 | }) |
249 | 249 | end |
|
274 | 274 | }) |
275 | 275 | }) |
276 | 276 | expect(@client.channel_users('mychannel')).to eq({ |
277 | | - :users => [{ 'id' => 1}] |
| 277 | + :users => [{ :id => 1}] |
| 278 | + }) |
| 279 | + end |
| 280 | + end |
| 281 | + |
| 282 | + describe '#channel_history' do |
| 283 | + it "should call correct URL and symbolise response" do |
| 284 | + api_path = %r{/apps/20/channels/mychannel/history} |
| 285 | + stub_request(:get, api_path).with( |
| 286 | + query: hash_including( |
| 287 | + "direction" => "newest_first", |
| 288 | + "limit" => "2" |
| 289 | + ) |
| 290 | + ).to_return({ |
| 291 | + :status => 200, |
| 292 | + :body => MultiJson.encode({ |
| 293 | + 'items' => [ |
| 294 | + { 'serial' => 2 }, |
| 295 | + { 'serial' => 1 } |
| 296 | + ], |
| 297 | + 'has_more' => false |
| 298 | + }) |
| 299 | + }) |
| 300 | + |
| 301 | + expect(@client.channel_history('mychannel', direction: 'newest_first', limit: 2)).to eq({ |
| 302 | + :items => [ |
| 303 | + { :serial => 2 }, |
| 304 | + { :serial => 1 } |
| 305 | + ], |
| 306 | + :has_more => false |
| 307 | + }) |
| 308 | + end |
| 309 | + end |
| 310 | + |
| 311 | + describe '#channel_presence_history' do |
| 312 | + it "should call correct URL and symbolise response" do |
| 313 | + api_path = %r{/apps/20/channels/presence-mychannel/presence/history} |
| 314 | + stub_request(:get, api_path).with( |
| 315 | + query: hash_including( |
| 316 | + "direction" => "newest_first", |
| 317 | + "limit" => "2" |
| 318 | + ) |
| 319 | + ).to_return({ |
| 320 | + :status => 200, |
| 321 | + :body => MultiJson.encode({ |
| 322 | + 'items' => [ |
| 323 | + { 'serial' => 2, 'event' => 'member_removed' }, |
| 324 | + { 'serial' => 1, 'event' => 'member_added' } |
| 325 | + ], |
| 326 | + 'has_more' => false |
| 327 | + }) |
| 328 | + }) |
| 329 | + |
| 330 | + expect(@client.channel_presence_history('presence-mychannel', direction: 'newest_first', limit: 2)).to eq({ |
| 331 | + :items => [ |
| 332 | + { :serial => 2, :event => 'member_removed' }, |
| 333 | + { :serial => 1, :event => 'member_added' } |
| 334 | + ], |
| 335 | + :has_more => false |
| 336 | + }) |
| 337 | + end |
| 338 | + end |
| 339 | + |
| 340 | + describe '#channel_presence_snapshot' do |
| 341 | + it "should call correct URL and symbolise response" do |
| 342 | + api_path = %r{/apps/20/channels/presence-mychannel/presence/history/snapshot} |
| 343 | + stub_request(:get, api_path).with( |
| 344 | + query: hash_including( |
| 345 | + "at_serial" => "4" |
| 346 | + ) |
| 347 | + ).to_return({ |
| 348 | + :status => 200, |
| 349 | + :body => MultiJson.encode({ |
| 350 | + 'channel' => 'presence-mychannel', |
| 351 | + 'members' => [{ 'user_id' => 'u-1' }], |
| 352 | + 'member_count' => 1 |
| 353 | + }) |
| 354 | + }) |
| 355 | + |
| 356 | + expect(@client.channel_presence_snapshot('presence-mychannel', at_serial: 4)).to eq({ |
| 357 | + :channel => 'presence-mychannel', |
| 358 | + :members => [{ :user_id => 'u-1' }], |
| 359 | + :member_count => 1 |
278 | 360 | }) |
279 | 361 | end |
280 | 362 | end |
|
439 | 521 | } |
440 | 522 | end |
441 | 523 |
|
442 | | - it "should not include idempotency_key header when not provided" do |
| 524 | + it "should auto-generate idempotency_key when not provided" do |
443 | 525 | @client.trigger('mychannel', 'event', {'some' => 'data'}) |
444 | 526 | expect(WebMock).to have_requested(:post, @api_path).with { |req| |
445 | 527 | parsed = MultiJson.decode(req.body) |
446 | | - expect(parsed).not_to have_key("idempotency_key") |
447 | | - expect(req.headers).not_to have_key('X-Idempotency-Key') |
| 528 | + expect(parsed["idempotency_key"]).to match(/\A[\w-]+:\d+\z/) |
| 529 | + expect(req.headers['X-Idempotency-Key']).to eq(parsed["idempotency_key"]) |
448 | 530 | } |
449 | 531 | end |
450 | 532 | end |
|
470 | 552 | ) |
471 | 553 | expect(WebMock).to have_requested(:post, @api_path).with { |req| |
472 | 554 | parsed = MultiJson.decode(req.body) |
473 | | - expect(parsed).to eq( |
474 | | - "batch" => [ |
475 | | - { "channel" => "mychannel", "name" => "event", "data" => "{\"some\":\"data\"}"}, |
476 | | - { "channel" => "mychannel", "name" => "event", "data" => "already encoded"} |
477 | | - ] |
478 | | - ) |
| 555 | + batch = parsed["batch"] |
| 556 | + expect(batch[0]["channel"]).to eq("mychannel") |
| 557 | + expect(batch[0]["name"]).to eq("event") |
| 558 | + expect(batch[0]["data"]).to eq("{\"some\":\"data\"}") |
| 559 | + expect(batch[0]["idempotency_key"]).to match(/\A[\w-]+:\d+:0\z/) |
| 560 | + expect(batch[1]["channel"]).to eq("mychannel") |
| 561 | + expect(batch[1]["name"]).to eq("event") |
| 562 | + expect(batch[1]["data"]).to eq("already encoded") |
| 563 | + expect(batch[1]["idempotency_key"]).to match(/\A[\w-]+:\d+:1\z/) |
479 | 564 | } |
480 | 565 | end |
481 | 566 |
|
|
528 | 613 | } |
529 | 614 | end |
530 | 615 |
|
531 | | - it "should preserve idempotency_key per event in batch" do |
| 616 | + it "should preserve explicit idempotency_key and auto-generate missing ones in batch" do |
532 | 617 | @client.trigger_batch( |
533 | 618 | {channel: 'mychannel', name: 'event', data: 'foo', idempotency_key: 'key-1'}, |
534 | 619 | {channel: 'mychannel', name: 'event2', data: 'bar'}, |
535 | 620 | ) |
536 | 621 | expect(WebMock).to have_requested(:post, @api_path).with { |req| |
537 | 622 | batch = MultiJson.decode(req.body)["batch"] |
538 | 623 | expect(batch[0]["idempotency_key"]).to eq("key-1") |
539 | | - expect(batch[1]).not_to have_key("idempotency_key") |
| 624 | + expect(batch[1]["idempotency_key"]).to match(/\A[\w-]+:\d+:1\z/) |
540 | 625 | } |
541 | 626 | end |
542 | 627 | end |
|
611 | 696 | :body => MultiJson.encode({'something' => {'a' => 'hash'}}) |
612 | 697 | }) |
613 | 698 | expect(call_api).to eq({ |
614 | | - :something => {'a' => 'hash'} |
| 699 | + :something => { :a => 'hash' } |
615 | 700 | }) |
616 | 701 | end |
617 | 702 |
|
|
743 | 828 | }) |
744 | 829 | call_api.callback { |response| |
745 | 830 | expect(response).to eq({ |
746 | | - :something => {'a' => 'hash'} |
| 831 | + :something => { :a => 'hash' } |
747 | 832 | }) |
748 | 833 | EM.stop |
749 | 834 | } |
|
0 commit comments