-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathevent_dispatcher_spec.rb
More file actions
200 lines (159 loc) · 7.23 KB
/
event_dispatcher_spec.rb
File metadata and controls
200 lines (159 loc) · 7.23 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# frozen_string_literal: true
#
# Copyright 2016-2017, 2019-2020, 2022, Optimizely and contributors
#
# 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.
#
require 'spec_helper'
require 'optimizely/event_dispatcher'
require 'optimizely/exceptions'
describe Optimizely::EventDispatcher do
let(:error_handler) { spy(Optimizely::NoOpErrorHandler.new) }
let(:spy_logger) { spy('logger') }
let(:proxy_config) { nil }
before(:context) do
@url = 'https://www.optimizely.com'
@params = {
'a' => '111001',
'n' => 'test_event',
'g' => '111028',
'u' => 'test_user'
}
@post_headers = {'Content-Type' => 'application/json'}
end
before(:example) do
@event_dispatcher = Optimizely::EventDispatcher.new
@customized_event_dispatcher = Optimizely::EventDispatcher.new(
logger: spy_logger, error_handler: error_handler, proxy_config: proxy_config
)
end
context 'passing in proxy config' do
let(:proxy_config) { double(:proxy_config) }
it 'should pass the proxy_config to the HttpUtils helper class' do
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
# Allow the method to be called (potentially multiple times due to retries)
allow(Optimizely::Helpers::HttpUtils).to receive(:make_request).with(
event.url,
event.http_verb,
event.params.to_json,
event.headers,
Optimizely::Helpers::Constants::EVENT_DISPATCH_CONFIG[:REQUEST_TIMEOUT],
proxy_config
).and_return(double(code: '200'))
@customized_event_dispatcher.dispatch_event(event)
# Verify it was called at least once with the correct parameters
expect(Optimizely::Helpers::HttpUtils).to have_received(:make_request).with(
event.url,
event.http_verb,
event.params.to_json,
event.headers,
Optimizely::Helpers::Constants::EVENT_DISPATCH_CONFIG[:REQUEST_TIMEOUT],
proxy_config
).at_least(:once)
end
end
it 'should properly dispatch V2 (POST) events' do
stub_request(:post, @url)
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
@event_dispatcher.dispatch_event(event)
expect(a_request(:post, @url)
.with(body: @params, headers: @post_headers)).to have_been_made.once
end
it 'should properly dispatch V2 (POST) events to http url' do
http_url = 'http://www.optimizely.com'
stub_request(:post, http_url)
event = Optimizely::Event.new(:post, http_url, @params, @post_headers)
@event_dispatcher.dispatch_event(event)
expect(a_request(:post, http_url)
.with(body: @params, headers: @post_headers)).to have_been_made.once
end
it 'should properly dispatch V2 (POST) events with timeout exception' do
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
timeout_error = Timeout::Error.new
stub_request(:post, @url).to_raise(timeout_error)
result = @event_dispatcher.dispatch_event(event)
expect(result).to eq(timeout_error)
end
it 'should properly dispatch V2 (GET) events' do
get_url = "#{@url}?a=111001&g=111028&n=test_event&u=test_user"
stub_request(:get, get_url)
event = Optimizely::Event.new(:get, get_url, @params, @post_headers)
@event_dispatcher.dispatch_event(event)
expect(a_request(:get, get_url)).to have_been_made.once
end
it 'should properly dispatch V2 (GET) events with timeout exception' do
get_url = "#{@url}?a=111001&g=111028&n=test_event&u=test_user"
event = Optimizely::Event.new(:get, get_url, @params, @post_headers)
timeout_error = Timeout::Error.new
stub_request(:get, get_url).to_raise(timeout_error)
result = @event_dispatcher.dispatch_event(event)
expect(result).to eq(timeout_error)
end
it 'should log and handle Timeout error' do
get_url = "#{@url}?a=111001&g=111028&n=test_event&u=test_user"
event = Optimizely::Event.new(:post, get_url, @params, @post_headers)
timeout_error = Timeout::Error.new
stub_request(:post, get_url).to_raise(timeout_error)
result = @customized_event_dispatcher.dispatch_event(event)
expect(result).to eq(timeout_error)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR, 'Request Timed out. Error: Timeout::Error'
).once
expect(error_handler).to have_received(:handle_error).once.with(Timeout::Error)
end
it 'should log and handle any standard error' do
get_url = "#{@url}?a=111001&g=111028&n=test_event&u=test_user"
event = Optimizely::Event.new(:post, get_url, @params, @post_headers)
stub_request(:post, get_url).to_raise(ArgumentError.new)
result = @customized_event_dispatcher.dispatch_event(event)
expect(result).to eq(nil)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR, 'Event failed to dispatch. Error: ArgumentError'
).once
expect(error_handler).to have_received(:handle_error).once.with(ArgumentError)
end
it 'should log and handle any response with status code 4xx' do
stub_request(:post, @url).to_return(status: 499)
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
@customized_event_dispatcher.dispatch_event(event)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR, 'Event failed to dispatch with response code: 499'
).once
error = Optimizely::HTTPCallError.new('HTTP Client Error: 499')
expect(error_handler).to have_received(:handle_error).once.with(error)
end
it 'should log and handle any response with status code 5xx' do
stub_request(:post, @url).to_return(status: 500)
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
@customized_event_dispatcher.dispatch_event(event)
expect(spy_logger).to have_received(:log).with(
Logger::ERROR, 'Event failed to dispatch with response code: 500'
).once
error = Optimizely::HTTPCallError.new('HTTP Server Error: 500')
expect(error_handler).to have_received(:handle_error).once.with(error)
end
it 'should do nothing on response with status code 3xx' do
stub_request(:post, @url).to_return(status: 399)
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
@customized_event_dispatcher.dispatch_event(event)
expect(spy_logger).to have_received(:log).with(Logger::DEBUG, 'event successfully sent with response code 399')
expect(error_handler).to_not have_received(:handle_error)
end
it 'should do nothing on response with status code 600' do
stub_request(:post, @url).to_return(status: 600)
event = Optimizely::Event.new(:post, @url, @params, @post_headers)
@customized_event_dispatcher.dispatch_event(event)
expect(spy_logger).to have_received(:log).with(Logger::DEBUG, 'event successfully sent with response code 600')
expect(error_handler).not_to have_received(:handle_error)
end
end