Skip to content

Commit

Permalink
Migrate from fakeweb to webmock
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 3, 2021
1 parent 7f1e7fc commit 0de8d22
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 51 deletions.
64 changes: 33 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ GEM
specs:
addressable (2.4.0)
builder (3.2.4)
byebug (11.1.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
byebug (11.1.3)
crack (0.4.5)
rexml
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
diff-lcs (1.4.4)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
git (1.5.0)
git (1.9.1)
rchardet (~> 1.8)
github_api (0.16.0)
addressable (~> 2.4.0)
descendants_tracker (~> 0.0.4)
faraday (~> 0.8, < 0.10)
hashie (>= 3.4)
mime-types (>= 1.16, < 3.0)
oauth2 (~> 1.0)
hashdiff (1.0.0)
hashie (4.0.0)
hashdiff (1.0.1)
hashie (4.1.0)
highline (2.0.3)
jeweler (2.3.9)
builder
Expand All @@ -33,47 +34,48 @@ GEM
rake
rdoc
semver2
jwt (2.2.1)
jwt (2.3.0)
mime-types (2.99.3)
mini_portile2 (2.4.0)
multi_json (1.14.1)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
nokogiri (1.10.8)
mini_portile2 (~> 2.4.0)
oauth2 (1.4.2)
nokogiri (1.12.5-x86_64-darwin)
racc (~> 1.4)
oauth2 (1.4.7)
faraday (>= 0.8, < 2.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
psych (3.1.0)
psych (4.0.2)
racc (1.6.0)
rack (2.2.3)
rake (13.0.1)
rdoc (6.2.1)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.1)
rspec-support (~> 3.9.1)
rspec-expectations (3.9.0)
rake (13.0.6)
rchardet (1.8.0)
rdoc (6.3.2)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
safe_yaml (1.0.5)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
semver2 (3.4.2)
thread_safe (0.3.6)
webmock (3.8.0)
webmock (3.13.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
x86_64-darwin-19

DEPENDENCIES
bundler
Expand All @@ -83,4 +85,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.0.1
2.2.22
49 changes: 31 additions & 18 deletions lib/http_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#
# cat /tmp/all.log
class HttpLogger
AUTHORIZATION_HEADER = 'Authorization'

class << self
attr_accessor :collapse_body_limit
attr_accessor :log_headers
Expand Down Expand Up @@ -82,10 +84,17 @@ def request_url(http, request)

def log_request_headers(request)
if self.class.log_headers
request.each_capitalized { |k,v| log("HTTP request header", "#{k}: #{v}") }
request.each_capitalized do |k,v|
log_header(:request, k, v)
end
end
end

def log_header(type, name, value)
value = "<filtered>" if name == AUTHORIZATION_HEADER
log("HTTP #{type} header", "#{name}: #{value}")
end

HTTP_METHODS_WITH_BODY = Set.new(%w(POST PUT GET PATCH))

def log_request_body(request)
Expand All @@ -104,7 +113,9 @@ def log_response_code(response)

def log_response_headers(response)
if HttpLogger.log_headers
response.each_capitalized { |k,v| log("HTTP response header", "#{k}: #{v}") }
response.each_capitalized do |k,v|
log_header(:response, k, v)
end
end
end

Expand All @@ -121,7 +132,8 @@ def log_response_body(body)
end

def require_logging?(http, request)
self.logger && !ignored?(http, request) && (http.started? || fakeweb?(http, request))

self.logger && !ignored?(http, request) && (http.started? || webmock?(http, request))
end

def ignored?(http, request)
Expand All @@ -131,7 +143,7 @@ def ignored?(http, request)
end
end

def fakeweb?(http, request)
def webmock?(http, request)
return false unless defined?(::WebMock)
uri = request_uri_as_string(http, request)
method = request.method.downcase.to_sym
Expand All @@ -146,8 +158,8 @@ def request_uri_as_string(net_http, request)
path = URI.parse(request.path).request_uri if request.path =~ /^http/

if request["authorization"] =~ /^Basic /
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
userinfo = WebMock::Utility.decode_userinfo_from_header(request["authorization"])
userinfo = WebMock::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
else
userinfo = ""
end
Expand Down Expand Up @@ -190,27 +202,28 @@ def collapse_body_limit
end
end

if defined?(::WebMock)
WebMock::HttpLibAdapters::NetHttpAdapter.instance_variable_get("@webMockNetHTTP").prepend(NetHttpLogger)
end

Net::HTTP.class_eval do
block = lambda do |a|
# raise instance_methods.inspect
alias request_without_net_http_logger request
def request(request, body = nil, &block)
HttpLogger.perform(self, request, body) do
request_without_net_http_logger(request, body, &block)
end

end
end

if defined?(Rails)
if defined?(::WebMock)
klass = WebMock::HttpLibAdapters::NetHttpAdapter.instance_variable_get("@webMockNetHTTP")
# raise klass.instance_methods.inspect
klass.class_eval(&block)
end

if !Rails.respond_to?(:application) || (Rails.application && Rails.application.config)
# Rails2
Rails.configuration.after_initialize do
HttpLogger.logger = Rails.logger unless HttpLogger.logger
end
elsif defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load)

Net::HTTP.class_eval(&block)

if defined?(Rails)
if defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load)
# Rails3
ActiveSupport.on_load(:after_initialize) do
HttpLogger.logger = Rails.logger unless HttpLogger.logger
Expand Down
16 changes: 14 additions & 2 deletions spec/http_logger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require "uri"
require "base64"

describe HttpLogger do

Expand All @@ -9,16 +10,18 @@
f.close
stub_request(:any, url).to_return(
body: response_body,
headers: {"X-Http-logger" => true},
headers: {"X-Http-logger" => true, **response_headers},
)
end

let(:response_body) { "Success" }
let(:response_headers) { {} }
let(:request_headers) { {} }

let(:url) { "http://google.com/" }
let(:uri) { URI.parse(url) }
let(:request) do
Net::HTTP.get_response(uri)
Net::HTTP.get_response(uri, **request_headers)
end

let(:long_body) do
Expand Down Expand Up @@ -51,6 +54,15 @@
it { should include("HTTP response header") }
it { should include("HTTP request header") }


context "authorization header" do

let(:request_headers) do
{'Authorization' => "Basic #{Base64.encode64('hello:world')}".strip}
end
it { should include("Authorization: <filtered>") }
end

after(:each) do
HttpLogger.log_headers = false
end
Expand Down

0 comments on commit 0de8d22

Please sign in to comment.