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:
parent
753b94ecb0
commit
b52ea9adeb
|
@ -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 `: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 `: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 `:all`, all request headers will be output.
|
||||||
+ If array, output will be filtered by names in the array. (case-insensitive)
|
+ If array, output will be filtered by names in the array. (case-insensitive)
|
||||||
|
|
||||||
|
|
|
@ -95,14 +95,14 @@ class Grape::Middleware::Logger < Grape::Middleware::Globals
|
||||||
|
|
||||||
def headers
|
def headers
|
||||||
request_headers = env[Grape::Env::GRAPE_REQUEST_HEADERS].to_hash
|
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])
|
headers_needed = Array(@options[:headers])
|
||||||
Hash[request_headers.sort].select do |key, value|
|
result = {}
|
||||||
headers_needed.any? do |need|
|
headers_needed.each do |need|
|
||||||
need.to_s.casecmp(key).zero?
|
result.merge!(request_headers.select { |key, value| need.to_s.casecmp(key).zero? })
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
Hash[result.sort]
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_time
|
def start_time
|
||||||
|
|
|
@ -52,8 +52,10 @@ FactoryGirl.define do
|
||||||
|
|
||||||
trait :prefixed_basic_headers do
|
trait :prefixed_basic_headers do
|
||||||
other_env_params { {
|
other_env_params { {
|
||||||
|
'HTTP_VERSION' => 'HTTP/1.1',
|
||||||
'HTTP_CACHE_CONTROL' => 'max-age=0',
|
'HTTP_CACHE_CONTROL' => 'max-age=0',
|
||||||
'HTTP_USER_AGENT' => 'Mozilla/5.0'
|
'HTTP_USER_AGENT' => 'Mozilla/5.0',
|
||||||
|
'HTTP_ACCEPT_LANGUAGE' => 'en-US'
|
||||||
} }
|
} }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -103,8 +105,10 @@ FactoryGirl.define do
|
||||||
|
|
||||||
trait :basic_headers do
|
trait :basic_headers do
|
||||||
headers { {
|
headers { {
|
||||||
|
'Version' => 'HTTP/1.1',
|
||||||
'Cache-Control' => 'max-age=0',
|
'Cache-Control' => 'max-age=0',
|
||||||
'User-Agent' => 'Mozilla/5.0'
|
'User-Agent' => 'Mozilla/5.0',
|
||||||
|
'Accept-Language' => 'en-US'
|
||||||
} }
|
} }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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(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(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( 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 /Completed 200 in \d+.\d+ms/
|
||||||
expect(subject.logger).to receive(:info).with ''
|
expect(subject.logger).to receive(:info).with ''
|
||||||
subject.call!(env)
|
subject.call!(env)
|
||||||
|
|
|
@ -14,8 +14,10 @@ describe Grape::Middleware::Logger do
|
||||||
context 'when @options[:headers] has a symbol :all' do
|
context 'when @options[:headers] has a symbol :all' do
|
||||||
let(:options) { { headers: :all, logger: Object.new } }
|
let(:options) { { headers: :all, logger: Object.new } }
|
||||||
it 'all request headers should be retrieved' do
|
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('Cache-Control')).to eq('max-age=0')
|
||||||
expect(subject.headers.fetch('User-Agent')).to eq('Mozilla/5.0')
|
expect(subject.headers.fetch('User-Agent')).to eq('Mozilla/5.0')
|
||||||
|
expect(subject.headers.fetch('Version')).to eq('HTTP/1.1')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue