Skip to content

Commit

Permalink
close #19 release 1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Feb 21, 2020
1 parent ac178bd commit bf3d95c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ RESPONSE:
```

If you want to get the last request sent or the last response use `NiceHttp.last_request` or `NiceHttp.last_response`

Also you can collect all data sent and received by setting `NiceHttp.capture = true` and all data will be stored on `NiceHttp.captured` as an Array of Strings (Request+Response).

### Multithreading

In case you want to use multithread and log in different files every thread, add an unique name for the thread then the logs will be stored accordingly
Expand Down
12 changes: 9 additions & 3 deletions lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Attributes you can access using NiceHttp.the_attribute:
# :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
# :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured
#
# @attr [String] host The host to be accessed
# @attr [Integer] port The port number
Expand Down Expand Up @@ -45,6 +45,8 @@
# @attr [Hash] values_for The default values to set on the data in case not specified others
# @attr [Boolean] create_stats If true, NiceHttp will create stats of the http communication and store them on NiceHttp.stats hash
# @attr [Hash] stats It contains detailed stats of the http communication
# @attr [Boolean] capture If true, NiceHttp will store all requests and responses on NiceHttp.captured as strings
# @attr [Array] captured It contains all the http requests and responses if NiceHttp.capture is set to true
######################################################
class NiceHttp
include NiceHttpManageRequest
Expand All @@ -69,7 +71,7 @@ def initialize(attribute, message = "")
class << self
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
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured
end

at_exit do
Expand Down Expand Up @@ -119,6 +121,8 @@ def self.reset!
path: {},
name: {},
}
@capture = false
@captured = []
end
reset!

Expand All @@ -135,7 +139,7 @@ def self.inherited(subclass)
######################################################
# 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, :log_headers
# @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, :capture
######################################################
def self.defaults=(par = {})
@host = par[:host] if par.key?(:host)
Expand All @@ -152,6 +156,7 @@ def self.defaults=(par = {})
@use_mocks = par[:use_mocks] if par.key?(:use_mocks)
@auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect)
@create_stats = par[:create_stats] if par.key?(:create_stats)
@capture = par[:capture] if par.key?(:capture)
end

######################################################
Expand Down Expand Up @@ -331,6 +336,7 @@ def initialize(args = {})
auto_redirect = self.class.auto_redirect
@num_redirects = 0
@create_stats = self.class.create_stats
@capture = self.class.capture

#todo: set only the cookies for the current domain
#key: path, value: hash with key is the name of the cookie and value the value
Expand Down
2 changes: 2 additions & 0 deletions lib/nice_http/manage_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ def manage_response(resp, data)
end
end
}
self.class.captured << "#{self.class.last_request}\n#{self.class.last_response}" if self.class.capture
else
message += "\n Same as the last response."
self.class.captured << "#{self.class.last_request}\n#{message}" if self.class.capture
end
@logger.info message
if @response.kind_of?(Hash)
Expand Down
2 changes: 1 addition & 1 deletion 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.25'
s.version = '1.8.0'
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 Down
37 changes: 37 additions & 0 deletions spec/nice_http/capture_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "nice_http"
require "English"

RSpec.describe NiceHttp do
let(:klass) { Class.new NiceHttp }

describe "capture" do
it "captures the requests and responses" do
klass.capture = true
http = klass.new("http://example.com")
http.get "/"
expect(klass.captured.size).to eq 1
expect(klass.captured.join).to match(/^\s*\w+\s+Request\s*$/i)
expect(klass.captured.join).to match(/^\s*RESPONSE:/i)
http2 = klass.new("http://www.google.com")
http2.get "/"
expect(klass.captured.join.scan(/^\s*\w+\s+Request\s*$/i).flatten.size).to eq 2
expect(klass.captured.join.scan(/^\s*Response:/i).flatten.size).to eq 2
end
it "doesn't capture the requests and responses if capture set to false" do
http = klass.new("http://example.com")
http.get "/"
expect(klass.captured.size).to eq 0
klass.capture = true
http = klass.new("http://example.com")
http.get "/"
expect(klass.captured.size).to eq 1
expect(klass.captured.join).to match(/^\s*\w+\s+Request\s*$/i)
expect(klass.captured.join).to match(/^\s*RESPONSE:/i)
klass.capture = false
http2 = klass.new("http://www.google.com")
http2.get "/"
expect(klass.captured.join.scan(/^\s*\w+\s+Request\s*$/i).flatten.size).to eq 1
expect(klass.captured.join.scan(/^\s*Response:/i).flatten.size).to eq 1
end
end
end
7 changes: 7 additions & 0 deletions spec/nice_http/nice_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
klass.values_for = {one: 1}
klass.create_stats = true
klass.stats = {one: 1}
klass.capture = true

klass.reset!

Expand All @@ -49,6 +50,7 @@
expect(klass.values_for).to eq ({})
expect(klass.create_stats).to eq false
expect(klass.stats[:all][:num_requests]).to eq 0
expect(klass.capture).to eq false
end
end

Expand Down Expand Up @@ -388,6 +390,9 @@
specify "create_stats is false" do
expect(klass.create_stats).to eq false
end
specify "capture is false" do
expect(klass.capture).to eq false
end
specify "I can set/get them with accessors" do
expect { klass.port = 8888 }.to change { klass.port }.to(8888)
expect { klass.host = "localhost" }.to change { klass.host }.to("localhost")
Expand All @@ -401,6 +406,7 @@
expect { klass.log = :screen }.to change { klass.log }.to(:screen)
expect { klass.log_path = './tmp/' }.to change { klass.log_path }.to('./tmp/')
expect { klass.create_stats = true }.to change { klass.create_stats }.to(true)
expect { klass.capture = true }.to change { klass.capture }.to(true)
end
specify "I can set many at once with a hash" do
expect { klass.defaults = {port: 8888} }.to change { klass.port }.to(8888)
Expand All @@ -415,6 +421,7 @@
expect { klass.defaults = {log: :screen} }.to change { klass.log }.to(:screen)
expect { klass.defaults = {log_path: './tmp/'} }.to change { klass.log_path }.to('./tmp/')
expect { klass.defaults = {create_stats: true} }.to change { klass.create_stats }.to(true)
expect { klass.defaults = {capture: true} }.to change { klass.capture }.to(true)
end
specify 'setting many at once doesn\'t override unprovided values' do
expect { klass.defaults = {host: "http://whatevz.com"} }.to_not change { klass.port }
Expand Down

0 comments on commit bf3d95c

Please sign in to comment.