Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ethon typhoeus exception handling #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 4 additions & 5 deletions lib/sniffer/adapters/ethon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/sniffer/adapters/curb_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion spec/sniffer/adapters/ethon_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/sniffer/adapters/eventmachine_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/sniffer/adapters/http_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/sniffer/adapters/httpclient_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/sniffer/adapters/net_http_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/sniffer/adapters/patron_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion spec/sniffer/adapters/typhoeus_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/support/shared_examples_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down