From ee5ee8525704496d551757ddcb9482d1d42561dd Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Wed, 19 Aug 2015 08:46:06 -0400
Subject: [PATCH] Force :google to use HTTPS if API key set.

---
 lib/geocoder/lookups/google.rb           | 9 +++++++++
 test/unit/https_test.rb                  | 2 +-
 test/unit/lookups/google_premier_test.rb | 2 +-
 test/unit/lookups/google_test.rb         | 5 +++++
 test/unit/proxy_test.rb                  | 8 ++++----
 5 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb
index 74d273de..831b619f 100644
--- a/lib/geocoder/lookups/google.rb
+++ b/lib/geocoder/lookups/google.rb
@@ -12,6 +12,15 @@ module Geocoder::Lookup
       "http://maps.google.com/maps?q=#{coordinates.join(',')}"
     end
 
+    def supported_protocols
+      # Google requires HTTPS if an API key is used.
+      if configuration.api_key
+        [:https]
+      else
+        [:http, :https]
+      end
+    end
+
     def query_url(query)
       "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query)
     end
diff --git a/test/unit/https_test.rb b/test/unit/https_test.rb
index 6392f2a1..dbdbc75e 100644
--- a/test/unit/https_test.rb
+++ b/test/unit/https_test.rb
@@ -10,7 +10,7 @@ class HttpsTest < GeocoderTestCase
   end
 
   def test_uses_http_by_default
-    g = Geocoder::Lookup::Google.new
+    g = Geocoder::Lookup::Esri.new
     assert_match(/^http:/, g.query_url(Geocoder::Query.new("test")))
   end
 end
diff --git a/test/unit/lookups/google_premier_test.rb b/test/unit/lookups/google_premier_test.rb
index c96c4a1f..ceb5e8c6 100644
--- a/test/unit/lookups/google_premier_test.rb
+++ b/test/unit/lookups/google_premier_test.rb
@@ -16,6 +16,6 @@ class GooglePremierTest < GeocoderTestCase
   def test_query_url
     Geocoder.configure(google_premier: {api_key: ["deadbeef", "gme-test", "test-dev"]})
     query = Geocoder::Query.new("Madison Square Garden, New York, NY")
-    assert_equal "http://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=", query.url
+    assert_equal "https://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=", query.url
   end
 end
diff --git a/test/unit/lookups/google_test.rb b/test/unit/lookups/google_test.rb
index 0581fbf1..d7f72e76 100644
--- a/test/unit/lookups/google_test.rb
+++ b/test/unit/lookups/google_test.rb
@@ -80,4 +80,9 @@ class GoogleTest < GeocoderTestCase
     assert url.include?(formatted), "Expected #{formatted} to be included in #{url}"
   end
 
+  def test_google_uses_https_when_api_key_is_set
+    Geocoder.configure(api_key: "deadbeef")
+    query = Geocoder::Query.new("Madison Square Garden, New York, NY")
+    assert_match /^https:/, query.url
+  end
 end
diff --git a/test/unit/proxy_test.rb b/test/unit/proxy_test.rb
index 23d8789a..4f671542 100644
--- a/test/unit/proxy_test.rb
+++ b/test/unit/proxy_test.rb
@@ -5,25 +5,25 @@ class ProxyTest < GeocoderTestCase
 
   def test_uses_proxy_when_specified
     Geocoder.configure(:http_proxy => 'localhost')
-    lookup = Geocoder::Lookup::Google.new
+    lookup = Geocoder::Lookup::Esri.new
     assert lookup.send(:http_client).proxy_class?
   end
 
   def test_doesnt_use_proxy_when_not_specified
-    lookup = Geocoder::Lookup::Google.new
+    lookup = Geocoder::Lookup::Esri.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::Google.new.send(:http_client)
+      Geocoder::Lookup::Esri.new.send(:http_client)
     end
   end
 
   def test_accepts_proxy_with_http_protocol
     Geocoder.configure(:http_proxy => 'http://localhost')
-    lookup = Geocoder::Lookup::Google.new
+    lookup = Geocoder::Lookup::Esri.new
     assert lookup.send(:http_client).proxy_class?
   end
 
-- 
GitLab