-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from visits-works/feature/with_header_option
Add 'headers' option to log request headers
- Loading branch information
Showing
5 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
spec/integration/lib/grape/middleware/headers_option_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Middleware::Logger, type: :integration do | ||
let(:app) { build :app } | ||
|
||
subject { described_class.new(app, options) } | ||
|
||
let(:grape_endpoint) { build(:grape_endpoint) } | ||
let(:env) { build(:expected_env, :prefixed_basic_headers, grape_endpoint: grape_endpoint) } | ||
|
||
context ':all option is set to option headers' do | ||
let(:options) { { | ||
filter: build(:param_filter), | ||
headers: :all, | ||
logger: Logger.new(Tempfile.new('logger')) | ||
} } | ||
it 'all headers will be shown, headers will be sorted by name' do | ||
expect(subject.logger).to receive(:info).with '' | ||
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: {"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) | ||
end | ||
end | ||
|
||
context 'list of names ["User-Agent", "Cache-Control"] is set to option headers' do | ||
let(:options) { { | ||
filter: build(:param_filter), | ||
headers: %w(User-Agent Cache-Control), | ||
logger: Logger.new(Tempfile.new('logger')) | ||
} } | ||
it 'two headers will be shown' do | ||
expect(subject.logger).to receive(:info).with '' | ||
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 /Completed 200 in \d+.\d+ms/ | ||
expect(subject.logger).to receive(:info).with '' | ||
subject.call!(env) | ||
end | ||
end | ||
|
||
context 'a single string "Cache-Control" is set to option headers' do | ||
let(:options) { { | ||
filter: build(:param_filter), | ||
headers: 'Cache-Control', | ||
logger: Logger.new(Tempfile.new('logger')) | ||
} } | ||
it 'only Cache-Control header will be shown' do | ||
expect(subject.logger).to receive(:info).with '' | ||
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"}) | ||
expect(subject.logger).to receive(:info).with /Completed 200 in \d+.\d+ms/ | ||
expect(subject.logger).to receive(:info).with '' | ||
subject.call!(env) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Middleware::Logger do | ||
let(:app) { double('app') } | ||
|
||
subject { described_class.new(app, options) } | ||
|
||
describe '#headers' do | ||
let(:grape_request) { build :grape_request, :basic_headers } | ||
let(:env) { build :expected_env, grape_request: grape_request } | ||
|
||
before { subject.instance_variable_set(:@env, env) } | ||
|
||
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 | ||
|
||
context 'when @options[:headers] is a string "user-agent"' do | ||
let(:options) { { headers: 'user-agent', logger: Object.new } } | ||
it 'only "User-Agent" should be retrieved' do | ||
expect(subject.headers.fetch('User-Agent')).to eq('Mozilla/5.0') | ||
expect(subject.headers.length).to eq(1) | ||
end | ||
end | ||
|
||
context 'when @options[:headers] is an array of ["user-agent", "Cache-Control", "Unknown"]' do | ||
let(:options) { { headers: %w(user-agent Cache-Control Unknown), logger: Object.new } } | ||
it '"User-Agent" and "Cache-Control" should be retrieved' do | ||
expect(subject.headers.fetch('Cache-Control')).to eq('max-age=0') | ||
expect(subject.headers.fetch('User-Agent')).to eq('Mozilla/5.0') | ||
end | ||
it '"Unknown" name does not make any effect' do | ||
expect(subject.headers.length).to eq(2) | ||
end | ||
end | ||
end | ||
|
||
describe '#headers if no request header' do | ||
let(:env) { build :expected_env } | ||
before { subject.instance_variable_set(:@env, env) } | ||
|
||
context 'when @options[:headers] is set, but no request header is there' do | ||
let(:options) { { headers: %w(user-agent Cache-Control), logger: Object.new } } | ||
it 'subject.headers should return empty hash' do | ||
expect(subject.headers.length).to eq(0) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|