-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest_event_dispatcher.py
More file actions
82 lines (67 loc) · 3.32 KB
/
test_event_dispatcher.py
File metadata and controls
82 lines (67 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2016, 2018, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
import json
import unittest
from requests import exceptions as request_exception
from optimizely import event_builder
from optimizely import event_dispatcher
from optimizely.helpers.enums import EventDispatchConfig
class EventDispatcherTest(unittest.TestCase):
def test_dispatch_event__get_request(self):
""" Test that dispatch event fires off requests call with provided URL and params. """
url = 'https://www.optimizely.com'
params = {'a': '111001', 'n': 'test_event', 'g': '111028', 'u': 'oeutest_user'}
event = event_builder.Event(url, params)
with mock.patch('requests.Session.get') as mock_request_get:
event_dispatcher.EventDispatcher.dispatch_event(event)
mock_request_get.assert_called_once_with(url, params=params, timeout=EventDispatchConfig.REQUEST_TIMEOUT)
def test_dispatch_event__post_request(self):
""" Test that dispatch event fires off requests call with provided URL, params, HTTP verb and headers. """
url = 'https://www.optimizely.com'
params = {
'accountId': '111001',
'eventName': 'test_event',
'eventEntityId': '111028',
'visitorId': 'oeutest_user',
}
event = event_builder.Event(url, params, http_verb='POST', headers={'Content-Type': 'application/json'})
with mock.patch('requests.Session.post') as mock_request_post:
event_dispatcher.EventDispatcher.dispatch_event(event)
mock_request_post.assert_called_once_with(
url,
data=json.dumps(params),
headers={'Content-Type': 'application/json'},
timeout=EventDispatchConfig.REQUEST_TIMEOUT,
)
def test_dispatch_event__handle_request_exception(self):
""" Test that dispatch event handles exceptions and logs error. """
url = 'https://www.optimizely.com'
params = {
'accountId': '111001',
'eventName': 'test_event',
'eventEntityId': '111028',
'visitorId': 'oeutest_user',
}
event = event_builder.Event(url, params, http_verb='POST', headers={'Content-Type': 'application/json'})
with mock.patch(
'requests.Session.post', side_effect=request_exception.RequestException('Failed Request'),
) as mock_request_post, mock.patch('logging.error') as mock_log_error:
event_dispatcher.EventDispatcher.dispatch_event(event)
mock_request_post.assert_called_once_with(
url,
data=json.dumps(params),
headers={'Content-Type': 'application/json'},
timeout=EventDispatchConfig.REQUEST_TIMEOUT,
)
mock_log_error.assert_called_once_with('Dispatch event failed. Error: Failed Request')