lib/toxiproxy.rb in toxiproxy-1.0.0 vs lib/toxiproxy.rb in toxiproxy-1.0.2

- old
+ new

@@ -29,28 +29,28 @@ def_delegators :all, *ProxyCollection::METHODS # Re-enables all proxies and disables all toxics. def self.reset request = Net::HTTP::Post.new("/reset") - response = http.request(request) + response = http_request(request) assert_response(response) self end def self.version return false unless running? request = Net::HTTP::Get.new("/version") - response = http.request(request) + response = http_request(request) assert_response(response) response.body end # Returns a collection of all currently active Toxiproxies. def self.all request = Net::HTTP::Get.new("/proxies") - response = http.request(request) + response = http_request(request) assert_response(response) proxies = JSON.parse(response.body).map { |name, attrs| self.new({ upstream: attrs["upstream"], @@ -146,11 +146,11 @@ request = Net::HTTP::Post.new("/proxies/#{name}") hash = {enabled: false} request.body = hash.to_json - response = http.request(request) + response = http_request(request) assert_response(response) self end # Enables a Toxiproxy. This will cause the proxy to start listening again. @@ -158,11 +158,11 @@ request = Net::HTTP::Post.new("/proxies/#{name}") hash = {enabled: true} request.body = hash.to_json - response = http.request(request) + response = http_request(request) assert_response(response) self end # Create a Toxiproxy, proxying traffic from `@listen` (optional argument to @@ -172,11 +172,11 @@ request = Net::HTTP::Post.new("/proxies") hash = {upstream: upstream, name: name, listen: listen, enabled: enabled} request.body = hash.to_json - response = http.request(request) + response = http_request(request) assert_response(response) new = JSON.parse(response.body) @listen = new["listen"] @@ -184,19 +184,19 @@ end # Destroys a Toxiproxy. def destroy request = Net::HTTP::Delete.new("/proxies/#{name}") - response = http.request(request) + response = http_request(request) assert_response(response) self end # Returns an array of the current toxics for a direction. def toxics request = Net::HTTP::Get.new("/proxies/#{name}/toxics") - response = http.request(request) + response = http_request(request) assert_response(response) JSON.parse(response.body).map { |attrs| Toxic.new( type: attrs['type'], @@ -208,9 +208,26 @@ ) } end private + + def self.http_request(request) + ensure_webmock_whitelists_toxiproxy if defined? WebMock + http.request(request) + end + + def http_request(request) + self.class.http_request(request) + end + + def self.ensure_webmock_whitelists_toxiproxy + endpoint = "#{uri.host}:#{uri.port}" + WebMock::Config.instance.allow ||= [] + unless WebMock::Config.instance.allow.include?(endpoint) + WebMock::Config.instance.allow << endpoint + end + end def self.uri @uri ||= ::URI.parse(DEFAULT_URI) end