|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | #
|
4 |
| -# Copyright 2016-2017, Optimizely and contributors |
| 4 | +# Copyright 2016-2017, 2019, Optimizely and contributors |
5 | 5 | #
|
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License");
|
7 | 7 | # you may not use this file except in compliance with the License.
|
|
16 | 16 | # limitations under the License.
|
17 | 17 | #
|
18 | 18 | require 'spec_helper'
|
19 |
| -require 'webmock' |
20 |
| -require 'optimizely/event_builder' |
21 | 19 | require 'optimizely/event_dispatcher'
|
| 20 | +require 'optimizely/exceptions' |
22 | 21 |
|
23 | 22 | describe Optimizely::EventDispatcher do
|
| 23 | + let(:error_handler) { spy(Optimizely::NoOpErrorHandler.new) } |
| 24 | + let(:spy_logger) { spy('logger') } |
| 25 | + |
24 | 26 | before(:context) do
|
25 | 27 | @url = 'https://www.optimizely.com'
|
26 | 28 | @params = {
|
|
34 | 36 |
|
35 | 37 | before(:example) do
|
36 | 38 | @event_dispatcher = Optimizely::EventDispatcher.new
|
| 39 | + @customized_event_dispatcher = Optimizely::EventDispatcher.new( |
| 40 | + logger: spy_logger, error_handler: error_handler |
| 41 | + ) |
37 | 42 | end
|
38 | 43 |
|
39 | 44 | it 'should properly dispatch V2 (POST) events' do
|
|
64 | 69 | expect(a_request(:get, get_url)).to have_been_made.once
|
65 | 70 | end
|
66 | 71 |
|
67 |
| - it 'should properly dispatch V2 (GET) events' do |
| 72 | + it 'should properly dispatch V2 (GET) events with timeout exception' do |
68 | 73 | get_url = @url + '?a=111001&g=111028&n=test_event&u=test_user'
|
69 | 74 | stub_request(:get, get_url)
|
70 | 75 | event = Optimizely::Event.new(:get, get_url, @params, @post_headers)
|
|
74 | 79 |
|
75 | 80 | expect(result).to eq(timeout_error)
|
76 | 81 | end
|
| 82 | + |
| 83 | + it 'should log and handle Timeout error' do |
| 84 | + get_url = @url + '?a=111001&g=111028&n=test_event&u=test_user' |
| 85 | + stub_request(:post, get_url) |
| 86 | + event = Optimizely::Event.new(:post, get_url, @params, @post_headers) |
| 87 | + timeout_error = Timeout::Error.new |
| 88 | + allow(HTTParty).to receive(:post).with(any_args).and_raise(timeout_error) |
| 89 | + result = @customized_event_dispatcher.dispatch_event(event) |
| 90 | + |
| 91 | + expect(result).to eq(timeout_error) |
| 92 | + expect(spy_logger).to have_received(:log).with( |
| 93 | + Logger::ERROR, 'Request Timed out. Error: Timeout::Error' |
| 94 | + ).once |
| 95 | + |
| 96 | + expect(error_handler).to have_received(:handle_error).once.with(Timeout::Error) |
| 97 | + end |
| 98 | + |
| 99 | + it 'should log and handle any standard error' do |
| 100 | + get_url = @url + '?a=111001&g=111028&n=test_event&u=test_user' |
| 101 | + stub_request(:post, get_url) |
| 102 | + event = Optimizely::Event.new(:post, get_url, @params, @post_headers) |
| 103 | + error = ArgumentError |
| 104 | + allow(HTTParty).to receive(:post).with(any_args).and_raise(error) |
| 105 | + result = @customized_event_dispatcher.dispatch_event(event) |
| 106 | + |
| 107 | + expect(result).to eq(nil) |
| 108 | + expect(spy_logger).to have_received(:log).with( |
| 109 | + Logger::ERROR, 'Event failed to dispatch. Error: ArgumentError' |
| 110 | + ).once |
| 111 | + |
| 112 | + expect(error_handler).to have_received(:handle_error).once.with(ArgumentError) |
| 113 | + end |
| 114 | + |
| 115 | + it 'should log and handle any response with status code 4xx' do |
| 116 | + stub_request(:post, @url).to_return(status: 499) |
| 117 | + event = Optimizely::Event.new(:post, @url, @params, @post_headers) |
| 118 | + |
| 119 | + @customized_event_dispatcher.dispatch_event(event) |
| 120 | + |
| 121 | + expect(spy_logger).to have_received(:log).with( |
| 122 | + Logger::ERROR, 'Event failed to dispatch with response code: 499' |
| 123 | + ).once |
| 124 | + |
| 125 | + error = Optimizely::HTTPCallError.new('HTTP Client Error: 499') |
| 126 | + expect(error_handler).to have_received(:handle_error).once.with(error) |
| 127 | + end |
| 128 | + |
| 129 | + it 'should log and handle any response with status code 5xx' do |
| 130 | + stub_request(:post, @url).to_return(status: 500) |
| 131 | + event = Optimizely::Event.new(:post, @url, @params, @post_headers) |
| 132 | + |
| 133 | + @customized_event_dispatcher.dispatch_event(event) |
| 134 | + |
| 135 | + expect(spy_logger).to have_received(:log).with( |
| 136 | + Logger::ERROR, 'Event failed to dispatch with response code: 500' |
| 137 | + ).once |
| 138 | + |
| 139 | + error = Optimizely::HTTPCallError.new('HTTP Server Error: 500') |
| 140 | + expect(error_handler).to have_received(:handle_error).once.with(error) |
| 141 | + end |
| 142 | + |
| 143 | + it 'should do nothing on response with status code 3xx' do |
| 144 | + stub_request(:post, @url).to_return(status: 399) |
| 145 | + event = Optimizely::Event.new(:post, @url, @params, @post_headers) |
| 146 | + |
| 147 | + response = @customized_event_dispatcher.dispatch_event(event) |
| 148 | + |
| 149 | + expect(response).to be_nil |
| 150 | + expect(spy_logger).not_to have_received(:log) |
| 151 | + expect(error_handler).not_to have_received(:handle_error) |
| 152 | + end |
| 153 | + |
| 154 | + it 'should do nothing on response with status code 600' do |
| 155 | + stub_request(:post, @url).to_return(status: 600) |
| 156 | + event = Optimizely::Event.new(:post, @url, @params, @post_headers) |
| 157 | + |
| 158 | + response = @customized_event_dispatcher.dispatch_event(event) |
| 159 | + |
| 160 | + expect(response).to be_nil |
| 161 | + expect(spy_logger).not_to have_received(:log) |
| 162 | + expect(error_handler).not_to have_received(:handle_error) |
| 163 | + end |
77 | 164 | end
|
0 commit comments