Bugfix, logic optimization, correct code format

- Fix typo in README
- Replace tab character with spaces and correct wrong indents.
- Bugfix: headers are not sorted by keys when the option is `:all`
- Optimize filtering logic
This commit is contained in:
Keisuke Yamamoto 2017-04-21 11:47:06 +09:00
parent 753b94ecb0
commit b52ea9adeb
5 changed files with 28 additions and 22 deletions

View File

@ -53,7 +53,7 @@ The middleware logger can be customized with the following options:
* The `:logger` option can be any object that responds to `.info(String)`
* The `:filter` option can be any object that responds to `.filter(Hash)` and returns a hash.
* The `:headers` option can be eather `:all` or array of strings.
* The `:headers` option can be either `:all` or array of strings.
+ If `:all`, all request headers will be output.
+ If array, output will be filtered by names in the array. (case-insensitive)

View File

@ -95,14 +95,14 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
def headers
request_headers = env[Grape::Env::GRAPE_REQUEST_HEADERS].to_hash
return request_headers if @options[:headers] == :all
return Hash[request_headers.sort] if @options[:headers] == :all
headers_needed = Array(@options[:headers])
Hash[request_headers.sort].select do |key, value|
headers_needed.any? do |need|
need.to_s.casecmp(key).zero?
end
result = {}
headers_needed.each do |need|
result.merge!(request_headers.select { |key, value| need.to_s.casecmp(key).zero? })
end
Hash[result.sort]
end
def start_time

View File

@ -52,8 +52,10 @@ FactoryGirl.define do
trait :prefixed_basic_headers do
other_env_params { {
'HTTP_VERSION' => 'HTTP/1.1',
'HTTP_CACHE_CONTROL' => 'max-age=0',
'HTTP_USER_AGENT' => 'Mozilla/5.0'
'HTTP_USER_AGENT' => 'Mozilla/5.0',
'HTTP_ACCEPT_LANGUAGE' => 'en-US'
} }
end
@ -103,8 +105,10 @@ FactoryGirl.define do
trait :basic_headers do
headers { {
'Version' => 'HTTP/1.1',
'Cache-Control' => 'max-age=0',
'User-Agent' => 'Mozilla/5.0'
'User-Agent' => 'Mozilla/5.0',
'Accept-Language' => 'en-US'
} }
end
end

View File

@ -19,7 +19,7 @@ describe Grape::Middleware::Logger, type: :integration do
expect(subject.logger).to receive(:info).with %Q(Started POST "/api/1.0/users" at #{subject.start_time})
expect(subject.logger).to receive(:info).with %Q(Processing by TestAPI/users)
expect(subject.logger).to receive(:info).with %Q( Parameters: {"id"=>"101001", "secret"=>"[FILTERED]", "customer"=>[], "name"=>"foo", "password"=>"[FILTERED]"})
expect(subject.logger).to receive(:info).with %Q( Headers: {"Cache-Control"=>"max-age=0", "User-Agent"=>"Mozilla/5.0"})
expect(subject.logger).to receive(:info).with %Q( Headers: {"Accept-Language"=>"en-US", "Cache-Control"=>"max-age=0", "User-Agent"=>"Mozilla/5.0", "Version"=>"HTTP/1.1"})
expect(subject.logger).to receive(:info).with /Completed 200 in \d+.\d+ms/
expect(subject.logger).to receive(:info).with ''
subject.call!(env)

View File

@ -14,8 +14,10 @@ describe Grape::Middleware::Logger do
context 'when @options[:headers] has a symbol :all' do
let(:options) { { headers: :all, logger: Object.new } }
it 'all request headers should be retrieved' do
expect(subject.headers.fetch('Accept-Language')).to eq('en-US')
expect(subject.headers.fetch('Cache-Control')).to eq('max-age=0')
expect(subject.headers.fetch('User-Agent')).to eq('Mozilla/5.0')
expect(subject.headers.fetch('Version')).to eq('HTTP/1.1')
end
end