-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
213 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require_relative "../test_helper" | ||
|
||
class HTTPigeon::ConfigurationTest < HTTPigeon::TestCase | ||
describe '#new' do | ||
it 'sets the expected defaults' do | ||
config = HTTPigeon::Configuration.new | ||
|
||
assert_equal 'http.outbound', config.default_event_type | ||
assert_empty config.default_filter_keys | ||
assert_nil config.event_logger | ||
assert_nil config.exception_notifier | ||
refute config.notify_all_exceptions | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
require_relative "../test_helper" | ||
|
||
class HTTPigeon::LoggerTest < HTTPigeon::TestCase | ||
describe '#log' do | ||
let(:event_type) { nil } | ||
let(:filter_keys) { %w[account_number ssn X-Subscription-Key x-api-token] } | ||
let(:logger) { HTTPigeon::Logger.new(event_type: event_type, additional_filter_keys: filter_keys) } | ||
let(:error) { TypeError.new('Not my type') } | ||
let(:base_data) { { something: 'important', error: error } } | ||
let(:response_status) { 200 } | ||
let(:faraday_env) do | ||
OpenStruct.new( | ||
{ | ||
method: 'post', | ||
url: OpenStruct.new( | ||
{ | ||
to_s: 'http://example.com/home', | ||
host: 'example.com', | ||
path: 'home', | ||
scheme: 'https' | ||
} | ||
), | ||
request_headers: { 'X-Request-Id' => 'abc-012-xyz-789', 'X-Subscription-Key' => 'super-secret-key', 'X-API-Token' => 'super-top-secret-token' }, | ||
request_body: { foo: 'barzz' }, | ||
response_headers: { 'X-Request-Id' => 'abc-012-xyz-789' }, | ||
response_body: response_body, | ||
status: response_status | ||
} | ||
) | ||
end | ||
|
||
let(:log_payload) do | ||
{ | ||
something: 'important', | ||
request: { | ||
method: 'post', | ||
url: 'http://example.com/home', | ||
headers: { 'X-Request-Id' => 'abc-012-xyz-789', 'X-Subscription-Key' => '[FILTERED]', 'X-API-Token' => '[FILTERED]' }, | ||
body: { foo: 'barzz' }, | ||
host: 'example.com', | ||
path: 'home' | ||
}, | ||
response: { | ||
headers: { 'X-Request-Id' => 'abc-012-xyz-789' }, | ||
body: filtered_response_body, | ||
status: response_status | ||
}, | ||
metadata: { | ||
latency: nil, | ||
identifier: 'abc-012-xyz-789', | ||
protocol: 'https' | ||
}, | ||
error: { | ||
type: 'TypeError', | ||
message: 'Not my type', | ||
backtrace: error.backtrace.last(10) | ||
} | ||
} | ||
end | ||
|
||
before { error.set_backtrace(caller) } | ||
|
||
context 'when the response body is valid JSON' do | ||
let(:event_type) { 'custom.event' } | ||
let(:response_body) { { account_number: '0000000100100011', ssn: '123-45-6789', ifdis: 'dendat' } } | ||
let(:filtered_response_body) { { account_number: '[FILTERED]', ssn: '[FILTERED]', ifdis: 'dendat' } } | ||
|
||
context 'when there is a custom event logger' do | ||
before do | ||
class MyCustomLogger | ||
def initialize(event_type); end | ||
|
||
def log(data = {}); end | ||
end | ||
end | ||
|
||
after { HTTPigeon::LoggerTest.send :remove_const, "MyCustomLogger" } | ||
|
||
it 'logs the filtered payload using the custom event logger' do | ||
HTTPigeon.configure { |c| c.event_logger = MyCustomLogger } | ||
|
||
logger_mock = Minitest::Mock.new | ||
logger_mock.expect(:log, nil, [log_payload]) | ||
|
||
event_logger_on_new = ->(e_type) { assert_equal e_type, event_type; logger_mock } | ||
|
||
HTTPigeon.event_logger.stub(:new, event_logger_on_new) do | ||
logger.log(faraday_env, base_data) | ||
|
||
assert_mock logger_mock | ||
end | ||
end | ||
end | ||
|
||
context 'when there is no custom event logger' do | ||
it 'logs the filtered payload with ruby logger' do | ||
on_log = ->(*args) { assert_equal [:info, { event_type: event_type, data: log_payload }.to_json, nil], args } | ||
ruby_logger_on_new = ->(arg) { assert_equal $stdout, arg } | ||
|
||
Logger.stub(:new, ruby_logger_on_new) do | ||
Logger.stub_any_instance(:log, on_log) do | ||
logger.log(faraday_env, base_data) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'when the response body is invalid JSON' do | ||
let(:response_status) { 400 } | ||
let(:response_body) { 'not found' } | ||
let(:filtered_response_body) { response_body } | ||
|
||
it 'logs the original payload' do | ||
on_log = ->(*args) { assert_equal [:info, { event_type: HTTPigeon.default_event_type, data: log_payload }.to_json, nil], args } | ||
ruby_logger_on_new = ->(arg) { assert_equal $stdout, arg } | ||
|
||
Logger.stub(:new, ruby_logger_on_new) do | ||
Logger.stub_any_instance(:log, on_log) do | ||
logger.log(faraday_env, base_data) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative "../test_helper" | ||
|
||
class HTTPigeon::RequestTest < HTTPigeon::TestCase | ||
describe '#new' do | ||
context 'when a custom logger is not provided' do | ||
it 'uses the default :httpigeon_logger' do | ||
event_type = 'some.event' | ||
filter_keys = [:super_secret] | ||
logger_mock = Minitest::Mock.new | ||
logger_mock.expect(:call, nil) { |args| args[:event_type] == event_type && args[:additional_filter_keys] == filter_keys } | ||
|
||
HTTPigeon::Logger.stub(:new, logger_mock) do | ||
request = HTTPigeon::Request.new(base_url: 'https://www.example.com', event_type: event_type, filter_keys: filter_keys) | ||
|
||
assert_mock logger_mock | ||
assert_equal 'application/json', request.connection.headers['Accept'] | ||
end | ||
end | ||
end | ||
|
||
context 'when a custom logger is provided' do | ||
it 'uses the custom logger' do | ||
logger = Logger.new($stdout) | ||
logger_mock = Minitest::Mock.new | ||
logger_mock.expect(:call, nil) { |args| args.keys == %i[event_type filter_keys] } | ||
|
||
HTTPigeon::Logger.stub(:new, logger_mock) do | ||
HTTPigeon::Request.new(base_url: 'http://www.example.com', logger: logger) | ||
|
||
refute_mock logger_mock | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.unshift File.expand_path("../lib", __dir__) | ||
require "httpigeon/ruby" | ||
|
||
require "httpigeon" | ||
require "minitest/autorun" | ||
require "minitest/stub_any_instance" | ||
require "pry" | ||
|
||
class HTTPigeon::TestCase < Minitest::Spec | ||
def self.context(...) | ||
describe(...) | ||
end | ||
|
||
def self.let!(name, &block) | ||
let(name, &block) | ||
instance_eval { setup { send(name) } } | ||
end | ||
|
||
def refute_mock(mock) | ||
assert_raises(MockExpectationError) { mock.verify } | ||
end | ||
end |