Back to Repositories

Testing HTTParty Logger Formatter Configuration in jnunemaker/httparty

This test suite validates the logging functionality in HTTParty, focusing on the Logger class implementation and its various formatting options. The tests ensure proper initialization of logger instances and verify the correct handling of different logging formats including Apache, cURL, and Logstash.

Test Coverage Overview

The test suite provides comprehensive coverage of HTTParty’s Logger class initialization and formatter selection. Key functionality tested includes:

  • Default logging level verification
  • Default formatter configuration
  • Support for multiple formatter styles (Apache, cURL, Logstash)
  • Custom formatter registration and validation
  • Error handling for duplicate formatter registration

Implementation Analysis

The testing approach utilizes RSpec’s describe and context blocks to organize test cases logically. The implementation leverages RSpec doubles for mocking logger instances and employs expectation matchers to verify object types and behavior. The test structure follows a clear pattern of validating both default behaviors and specific formatter configurations.

Technical Details

Testing tools and configuration:

  • RSpec testing framework
  • Double mocking for logger instances
  • Custom formatter class definition
  • Error handling validation
  • Instance type checking

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Isolated test cases for each formatter type
  • Proper use of subject for consistent testing context
  • Clear test descriptions that specify expected behavior
  • Effective use of RSpec doubles to mock dependencies
  • Comprehensive error case coverage

jnunemaker/httparty

spec/httparty/logger/logger_spec.rb

            
require 'spec_helper'

RSpec.describe HTTParty::Logger do
  describe ".build" do
    subject { HTTParty::Logger }

    it "defaults level to :info" do
      logger_double = double
      expect(subject.build(logger_double, nil, nil).level).to eq(:info)
    end

    it "defaults format to :apache" do
      logger_double = double
      expect(subject.build(logger_double, nil, nil)).to be_an_instance_of(HTTParty::Logger::ApacheFormatter)
    end

    it "builds :curl style logger" do
      logger_double = double
      expect(subject.build(logger_double, nil, :curl)).to be_an_instance_of(HTTParty::Logger::CurlFormatter)
    end

    it "builds :logstash style logger" do
      logger_double = double
      expect(subject.build(logger_double, nil, :logstash)).to be_an_instance_of(HTTParty::Logger::LogstashFormatter)
    end

    it "builds :custom style logger" do
      CustomFormatter = Class.new(HTTParty::Logger::CurlFormatter)
      HTTParty::Logger.add_formatter(:custom, CustomFormatter)

      logger_double = double
      expect(subject.build(logger_double, nil, :custom)).
        to be_an_instance_of(CustomFormatter)
    end
    it "raises error when formatter exists" do
      CustomFormatter2= Class.new(HTTParty::Logger::CurlFormatter)
      HTTParty::Logger.add_formatter(:custom2, CustomFormatter2)

      expect{ HTTParty::Logger.add_formatter(:custom2, CustomFormatter2) }.
        to raise_error HTTParty::Error
    end
  end
end