describe HttpEventLogger do let(:host) { "localhost" } let(:port) { 9292 } let(:path) { "/index.html" } let(:data) { "foo=bar&bar=foo" } DRIVERS = [ HttpEventLogger::Test::Driver::Ethon, HttpEventLogger::Test::Driver::Excon, HttpEventLogger::Test::Driver::Faraday, HttpEventLogger::Test::Driver::HTTParty, HttpEventLogger::Test::Driver::HTTPClient, HttpEventLogger::Test::Driver::NetHTTP, HttpEventLogger::Test::Driver::OpenUri, HttpEventLogger::Test::Driver::Patron, HttpEventLogger::Test::Driver::Typhoeus ].freeze DRIVERS.each do |driver_class| connection_event_supported = !driver_class.is_libcurl? context "when #{driver_class.library_name} is in use" do let(:driver_class) { driver_class } let(:connection_event_supported) { connection_event_supported } let(:expected_response_type) { driver_class.expected_response_type } let(:driver) { driver_class.new(host, port, path) } context "and the defaults are configured" do it "logs GET requests" do res = driver.send_get_request expect(log).to include_message("Connected") if connection_event_supported expect(log).to include_message("Sent - GET http://#{host}:#{port}#{path}") expect(log).to include_message("Sent - Headers:") expect(log).to_not include_message("Sent - Body:") expect(log).to include_message("Benchmark:") expect(log).to include_message("Received - Status: 200") expect(log).to include_message("Received - Headers:") expect(log).to include_message("Received - Body:#{driver_class.expected_response_body}") expect(res).to be_a(expected_response_type) if expected_response_type end if driver_class.method_defined?(:send_get_request) it "logs POST requests" do res = driver.send_post_request expect(log).to include_message("Connected") if connection_event_supported expect(log).to include_message("Sent - POST http://#{host}:#{port}#{path}") expect(log).to include_message("Sent - Headers:") expect(log).to include_message("Sent - Body: #{data}") expect(log).to include_message("Benchmark:") expect(log).to include_message("Received - Status: 200") expect(log).to include_message("Received - Headers:") expect(log).to include_message("Received - Body:#{driver_class.expected_response_body}") expect(res).to be_a(expected_response_type) if expected_response_type end if driver_class.method_defined?(:send_post_request) end context "and the default logger is configured with a custom log level" do before(:example) { configure(HttpEventLogger::Logger.new(logger: @logger, severity: ::Logger::Severity::INFO)) } it "logs at the configured level" do driver.send_get_request expect(log).to include("INFO") end end context "and the default logger is configured with a uri blacklist pattern" do before(:example) do configure(HttpEventLogger::Logger.new(logger: @logger, blacklist_pattern: blacklist_pattern)) end context "and a request is made matching the pattern" do let(:blacklist_pattern) { /localhost/ } it "should not log anything" do driver.send_get_request expect(log).to be_empty end end context "and a request is made that does not match the pattern" do let(:blacklist_pattern) { /does_not_match/ } it "logs all events" do driver.send_get_request expect(log).to include_message("Sent") expect(log).to include_message("Benchmark") expect(log).to include_message("Received") end end end context "and a custom logger is configured" do before(:example) { configure(HttpEventLogger::Test::CustomLogger.new(@logger)) } it "logs connection events" do driver.send_get_request expect(log).to include("Custom - Connected: #{host}:#{port}") end if connection_event_supported it "logs request events" do driver.send_get_request expect(log).to include("Custom - Sent: Request Event") end it "logs repsonse events" do driver.send_get_request expect(log).to include("Custom - Received: Response Event") end end end end def include_message(message) include("[#{host}:#{port}] #{message}") end def configure(logger) HttpEventLogger.configuration.logger = logger end end