From 5e86393f72f62a1f37ca49d5dfe4ea64f8290a3e Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Sat, 29 Oct 2016 19:40:48 +0200 Subject: [PATCH] Use webmock to power the with_http_stubs helper 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 | 5 ----- test/http/geocoder_ca.yml | 14 +++++++------- test/http/nominatim.yml | 10 +++++----- test/http/npemap.yml | 4 ++-- test/test_helper.rb | 21 +++++---------------- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/lib/osm.rb b/lib/osm.rb index cd3a2156c..370f0f300 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -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) diff --git a/test/http/geocoder_ca.yml b/test/http/geocoder_ca.yml index 88a3fdc94..0fbaa105d 100644 --- a/test/http/geocoder_ca.yml +++ b/test/http/geocoder_ca.yml @@ -1,5 +1,5 @@ -/?geoit=XML&postal=A1B+2C3: - code: 200 +/?geoit=XML&postal=A1B%202C3: + code: 200 body: | @@ -15,8 +15,8 @@ -/?geoit=XML&postal=k1a+0b1: - code: 200 +/?geoit=XML&postal=k1a%200b1: + code: 200 body: | @@ -31,9 +31,9 @@ 0.9 - -/?geoit=XML&postal=Q0Q+0Q0: - code: 200 + +/?geoit=XML&postal=Q0Q%200Q0: + code: 200 body: | diff --git a/test/http/nominatim.yml b/test/http/nominatim.yml index 41467721f..accaebf33 100644 --- a/test/http/nominatim.yml +++ b/test/http/nominatim.yml @@ -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: | - -/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: | @@ -15,7 +15,7 @@ - + /reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=15: code: 200 body: | @@ -32,7 +32,7 @@ gb - + /reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=17: code: 200 body: | diff --git a/test/http/npemap.yml b/test/http/npemap.yml index e9e3f45c4..28074d089 100644 --- a/test/http/npemap.yml +++ b/test/http/npemap.yml @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 7624c0b34..11782e098 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 -- 2.43.2