Skip to content

Commit

Permalink
Release v1.7.20 fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Oct 5, 2019
1 parent 15e3cff commit 0113dc4
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 17 deletions.
11 changes: 6 additions & 5 deletions .yardopts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
--title 'nice_http - NiceHttp -- simplest library for accessing and testing HTTP and REST resources.'
--charset utf-8
--markup markdown
'lib/nice_http.rb'
'lib/nice_http/http_methods.rb'
'lib/nice_http/utils.rb'
'*.md'
'LICENSE'
-
lib/nice_http.rb
lib/nice_http/http_methods.rb
lib/nice_http/utils.rb

LICENSE
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ Other values you can supply:
* :file_run, will generate a log file with the name where the object was created and extension .log, fex: myfile.rb.log
* String, the path and file name where the logs will be stored.

In case you want to hide the header values from the requests on the logs use the option `log_headers`. Possible values:
* :all, (default), will log all header values.
* :none, won't log any value of the headers.
* :partial, will log only the last 10 characters of the header values.

Example of logs:
```
I, [2019-03-22T18:38:58.518964 #29412] INFO -- : (47266856647720): Http connection created. host:reqres.in, port:443, ssl:true, mode:, proxy_host: , proxy_port:
Expand Down
16 changes: 11 additions & 5 deletions lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

######################################################
# Attributes you can access using NiceHttp.the_attribute:
# :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
# :host, :port, :ssl, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port,
# :last_request, :last_response, :request_id, :use_mocks, :connections,
# :active, :auto_redirect, :values_for, :create_stats, :stats
#
Expand All @@ -24,6 +24,7 @@
# :file, will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log.
# :file_run, will generate a log file with the name where the object was created and extension .log, fex: myfile.rb.log
# String the path and file name where the logs will be stored.
# @attr [Symbol] log_headers. :all, :partial, :none (default :all) If :all will log all the headers. If :partial will log the last 10 characters. If :none no headers.
# @attr [String] proxy_host the proxy host to be used
# @attr [Integer] proxy_port the proxy port to be used
# @attr [String] last_request The last request with all the content sent
Expand Down Expand Up @@ -66,7 +67,7 @@ def initialize(attribute, message = "")
end

class << self
attr_accessor :host, :port, :ssl, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port,
attr_accessor :host, :port, :ssl, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port, :log_headers,
:last_request, :last_response, :request_id, :use_mocks, :connections,
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats
end
Expand All @@ -89,6 +90,7 @@ def self.reset!
@debug = false
@log = :fix_file
@log_path = ''
@log_headers = :all
@proxy_host = nil
@proxy_port = nil
@last_request = nil
Expand Down Expand Up @@ -128,12 +130,12 @@ def self.inherited(subclass)
end

attr_reader :host, :port, :ssl, :debug, :log, :log_path, :proxy_host, :proxy_port, :response, :num_redirects
attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for
attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for, :log_headers

######################################################
# Change the default values for NiceHttp supplying a Hash
#
# @param par [Hash] keys: :host, :port, :ssl, :headers, :debug, :log, :log_path, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats
# @param par [Hash] keys: :host, :port, :ssl, :headers, :debug, :log, :log_path, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats, :log_headers
######################################################
def self.defaults=(par = {})
@host = par[:host] if par.key?(:host)
Expand All @@ -144,6 +146,7 @@ def self.defaults=(par = {})
@debug = par[:debug] if par.key?(:debug)
@log_path = par[:log_path] if par.key?(:log_path)
@log = par[:log] if par.key?(:log)
@log_headers = par[:log_headers] if par.key?(:log_headers)
@proxy_host = par[:proxy_host] if par.key?(:proxy_host)
@proxy_port = par[:proxy_port] if par.key?(:proxy_port)
@use_mocks = par[:use_mocks] if par.key?(:use_mocks)
Expand Down Expand Up @@ -293,6 +296,7 @@ def self.save_stats(file_name = "")
# debug -- true, false (default)
# log_path -- string with path for the logs, empty string (default)
# log -- :no, :screen, :file, :fix_file (default).
# log_headers -- :all, :none, :partial (default).
# A string with a path can be supplied.
# If :fix_file: nice_http.log
# In case :file it will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log
Expand All @@ -319,6 +323,7 @@ def initialize(args = {})
@debug = self.class.debug
@log = self.class.log
@log_path = self.class.log_path
@log_headers = self.class.log_headers
@proxy_host = self.class.proxy_host
@proxy_port = self.class.proxy_port
@use_mocks = self.class.use_mocks
Expand Down Expand Up @@ -347,6 +352,7 @@ def initialize(args = {})
@debug = args[:debug] if args.keys.include?(:debug)
@log = args[:log] if args.keys.include?(:log)
@log_path = args[:log_path] if args.keys.include?(:log_path)
@log_headers = args[:log_headers] if args.keys.include?(:log_headers)
@proxy_host = args[:proxy_host] if args.keys.include?(:proxy_host)
@proxy_port = args[:proxy_port] if args.keys.include?(:proxy_port)
@use_mocks = args[:use_mocks] if args.keys.include?(:use_mocks)
Expand Down Expand Up @@ -433,7 +439,6 @@ def initialize(args = {})
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
@prepath = uri.path unless uri.path == "/"
end
raise InfoMissing, :port if @port.to_s == ""
raise InfoMissing, :host if @host.to_s == ""
raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass)
Expand All @@ -442,6 +447,7 @@ def initialize(args = {})
raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
raise InfoMissing, :headers unless @headers.is_a?(Hash)
raise InfoMissing, :values_for unless @values_for.is_a?(Hash)
raise InfoMissing, :log_headers unless [:all, :none, :partial].include?(@log_headers)
begin
if !@proxy_host.nil? && !@proxy_port.nil?
Expand Down
18 changes: 17 additions & 1 deletion lib/nice_http/manage_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,23 @@ def manage_request(*arguments)
end

headers_ts = ""
headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }

if @log_headers == :none
@logger.info "No header values since option log_headers is set to :none"
headers_t.each { |key, val| headers_ts += key.to_s + ":" + "''" + ", " }
elsif @log_headers == :partial
@logger.info "Just the last 10 characters on header values since option log_headers is set to :partial"
headers_t.each { |key, val|
if val.to_s.size>10
headers_ts += key.to_s + ": ..." + (val.to_s[-10..-1] || val.to_s) + ", "
else
headers_ts += key.to_s + ":" + (val.to_s[-10..-1] || val.to_s) + ", "
end
}
else
headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
end

message = "\n\n#{"- " * 25}\n"
if arguments.size == 1 and arguments[0].kind_of?(Hash) and arguments[0].key?(:name)
message += "#{method_s.upcase} Request: #{arguments[0][:name]}"
Expand Down
4 changes: 2 additions & 2 deletions nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.7.19'
s.version = '1.7.20'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Get http logs and statistics automatically. Use hashes on your requests. Access JSON even easier."
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ["LICENSE","README.md"]
s.homepage = 'https://github.com/MarioRuiz/nice_http'
s.license = 'MIT'
s.add_runtime_dependency 'nice_hash', '~> 1.14', '>= 1.14.0'
s.add_runtime_dependency 'nice_hash', '~> 1.15', '>= 1.15.3'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
Expand Down
36 changes: 35 additions & 1 deletion spec/nice_http/logs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,39 @@
content = File.read("./nice_http.log")
expect(content).to match /It was not possible to close the HTTP connection, already closed/
end
end

it 'logs all the headers if log_headers is :all' do
http = klass.new("http://example.com")
http.log_headers = :all
http.headers = {uno: 'abcdefghijklmnopqrstuvwxyz', dos: '2', tres: '00000000001234567890'}
http.get('/')
content = File.read("./nice_http.log")
expected = "headers: {uno:abcdefghijklmnopqrstuvwxyz, dos:2, tres:00000000001234567890,"
expect(content).to match(expected)
end

it 'doesn\'t log any headers if log_headers is :none' do
http = klass.new("http://example.com")
http.log_headers = :none
http.headers = {uno: 'abcdefghijklmnopqrstuvwxyz', dos: '2', tres: '00000000001234567890'}
http.get('/')
content = File.read("./nice_http.log")
expected = "headers: {uno:'', dos:'', tres:'',"
expect(content).to match(expected)
expect(content).to match('No header values since option log_headers is set to :none')
end

it 'logs only 10 last chars of headers if log_headers is :partial' do
http = klass.new("http://example.com")
http.log_headers = :partial
http.headers = {uno: 'abcdefghijklmnopqrstuvwxyz', dos: '2', tres: '00000000001234567890'}
http.get('/')
content = File.read("./nice_http.log")
expected = "{uno: ...qrstuvwxyz, dos:2, tres: ...1234567890"
expect(content).to match(expected)
expect(content).to match('Just the last 10 characters on header values since option log_headers is set to :partial')
end


end
end
36 changes: 33 additions & 3 deletions spec/nice_http/nice_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
klass.connections = [1, 1]
klass.active = 1
klass.auto_redirect = false
klass.log_headers = :none
klass.values_for = {one: 1}
klass.create_stats = true
klass.stats = {one: 1}
Expand All @@ -44,6 +45,7 @@
expect(klass.connections).to eq []
expect(klass.active).to eq 0
expect(klass.auto_redirect).to eq true
expect(klass.log_headers).to eq :all
expect(klass.values_for).to eq ({})
expect(klass.create_stats).to eq false
expect(klass.stats[:all][:num_requests]).to eq 0
Expand Down Expand Up @@ -160,7 +162,7 @@
klass.port = 443
klass.host = "localhost"
klass.auto_redirect = true
expect(klass.new(auto_redirect: false).debug).to eq false
expect(klass.new(auto_redirect: false).auto_redirect).to eq false
end
it 'raises an error when it can\'t figure out the auto_redirect' do
klass.auto_redirect = nil
Expand All @@ -173,6 +175,30 @@
end
end

describe "log_headers" do
it "uses the class log_headers by default" do
klass.host = "example.com"
klass.port = 443
klass.log_headers = :none
expect(klass.new.log_headers).to eq :none
end
it "can be provided an explicit log_headers" do
klass.port = 443
klass.host = "localhost"
klass.log_headers = :all
expect(klass.new(log_headers: :partial).log_headers).to eq :partial
end
it 'raises an error when it can\'t figure out the log_headers' do
klass.log_headers = nil
klass.host = "example.com"
klass.port = 443
klass.new rescue err = $ERROR_INFO
expect(err.class).to eq NiceHttp::InfoMissing
expect(err.attribute).to eq :log_headers
expect(err.message).to match(/wrong log_headers/i)
end
end

describe "use_mocks" do
it "uses the class use_mocks by default" do
klass.use_mocks = true
Expand All @@ -186,7 +212,7 @@
klass.use_mocks = false
expect(klass.new(use_mocks: true).use_mocks).to eq true
end
it 'raises an error when it can\'t figure out the auto_redirect' do
it 'raises an error when it can\'t figure out the use_mocks' do
klass.use_mocks = nil
klass.host = "example.com"
klass.port = 443
Expand Down Expand Up @@ -325,7 +351,6 @@
end
end


describe "class defaults" do
specify "port is 80" do
expect(klass.port).to eq 80
Expand All @@ -342,6 +367,9 @@
specify "auto_redirect is true" do
expect(klass.auto_redirect).to eq true
end
specify "log_headers is :all" do
expect(klass.log_headers).to eq :all
end
specify "use_mocks is false" do
expect(klass.use_mocks).to eq false
end
Expand All @@ -366,6 +394,7 @@
expect { klass.ssl = true }.to change { klass.ssl }.to(true)
expect { klass.debug = true }.to change { klass.debug }.to(true)
expect { klass.auto_redirect = false }.to change { klass.auto_redirect }.to(false)
expect { klass.log_headers = :partial }.to change { klass.log_headers }.to(:partial)
expect { klass.use_mocks = true }.to change { klass.use_mocks }.to(true)
expect { klass.headers = {example: "test"} }.to change { klass.headers }.to({example: "test"})
expect { klass.values_for = {example: "test"} }.to change { klass.values_for }.to({example: "test"})
Expand All @@ -379,6 +408,7 @@
expect { klass.defaults = {ssl: true} }.to change { klass.ssl }.to(true)
expect { klass.defaults = {debug: true} }.to change { klass.debug }.to(true)
expect { klass.defaults = {auto_redirect: false} }.to change { klass.auto_redirect }.to(false)
expect { klass.defaults = {log_headers: :none} }.to change { klass.log_headers }.to(:none)
expect { klass.defaults = {use_mocks: true} }.to change { klass.use_mocks }.to(true)
expect { klass.defaults = {headers: {example: "test"}} }.to change { klass.headers }.to({example: "test"})
expect { klass.defaults = {values_for: {example: "test"}} }.to change { klass.values_for }.to({example: "test"})
Expand Down

0 comments on commit 0113dc4

Please sign in to comment.