Back to Repositories

Testing HTTP Proxy Configuration in Geocoder

This test suite validates HTTP proxy functionality in the Geocoder gem, ensuring proper handling of proxy configurations for geocoding requests. The tests verify proxy setup, protocol support, and error handling across different lookup services.

Test Coverage Overview

The test suite provides comprehensive coverage of proxy-related functionality in Geocoder.

Key areas tested include:
  • Basic proxy configuration validation
  • HTTP and HTTPS protocol support
  • Proxy error handling
  • Integration with different lookup services (Bing, Google)

Implementation Analysis

The testing approach uses Ruby’s unit testing framework to validate proxy configuration behavior. Tests employ mock HTTP clients and configuration settings to verify proxy implementation across different scenarios.

Key patterns include:
  • Configuration state management
  • Protocol-specific proxy validation
  • Error condition verification

Technical Details

Testing infrastructure includes:
  • Ruby’s built-in test framework
  • GeocoderTestCase custom test class
  • Proxy configuration methods
  • HTTP client validation utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific functionality
  • Clear error scenario coverage
  • Consistent test naming conventions
  • Proper setup and teardown of configuration state

alexreisner/geocoder

test/unit/proxy_test.rb

            
# encoding: utf-8
require 'test_helper'

class ProxyTest < GeocoderTestCase

  def test_uses_proxy_when_specified
    Geocoder.configure(:http_proxy => 'localhost')
    lookup = Geocoder::Lookup::Bing.new
    assert lookup.send(:http_client).proxy_class?
  end

  def test_doesnt_use_proxy_when_not_specified
    lookup = Geocoder::Lookup::Bing.new
    assert !lookup.send(:http_client).proxy_class?
  end

  def test_exception_raised_on_bad_proxy_url
    Geocoder.configure(:http_proxy => ' \\_O< Quack Quack')
    assert_raise Geocoder::ConfigurationError do
      Geocoder::Lookup::Bing.new.send(:http_client)
    end
  end

  def test_accepts_proxy_with_http_protocol
    Geocoder.configure(:http_proxy => 'http://localhost')
    lookup = Geocoder::Lookup::Bing.new
    assert lookup.send(:http_client).proxy_class?
  end

  def test_accepts_proxy_with_https_protocol
    Geocoder.configure(:https_proxy => 'https://localhost')
    Geocoder.configure(:use_https => true)
    lookup = Geocoder::Lookup::Google.new
    assert lookup.send(:http_client).proxy_class?
  end
end