Skip to content

Commit

Permalink
Fix error parsing headers and enhance specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bustikiller committed May 10, 2019
1 parent edadc15 commit f2a75d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
38 changes: 21 additions & 17 deletions lib/httplog/http_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
require 'rack'

module HttpLog
LOG_PREFIX = '[httplog] '.freeze
LOG_PREFIX = '[httplog] '
FILTER_VALUE = '[FILTERED]'
class BodyParsingError < StandardError; end

class << self
Expand Down Expand Up @@ -70,7 +71,7 @@ def log_request(method, uri)
def log_headers(headers = {})
return unless config.log_headers

filter_out_hash(headers).each do |key, value|
filter_out(headers).each do |key, value|
log("Header: #{key}: #{value}")
end
end
Expand Down Expand Up @@ -104,23 +105,26 @@ def log_body(body, encoding = nil, content_type = nil)
end

def filter_out(msg)
return msg unless msg.is_a?(String)

config.filtered_keywords.reduce(msg) do |acc, keyword|
if msg.include? keyword
acc.gsub(/#{keyword}=\w+/, "#{keyword}=[FILTERED]")
else
acc
case msg
when String, HTTP::URI, Addressable::URI
config.filtered_keywords.reduce(msg) do |acc, keyword|
if msg.to_s.include? keyword
acc.gsub(/#{keyword}=\w+/, "#{keyword}=#{FILTER_VALUE}")
else
acc
end
end
when Hash, Enumerator, HTTP::Headers
hash = msg.to_h
hash.keys
.select{ |k, _| config.filtered_keywords.include?(k.downcase) }
.each{ |k| hash[k] = FILTER_VALUE }
hash
else
msg
end
end

def filter_out_hash(hash)
hash = hash.to_h
(hash.keys & config.filtered_keywords).each{ | keyword| hash[keyword] = '[FILTERED]' }
hash
end

def parse_body(body, encoding, content_type)
unless text_based?(content_type)
raise BodyParsingError, "(not showing binary data)"
Expand Down Expand Up @@ -186,10 +190,10 @@ def log_json(data = {})
method: data[:method].to_s.upcase,
url: filter_out(data[:url]),
request_body: filter_out(data[:request_body]),
request_headers: filter_out_hash(data[:request_headers].to_h),
request_headers: filter_out(data[:request_headers].to_h),
response_code: data[:response_code].to_i,
response_body: filter_out(parsed_body),
response_headers: filter_out_hash(data[:response_headers].to_h),
response_headers: filter_out(data[:response_headers].to_h),
benchmark: data[:benchmark]
}.to_json)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/http_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
let(:host) { 'localhost' }
let(:port) { 9292 }
let(:path) { '/index.html' }
let(:headers) { { 'accept' => '*/*', 'foo' => 'bar', 'secret' => '1234', 'password' => '5678' } }
let(:data) { 'foo=bar&bar=foo&secret=1234&password=5678' }
let(:params) { { 'foo' => 'bar:form-data', 'bar' => 'foo', 'secret' => '1234', 'password' => '5678' } }
let(:headers) { { 'accept' => '*/*', 'foo' => 'bar', 'secret' => 'key_1', 'password' => 'key_2' } }
let(:data) { 'foo=bar&bar=foo&secret=key_1&password=key_2' }
let(:params) { { 'foo' => 'bar:form-data', 'bar' => 'foo', 'secret' => 'key_1', 'password' => 'key_2' } }
let(:html) { File.read('./spec/support/index.html') }
let(:json) { JSON.parse(log.match(/\[httplog\]\s(.*)/).captures.first) }

Expand Down
6 changes: 2 additions & 4 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
end

RSpec.shared_examples 'filters out keywords' do
it { is_expected.to_not include('secret=1234') }
it { is_expected.to_not include('secret: 1234') }
it { is_expected.to_not include('password=5678') }
it { is_expected.to_not include('password: 5678') }
it { is_expected.to_not include('key_1') }
it { is_expected.to_not include('key_2') }
end

0 comments on commit f2a75d2

Please sign in to comment.