diff --git a/Gemfile b/Gemfile index 6d6958927282c875a78ba4b77dff5741cc50e157..288a90caf46c6207edf968939c14a44f54ca50bd 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ end group :test do gem 'sqlite3', :platform => [:ruby, :mswin, :mingw] + gem 'webmock' platforms :ruby do gem 'pg' diff --git a/gemfiles/Gemfile.rails3.2 b/gemfiles/Gemfile.rails3.2 index abe4a1f0c12a40bfa8274fe6d10d9274724e957f..08d6223fbd41fc8101b2031f11699ca76d6e4f71 100644 --- a/gemfiles/Gemfile.rails3.2 +++ b/gemfiles/Gemfile.rails3.2 @@ -24,6 +24,7 @@ end group :test do gem 'sqlite3', :platform => [:ruby, :mswin, :mingw] + gem 'webmock' platforms :ruby do gem 'pg' diff --git a/gemfiles/Gemfile.rails4.1 b/gemfiles/Gemfile.rails4.1 index a9459d6020e75d931dd7c4487e8aa4055a8ae6b3..681c02cae153f351fdc0e44b03e01424c58e81b1 100644 --- a/gemfiles/Gemfile.rails4.1 +++ b/gemfiles/Gemfile.rails4.1 @@ -24,6 +24,7 @@ end group :test do gem 'sqlite3', :platform => [:ruby, :mswin, :mingw] + gem 'webmock' platforms :ruby do gem 'pg' diff --git a/gemfiles/Gemfile.ruby1.9.3 b/gemfiles/Gemfile.ruby1.9.3 index dc56f6f02d1fccad7551f54628b360169112d172..64383a62c3e551a0e6b8d63d2085cd2dfbc7208b 100644 --- a/gemfiles/Gemfile.ruby1.9.3 +++ b/gemfiles/Gemfile.ruby1.9.3 @@ -16,6 +16,7 @@ group :development, :test do gem 'test-unit' # install newer version with omit() method gem 'debugger' + gem 'webmock' platforms :jruby do gem 'jruby-openssl' diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index eb398937bab3490f9aa5f28cc93cc80df660af30..ed9b946be095175856a0d7f3ff98c8737712e531 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -274,6 +274,7 @@ module Geocoder uri = URI.parse(query_url(query)) Geocoder.log(:debug, "Geocoder: HTTP request being made for #{uri.to_s}") http_client.start(uri.host, uri.port, use_ssl: use_ssl?, open_timeout: configuration.timeout, read_timeout: configuration.timeout) do |client| + configure_ssl!(client) if use_ssl? req = Net::HTTP::Get.new(uri.request_uri, configuration.http_headers) if configuration.basic_auth[:user] and configuration.basic_auth[:password] req.basic_auth( @@ -297,6 +298,8 @@ module Geocoder end end + def configure_ssl!(client); end + def check_api_key_configuration!(query) key_parts = query.lookup.required_api_key_parts if key_parts.size > Array(configuration.api_key).size diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index d3ffeb74cfd84ca9b8c52b5fcede046d788e220b..4c8157069529ee9bf10532968d5a0fe331f030c2 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -27,6 +27,17 @@ module Geocoder::Lookup private # --------------------------------------------------------------- + def configure_ssl!(client) + client.instance_eval { + @ssl_context = OpenSSL::SSL::SSLContext.new + options = OpenSSL::SSL::OP_NO_SSLv2 | OpenSSL::SSL::OP_NO_SSLv3 + if OpenSSL::SSL.const_defined?('OP_NO_COMPRESSION') + options |= OpenSSL::SSL::OP_NO_COMPRESSION + end + @ssl_context.set_params({options: options}) + } + end + def valid_response?(response) json = parse_json(response.body) status = json["status"] if json diff --git a/test/test_helper.rb b/test/test_helper.rb index 8ad22fc798c4f541acfeab13b8540fe72fba2bff..00055c80748ca686fd5277c83bff2f6fbf189cc9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -120,6 +120,9 @@ module Geocoder fixture_exists?(filename) ? filename : default_fixture_filename end + # This alias allows us to use this method in further tests + # to actually test http requests + alias_method :actual_make_api_request, :make_api_request remove_method(:make_api_request) def make_api_request(query) diff --git a/test/unit/lookups/google_test.rb b/test/unit/lookups/google_test.rb index 490b863d523ed1c777413f4f885127fbb0de9029..32f147e978268248b81d08758c08bc83d85e043a 100644 --- a/test/unit/lookups/google_test.rb +++ b/test/unit/lookups/google_test.rb @@ -111,4 +111,19 @@ class GoogleTest < GeocoderTestCase query = Geocoder::Query.new("Madison Square Garden, New York, NY") assert_match(/^https:/, query.url) end + + def test_actual_make_api_request_with_https + Geocoder.configure(use_https: true, lookup: :google) + + require 'webmock/test_unit' + WebMock.enable! + stub_all = WebMock.stub_request(:any, /.*/).to_return(status: 200) + + g = Geocoder::Lookup::Google.new + g.send(:actual_make_api_request, Geocoder::Query.new('test location')) + assert_requested(stub_all) + + WebMock.reset! + WebMock.disable! + end end