def test_long_poll_for_events_and_errors(self): client = BoxClient('my_token') longpoll_response = { 'type': 'realtime_server', 'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all', 'ttl': '10', 'max_retries': '10', 'retry_timeout': 610 } (flexmock(client) .should_receive('_get_long_poll_data') .and_return(longpoll_response) .times(2)) expected_get_params = { 'channel': ['12345678'], 'stream_type': 'changes', 'stream_position': 'some_stream_position', } (flexmock(requests) .should_receive('get') .with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params) .and_return(mocked_response({'message': 'foo'})) .and_return(mocked_response('some error', status_code=400)) .times(2)) with self.assertRaises(BoxClientException) as expect_exception: client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES) self.assertEqual(400, expect_exception.exception.status_code) self.assertEqual('some error', expect_exception.exception.message)
def test_long_poll_for_events_and_errors(self): client = BoxClient("my_token") longpoll_response = { "type": "realtime_server", "url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all", "ttl": "10", "max_retries": "10", "retry_timeout": 610, } (flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(2)) expected_get_params = { "channel": ["12345678"], "stream_type": "changes", "stream_position": "some_stream_position", } ( flexmock(requests) .should_receive("get") .with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params) .and_return(mocked_response({"message": "foo"})) .and_return(mocked_response("some error", status_code=400)) .times(2) ) with self.assertRaises(BoxClientException) as expect_exception: client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES) self.assertEqual(400, expect_exception.exception.status_code) self.assertEqual("some error", expect_exception.exception.message)
def test_long_poll_for_events_ok(self): client = BoxClient('my_token') longpoll_response = { 'type': 'realtime_server', 'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all', 'ttl': '10', 'max_retries': '10', 'retry_timeout': 610 } (flexmock(client) .should_receive('_get_long_poll_data') .and_return(longpoll_response) .once()) expected_get_params = { 'channel': ['12345678'], 'stream_type': 'changes', 'stream_position': 'some_stream_position', } (flexmock(requests) .should_receive('get') .with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params) .and_return(mocked_response({'message': 'new_message'})) .once()) position = client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES) self.assertEqual('some_stream_position', position)
def test_long_poll_for_events_multiple_tries(self): client = BoxClient("my_token") longpoll_response = { "type": "realtime_server", "url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all", "ttl": "10", "max_retries": "10", "retry_timeout": 610, } (flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(5)) (flexmock(client).should_receive("get_events").times(0)) expected_get_params = { "channel": ["12345678"], "stream_type": "changes", "stream_position": "some_stream_position", } ( flexmock(requests) .should_receive("get") .with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params) .and_return(mocked_response({"message": "foo"})) .and_return(mocked_response({"message": "foo"})) .and_return(mocked_response({"message": "foo"})) .and_return(mocked_response({"message": "foo"})) .and_return(mocked_response({"message": "new_message"})) .times(5) ) position = client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES) self.assertEqual("some_stream_position", position)
def test_long_poll_for_latest_events(self): for stream_position in ['now', None]: client = BoxClient('my_token') longpoll_response = { 'type': 'realtime_server', 'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all', 'ttl': '10', 'max_retries': '10', 'retry_timeout': 610 } expected_get_params = { 'channel': ['12345678'], 'stream_type': 'changes', 'stream_position': 'some_stream_position', } flexmock(client) \ .should_receive('_get_long_poll_data') \ .and_return(longpoll_response) \ .once() flexmock(client) \ .should_receive('get_events') \ .with_args(stream_position='now', stream_type=EventFilter.CHANGES) \ .and_return({'next_stream_position': 'some_stream_position'}) \ .once() flexmock(requests) \ .should_receive('get') \ .with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params) \ .and_return(self.make_response(content={'message': 'new_message'})) \ .once() position = client.long_poll_for_events(stream_position, stream_type=EventFilter.CHANGES) self.assertEqual('some_stream_position', position)