|
1 | 1 | import mock
|
2 | 2 | import json
|
3 | 3 | import unittest
|
| 4 | +from requests import exceptions as request_exception |
4 | 5 |
|
5 | 6 | from optimizely import event_builder
|
6 | 7 | from optimizely import event_dispatcher
|
@@ -43,3 +44,25 @@ def test_dispatch_event__post_request(self):
|
43 | 44 | mock_request_post.assert_called_once_with(url, data=json.dumps(params),
|
44 | 45 | headers={'Content-Type': 'application/json'},
|
45 | 46 | timeout=event_dispatcher.REQUEST_TIMEOUT)
|
| 47 | + |
| 48 | + def test_dispatch_event__handle_request_exception(self): |
| 49 | + """ Test that dispatch event handles exceptions and logs error. """ |
| 50 | + |
| 51 | + url = 'https://www.optimizely.com' |
| 52 | + params = { |
| 53 | + 'accountId': '111001', |
| 54 | + 'eventName': 'test_event', |
| 55 | + 'eventEntityId': '111028', |
| 56 | + 'visitorId': 'oeutest_user' |
| 57 | + } |
| 58 | + event = event_builder.Event(url, params, http_verb='POST', headers={'Content-Type': 'application/json'}) |
| 59 | + |
| 60 | + with mock.patch('requests.post', |
| 61 | + side_effect=request_exception.RequestException('Failed Request')) as mock_request_post,\ |
| 62 | + mock.patch('logging.error') as mock_log_error: |
| 63 | + event_dispatcher.EventDispatcher.dispatch_event(event) |
| 64 | + |
| 65 | + mock_request_post.assert_called_once_with(url, data=json.dumps(params), |
| 66 | + headers={'Content-Type': 'application/json'}, |
| 67 | + timeout=event_dispatcher.REQUEST_TIMEOUT) |
| 68 | + mock_log_error.assert_called_once_with('Dispatch event failed. Error: Failed Request') |
0 commit comments