diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a9ac5e..3364491 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* Fixed `NoMethodError` on Ethon/Typhoeus network failures by [@blaze182](https://github.com/blaze182) +* Adjusted host and query parsing for Ethon/Typhoeus + ## 0.5.0 (May 6, 2022) ## * Added prepend of all adapters by [@nate-at-gusto](https://github.com/nate-at-gusto) diff --git a/lib/sniffer/adapters/ethon_adapter.rb b/lib/sniffer/adapters/ethon_adapter.rb index 7c1bef5..2035488 100644 --- a/lib/sniffer/adapters/ethon_adapter.rb +++ b/lib/sniffer/adapters/ethon_adapter.rb @@ -18,7 +18,7 @@ def make_sniffer_request(url, action_name, options) return unless Sniffer.enabled? @data_item = Sniffer::DataItem.new - uri = URI("http://#{url}") + uri = URI(url.start_with?(%r{https?://}) ? url : "http://#{url}") @data_item.request = Sniffer::DataItem::Request.new(host: uri.host, method: action_name.upcase, @@ -50,14 +50,13 @@ def perform_with_sniffer end if Sniffer.enabled? - uri = URI("http://#{@url}") + uri = URI(url.start_with?(%r{https?://}) ? url : "http://#{@url}") query = uri.path query += "?#{uri.query}" if uri.query @data_item.request.query = query - status = @response_headers.scan(%r{HTTP/... (\d{3})}).flatten[0].to_i - hash_headers = @response_headers - .split(/\r?\n/)[1..-1] + status = response_code + hash_headers = (@response_headers.split(/\r?\n/)[1..-1] || []) .each_with_object({}) do |item, res| k, v = item.split(": ") res[k] = v diff --git a/spec/sniffer/adapters/curb_adapter_spec.rb b/spec/sniffer/adapters/curb_adapter_spec.rb index 8977c01..5a43b38 100644 --- a/spec/sniffer/adapters/curb_adapter_spec.rb +++ b/spec/sniffer/adapters/curb_adapter_spec.rb @@ -17,6 +17,10 @@ end end + def unresolved_request + Curl::Easy.http_get('http://localh0st:45678/') + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/ethon_adapter_spec.rb b/spec/sniffer/adapters/ethon_adapter_spec.rb index c9e7138..b73a620 100644 --- a/spec/sniffer/adapters/ethon_adapter_spec.rb +++ b/spec/sniffer/adapters/ethon_adapter_spec.rb @@ -6,7 +6,7 @@ let(:headers) { { "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", "accept" => "*/*", "user-agent" => "Ruby", "host" => "localhost:4567" } } let(:get_request) do easy = Ethon::Easy.new - easy.http_request("localhost:4567/?lang=ruby&author=matz", :get, headers: headers) + easy.http_request("http://localhost:4567/?lang=ruby&author=matz", :get, headers: headers) easy.perform end @@ -30,6 +30,12 @@ easy.perform end + def unresolved_request + easy = Ethon::Easy.new + easy.http_request('localh0st:45678', :get) + easy.perform + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/eventmachine_adapter_spec.rb b/spec/sniffer/adapters/eventmachine_adapter_spec.rb index 7490cae..813a99d 100644 --- a/spec/sniffer/adapters/eventmachine_adapter_spec.rb +++ b/spec/sniffer/adapters/eventmachine_adapter_spec.rb @@ -35,6 +35,15 @@ } end + def unresolved_request + EventMachine.run { + http = EventMachine::HttpRequest.new("http://localh0st:45678").get + http.errback { + EventMachine.stop + } + } + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/http_adapter_spec.rb b/spec/sniffer/adapters/http_adapter_spec.rb index eb89304..a319881 100644 --- a/spec/sniffer/adapters/http_adapter_spec.rb +++ b/spec/sniffer/adapters/http_adapter_spec.rb @@ -9,6 +9,10 @@ let(:post_request) { HTTP.post('http://localhost:4567/data?lang=ruby', body: "author=Matz") } let(:post_json) { HTTP.post('http://localhost:4567/json', json: { 'lang' => 'Ruby', 'author' => 'Matz' }) } + def unresolved_request + HTTP.get('http://localh0st:45678/') + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/httpclient_adapter_spec.rb b/spec/sniffer/adapters/httpclient_adapter_spec.rb index a6689e1..3e60bea 100644 --- a/spec/sniffer/adapters/httpclient_adapter_spec.rb +++ b/spec/sniffer/adapters/httpclient_adapter_spec.rb @@ -23,6 +23,10 @@ def post_json JSONClient.new.post(uri, 'lang' => 'Ruby', 'author' => 'Matz') end + def unresolved_request + client.get(URI('http://localh0st:45678/')) + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/net_http_adapter_spec.rb b/spec/sniffer/adapters/net_http_adapter_spec.rb index f05c5e9..c770d70 100644 --- a/spec/sniffer/adapters/net_http_adapter_spec.rb +++ b/spec/sniffer/adapters/net_http_adapter_spec.rb @@ -31,6 +31,11 @@ def post_json http.request(request) end + def unresolved_request + uri = URI.parse('http://localh0st:45678/') + Net::HTTP.get(uri) + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/patron_adapter_spec.rb b/spec/sniffer/adapters/patron_adapter_spec.rb index f7e994e..f235813 100644 --- a/spec/sniffer/adapters/patron_adapter_spec.rb +++ b/spec/sniffer/adapters/patron_adapter_spec.rb @@ -10,6 +10,10 @@ let(:post_request) { session.post('/data?lang=ruby', "author=Matz") } let(:post_json) { session.post('/json', { 'lang' => 'Ruby', 'author' => 'Matz' }.to_json, "Content-Type" => "application/json; charset=UTF-8") } + def unresolved_request + Patron::Session.new(base_url: 'http://localhost:45678').get('/') + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/sniffer/adapters/typhoeus_adapter_spec.rb b/spec/sniffer/adapters/typhoeus_adapter_spec.rb index 4f2a826..6792438 100644 --- a/spec/sniffer/adapters/typhoeus_adapter_spec.rb +++ b/spec/sniffer/adapters/typhoeus_adapter_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Typhoeus do let(:headers) { { "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", "accept" => "*/*", "user-agent" => "Ruby", "host" => "localhost:4567" } } - let(:get_request) { Typhoeus::Request.new('localhost:4567/?lang=ruby&author=matz', method: :get, headers: headers).run } + let(:get_request) { Typhoeus::Request.new('http://localhost:4567/?lang=ruby&author=matz', method: :get, headers: headers).run } let(:get_request_dynamic_params) do Typhoeus::Request.new("localhost:4567/?lang=ruby", method: :get, @@ -20,6 +20,10 @@ headers: { 'Content-Type' => "application/json" }).run end + def unresolved_request + Typhoeus::Request.new('localhost:45678').run + end + it 'logs', enabled: true do logger = double Sniffer.config.logger = logger diff --git a/spec/support/shared_examples_requests_spec.rb b/spec/support/shared_examples_requests_spec.rb index 7e2911d..7d751fd 100644 --- a/spec/support/shared_examples_requests_spec.rb +++ b/spec/support/shared_examples_requests_spec.rb @@ -40,6 +40,22 @@ expect(Sniffer.data).to be_empty end + it 'preserves the original behavior for unresolved requests', enabled: true do + skip "Not implemented in adapter" unless respond_to?(:unresolved_request) + + def error_class(enabled: true) + Sniffer.disable! unless enabled + unresolved_request + nil + rescue StandardError => e + e.class + ensure + Sniffer.enable! + end + + expect(error_class).to eq(error_class(enabled: false)) + end + context 'with url_whitelist', enabled: true do it 'stores data with matched url' do Sniffer.config.url_whitelist = /localhost:4567/