]> git.openstreetmap.org Git - rails.git/commitdiff
Use webmock to power the with_http_stubs helper
authorAndy Allan <git@gravitystorm.co.uk>
Sat, 29 Oct 2016 17:40:48 +0000 (19:40 +0200)
committerAndy Allan <git@gravitystorm.co.uk>
Sun, 30 Oct 2016 09:29:16 +0000 (10:29 +0100)
This involves a small amount of changing the fixtures since we're
using the regexp and not the full url matching powers of webmock.

lib/osm.rb
test/http/geocoder_ca.yml
test/http/nominatim.yml
test/http/npemap.yml
test/test_helper.rb

index cd3a2156c82b5bfa89f46567776f233d0c48ac41..370f0f30009c2a09cafa3ba7ea5847a21636c372 100644 (file)
@@ -569,11 +569,6 @@ module OSM
     @http_client ||= Faraday.new
   end
 
-  # Set the HTTP client to use
-  def self.http_client=(client)
-    @http_client = client
-  end
-
   # Return the GeoIP database handle
   def self.geoip_database
     @geoip_database ||= GeoIP.new(GEOIP_DATABASE) if defined?(GEOIP_DATABASE)
index 88a3fdc94a8d5040febd86ee2ff039940e4f92a5..0fbaa105d84b6e679ac18be0833a0138960a6d66 100644 (file)
@@ -1,5 +1,5 @@
-/?geoit=XML&postal=A1B+2C3: 
-  code: 200 
+/?geoit=XML&postal=A1B%202C3:
+  code: 200
   body: |
     <?xml version="1.0" encoding="UTF-8"?>
     <geodata>
@@ -15,8 +15,8 @@
       </standard>
     </geodata>
 
-/?geoit=XML&postal=k1a+0b1:
-  code: 200 
+/?geoit=XML&postal=k1a%200b1:
+  code: 200
   body: |
     <?xml version="1.0" encoding="UTF-8"?>
     <geodata>
@@ -31,9 +31,9 @@
         <confidence>0.9</confidence>
       </standard>
     </geodata>
-  
-/?geoit=XML&postal=Q0Q+0Q0:
-  code: 200 
+
+/?geoit=XML&postal=Q0Q%200Q0:
+  code: 200
   body: |
     <?xml version="1.0" encoding="UTF-8"?>
     <geodata>
index 41467721f414d17b0d231445b0b7a87c816ddcc5..accaebf33aae157a4f966b80cf98c5a2bc5f6055 100644 (file)
@@ -1,12 +1,12 @@
-/search?accept-language=&format=xml&q=Hoddesdon&viewbox=-0.559%2C51.766%2C0.836%2C51.217:
+/search?accept-language=&extratags=1&format=xml&q=Hoddesdon&viewbox=-0.559,51.766,0.836,51.217:
   code: 200
   body: |
     <?xml version="1.0" encoding="UTF-8" ?>
     <searchresults timestamp='Sun, 01 Mar 15 20:02:29 +0000' attribution='Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring='Hoddesdon' viewbox='-0.559,51.766,0.836,51.217' polygon='false' exclude_place_ids='110741' more_url='http://nominatim.openstreetmap.org/search?format=xml&amp;exclude_place_ids=110741&amp;viewbox=-0.559%2C51.766%2C0.836%2C51.217&amp;q=Hoddesdon'>
       <place place_id='110741' osm_type='node' osm_id='18007599' place_rank='18' boundingbox="51.7216709,51.8016709,-0.0512898,0.0287102" lat='51.7616709' lon='-0.0112898' display_name='Hoddesdon, Hertfordshire, East of England, England, United Kingdom' class='place' type='town' importance='0.50547792382382' icon='http://nominatim.openstreetmap.org/images/mapicons/poi_place_town.p.20.png'/>
     </searchresults>
-  
-/search?accept-language=&format=xml&extratags=1&q=Broxbourne&viewbox=-0.559%2C51.766%2C0.836%2C51.217:
+
+/search?accept-language=&extratags=1&format=xml&q=Broxbourne&viewbox=-0.559,51.766,0.836,51.217:
   code: 200
   body: |
     <?xml version="1.0" encoding="UTF-8" ?>
@@ -15,7 +15,7 @@
       <place place_id='127984131' osm_type='relation' osm_id='2677978' place_rank='16' boundingbox="51.6808751,51.7806237,-0.114204,0.0145267" lat='51.73083995' lon='-0.0579457295222991' display_name='Broxbourne, Hertfordshire, East of England, England, United Kingdom' class='boundary' type='administrative' importance='0.46' icon='http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png'><extratags><tag key="place" value="village"/></extratags></place>
       <place place_id='109724' osm_type='node' osm_id='17044599' place_rank='30' boundingbox="51.7418469,51.7518469,-0.0156773,-0.0056773" lat='51.7468469' lon='-0.0106773' display_name='Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom' class='railway' type='station' importance='0.111' icon='http://nominatim.openstreetmap.org/images/mapicons/transport_train_station2.p.20.png'><extratags></extratags></place>
     </searchresults>
-  
+
 /reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=15:
   code: 200
   body: |
@@ -32,7 +32,7 @@
         <country_code>gb</country_code>
       </addressparts>
     </reversegeocode>
-  
+
 /reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=17:
   code: 200
   body: |
index e9e3f45c41d101925a6b5ff2a891e486ec8a62a6..28074d089b093cc52d847d21e922c4570287218f 100644 (file)
@@ -1,10 +1,10 @@
-/cgi/geocoder.fcgi?format=text&postcode=CV4+7AL:
+/cgi/geocoder.fcgi?format=text&postcode=CV4%207AL:
   code: 200
   body: |
     # Easting,Northing,Matched Postcode,Latitude,Longitude
     429926,276058,'CV4 7AL',52.381748701968,-1.56176420939232
 
-/cgi/geocoder.fcgi?format=text&postcode=XX9+9XX:
+/cgi/geocoder.fcgi?format=text&postcode=XX9%209XX:
   code: 200
   body: |
     Error: Postcode area 'XX' not found, postcode probably invalid
index 7624c0b34ed38cac9dd0ea13748a96648cae6abd..11782e098f7e2f0054c176f1790e4e219cb2c1bc 100644 (file)
@@ -161,23 +161,12 @@ module ActiveSupport
     ##
     # execute a block with a given set of HTTP responses stubbed
     def with_http_stubs(stubs_file)
-      http_client_save = OSM.http_client
-
-      begin
-        stubs = YAML.load_file(File.expand_path("../http/#{stubs_file}.yml", __FILE__))
-
-        OSM.http_client = Faraday.new do |builder|
-          builder.adapter :test do |stub|
-            stubs.each do |url, response|
-              stub.get(url) { |_env| [response["code"], {}, response["body"]] }
-            end
-          end
-        end
-
-        yield
-      ensure
-        OSM.http_client = http_client_save
+      stubs = YAML.load_file(File.expand_path("../http/#{stubs_file}.yml", __FILE__))
+      stubs.each do |url, response|
+        stub_request(:get, Regexp.new(Regexp.quote(url))).to_return(:status => response["code"], :body => response["body"])
       end
+
+      yield
     end
   end
 end