spec/lib/http_log_spec.rb in httplog-1.2.2 vs spec/lib/http_log_spec.rb in httplog-1.3.0

- old
+ new

@@ -3,20 +3,21 @@ require 'spec_helper' describe HttpLog do subject { log } # see spec_helper + let(:secret) { 'my secret' } let(:host) { 'localhost' } let(:port) { 9292 } let(:path) { '/index.html' } - let(:headers) { { 'accept' => '*/*', 'foo' => 'bar' } } - let(:data) { 'foo=bar&bar=foo' } - let(:params) { { 'foo' => 'bar:form-data', 'bar' => 'foo' } } + let(:headers) { { 'accept' => '*/*', 'foo' => secret } } + let(:data) { "foo=#{secret}&bar=foo" } + let(:params) { { 'foo' => secret, 'bar' => 'foo:form-data' } } let(:html) { File.read('./spec/support/index.html') } let(:json) { JSON.parse(log.match(/\[httplog\]\s(.*)/).captures.first) } - # Configuration + # Default configuration let(:enabled) { HttpLog.configuration.enabled } let(:severity) { HttpLog.configuration.severity } let(:log_headers) { HttpLog.configuration.log_headers } let(:log_request) { HttpLog.configuration.log_request } let(:log_response) { HttpLog.configuration.log_response } @@ -29,10 +30,11 @@ let(:prefix_line_numbers) { HttpLog.configuration.prefix_line_numbers } let(:json_log) { HttpLog.configuration.json_log } let(:compact_log) { HttpLog.configuration.compact_log } let(:url_blacklist_pattern) { HttpLog.configuration.url_blacklist_pattern } let(:url_whitelist_pattern) { HttpLog.configuration.url_whitelist_pattern } + let(:filter_parameters) { HttpLog.configuration.filter_parameters } def configure HttpLog.configure do |c| c.enabled = enabled c.severity = severity @@ -48,10 +50,11 @@ c.prefix_line_numbers = prefix_line_numbers c.json_log = json_log c.compact_log = compact_log c.url_blacklist_pattern = url_blacklist_pattern c.url_whitelist_pattern = url_whitelist_pattern + c.filter_parameters = filter_parameters end end ADAPTERS = [ NetHTTPAdapter, @@ -133,11 +136,11 @@ it { is_expected.to include("Connecting: #{host}:#{port}") } end it_behaves_like 'logs request', 'POST' it_behaves_like 'logs expected response' - it_behaves_like 'logs data', 'foo=bar&bar=foo' + it_behaves_like 'logs data' it_behaves_like 'logs status', 200 it_behaves_like 'logs benchmark' it { is_expected.to_not include('Header:') } @@ -172,10 +175,11 @@ context 'with headers logging' do let(:log_headers) { true } it { is_expected.to match(%r{Header: accept: */*}i) } # request it { is_expected.to match(/Header: Server: thin/i) } # response + it_behaves_like 'filtered parameters' end context 'with blacklist hit' do let(:url_blacklist_pattern) { /#{host}:#{port}/ } it_behaves_like 'logs nothing' @@ -204,10 +208,11 @@ it_behaves_like 'with request logging disabled' it_behaves_like 'with connection logging disabled' it_behaves_like 'data logging disabled' it_behaves_like 'response logging disabled' it_behaves_like 'benchmark logging disabled' + it_behaves_like 'filtered parameters' context 'with single color' do let(:color) { :red } it { is_expected.to include("\e[31m") } end @@ -229,11 +234,11 @@ it { is_expected.to_not include(HttpLog::LOG_PREFIX) } end context 'with compact config' do let(:compact_log) { true } - it { is_expected.to match(%r{\[httplog\] GET http://#{host}:#{port}#{path}(\?.*)? completed with status code \d{3} in (\d|\.)+}) } + it { is_expected.to match(%r{\[httplog\] GET http://#{host}:#{port}#{path}(\?.*)? completed with status code \d{3} in \d+\.\d{1,6} }) } it { is_expected.to_not include("Connecting: #{host}:#{port}") } it { is_expected.to_not include('Response:') } it { is_expected.to_not include('Data:') } it { is_expected.to_not include('Benchmark: ') } end @@ -246,10 +251,11 @@ it_behaves_like 'data logging disabled' it_behaves_like 'response logging disabled' it_behaves_like 'benchmark logging disabled' it_behaves_like 'with prefix response lines' it_behaves_like 'with line numbers' + it_behaves_like 'filtered parameters' end end context 'POST form data requests' do if adapter_class.method_defined? :send_post_form_request @@ -258,25 +264,27 @@ it_behaves_like 'data logging disabled' it_behaves_like 'response logging disabled' it_behaves_like 'benchmark logging disabled' it_behaves_like 'with prefix response lines' it_behaves_like 'with line numbers' + it_behaves_like 'filtered parameters' end end context 'POST multi-part requests (file upload)' do let(:upload) { Tempfile.new('http-log') } - let(:params) { { 'foo' => 'bar', 'file' => upload } } + let(:params) { { 'foo' => secret, 'file' => upload } } if adapter_class.method_defined? :send_multipart_post_request before { adapter.send_multipart_post_request } it_behaves_like 'data logging disabled' it_behaves_like 'response logging disabled' it_behaves_like 'benchmark logging disabled' it_behaves_like 'with prefix response lines' it_behaves_like 'with line numbers' + it_behaves_like 'filtered parameters' end end end context 'with JSON config' do @@ -290,9 +298,10 @@ it { expect(json['request_headers']).to be_a(Hash) } it { expect(json['response_headers']).to be_a(Hash) } it { expect(json['response_code']).to eq(200) } it { expect(json['response_body']).to eq(html) } it { expect(json['benchmark']).to be_a(Numeric) } + it_behaves_like 'filtered parameters' context 'and compact config' do let(:compact_log) { true } it { expect(json['method']).to eq('POST') }