Skip to content

Commit

Permalink
Write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2k-joker committed Jun 15, 2023
1 parent d05a4a0 commit 217c87e
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ GEM
json (2.6.3)
method_source (1.0.0)
minitest (5.18.0)
minitest-stub_any_instance (1.0.3)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down Expand Up @@ -61,6 +62,7 @@ PLATFORMS
DEPENDENCIES
httpigeon-ruby!
minitest (~> 5.0)
minitest-stub_any_instance
pry (~> 0.13.1)
rake (~> 13.0)
rubocop (~> 1.21)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
t.test_files = FileList["test/**/*_test.rb"]
end

require "rubocop/rake_task"
Expand Down
5 changes: 3 additions & 2 deletions httpigeon-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
end
end

# spec.bindir = "exe"
# spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "faraday", "~> 2.7.6"
Expand All @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "rubocop", "~> 1.21"
spec.add_development_dependency "pry", "~> 0.13.1"
spec.add_development_dependency "minitest-stub_any_instance"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
10 changes: 7 additions & 3 deletions lib/httpigeon/logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'active_support/core_ext/hash'
require 'active_support/core_ext/object/deep_dup'
require "active_support/core_ext/hash"
require "active_support/core_ext/object/deep_dup"

module HTTPigeon
class Logger
Expand All @@ -11,7 +11,7 @@ def initialize(event_type: nil, additional_filter_keys: nil)
def log(faraday_env, data = {})
log_data = build_log_data(faraday_env, data)

HTTPigeon.event_logger.nil? ? Logger.new($stdout).info(event_type: event_type, data: log_data) : HTTPigeon.event_logger.new(event_type).log(log_data)
HTTPigeon.event_logger.nil? ? log_to_stdout : HTTPigeon.event_logger.new(event_type).log(log_data)
rescue StandardError => e
HTTPigeon.exception_notifier.notify_exception(e) if HTTPigeon.notify_all_exceptions
raise e if ['development', 'test'].include?(ENV['RAILS_ENV'].to_s)
Expand Down Expand Up @@ -97,5 +97,9 @@ def filter_hash(data)
end
end
end

def log_to_stdout
Logger.new($stdout).log(:info, { event_type: event_type, data: log_data }.to_json)
end
end
end
4 changes: 2 additions & 2 deletions lib/httpigeon/middleware/httpigeon_logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'faraday/middleware_registry'
require 'faraday/middleware'
require "faraday/middleware_registry"
require "faraday/middleware"

module HTTPigeon
module Middleware
Expand Down
8 changes: 4 additions & 4 deletions lib/httpigeon/request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'faraday'
require 'active_support/core_ext/hash'
require 'active_support/isolated_execution_state'
require 'active_support/core_ext/time'
require "faraday"
require "active_support/core_ext/hash"
require "active_support/isolated_execution_state"
require "active_support/core_ext/time"
require_relative "middleware/httpigeon_logger"

module HTTPigeon
Expand Down
15 changes: 15 additions & 0 deletions test/httpigeon/configuration_test.rb
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
126 changes: 126 additions & 0 deletions test/httpigeon/logger_test.rb
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
35 changes: 35 additions & 0 deletions test/httpigeon/request_test.rb
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
22 changes: 18 additions & 4 deletions test/test_helper.rb
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

0 comments on commit 217c87e

Please sign in to comment.