-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
91 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,88 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Castle::Extractors::Headers do | ||
subject(:extractor) { described_class.new(request) } | ||
subject(:headers) { described_class.new(request).call } | ||
|
||
let(:client_id) { 'abcd' } | ||
let(:env) do | ||
Rack::MockRequest.env_for('/', | ||
'HTTP_X_FORWARDED_FOR' => '1.2.3.4', | ||
'HTTP_OK' => 'OK', | ||
'TEST' => '1', | ||
'HTTP_COOKIE' => "__cid=#{client_id};other=efgh") | ||
Rack::MockRequest.env_for( | ||
'/', | ||
'Action-Dispatch.request.content-Type' => 'application/json', | ||
'HTTP_AUTHORIZATION' => 'Basic 123456', | ||
'HTTP_COOKIE' => "__cid=#{client_id};other=efgh", | ||
'HTTP_OK' => 'OK', | ||
'HTTP_ACCEPT' => 'application/json', | ||
'HTTP_X_FORWARDED_FOR' => '1.2.3.4', | ||
'HTTP_USER_AGENT' => 'Mozilla 1234', | ||
'TEST' => '1' | ||
) | ||
end | ||
let(:request) { Rack::Request.new(env) } | ||
|
||
describe 'extract http headers with whitelisted and blacklisted support' do | ||
context 'when whitelist is not set in the configuration' do | ||
it do | ||
is_expected.to eq('Accept' => 'application/json', | ||
'Authorization' => true, | ||
'Cookie' => true, | ||
'Content-Length' => '0', | ||
'Ok' => 'OK', | ||
'User-Agent' => 'Mozilla 1234', | ||
'X-Forwarded-For' => '1.2.3.4') | ||
end | ||
end | ||
|
||
context 'when whitelist is set in the configuration' do | ||
before { Castle.config.whitelisted = %w[Accept OK] } | ||
|
||
it do | ||
is_expected.to eq('Accept' => 'application/json', | ||
'Authorization' => true, | ||
'Cookie' => true, | ||
'Content-Length' => true, | ||
'Ok' => 'OK', | ||
'User-Agent' => 'Mozilla 1234', | ||
'X-Forwarded-For' => true) | ||
end | ||
end | ||
|
||
context 'when blacklist is set in the configuration' do | ||
context 'with a User-Agent' do | ||
before { Castle.config.blacklisted = %w[User-Agent] } | ||
|
||
it do | ||
is_expected.to eq('Accept' => 'application/json', | ||
'Authorization' => true, | ||
'Cookie' => true, | ||
'Content-Length' => '0', | ||
'Ok' => 'OK', | ||
'User-Agent' => 'Mozilla 1234', | ||
'X-Forwarded-For' => '1.2.3.4') | ||
end | ||
end | ||
|
||
context 'with a different header' do | ||
before { Castle.config.blacklisted = %w[Accept] } | ||
|
||
it do | ||
is_expected.to eq('Accept' => true, | ||
'Authorization' => true, | ||
'Cookie' => true, | ||
'Content-Length' => '0', | ||
'Ok' => 'OK', | ||
'User-Agent' => 'Mozilla 1234', | ||
'X-Forwarded-For' => '1.2.3.4') | ||
end | ||
end | ||
end | ||
|
||
context 'when a header is both whitelisted and blacklisted' do | ||
before do | ||
Castle.config.whitelisted += ['TEST'] | ||
Castle.config.whitelisted = %w[Accept] | ||
Castle.config.blacklisted = %w[Accept] | ||
end | ||
|
||
it do | ||
expect(extractor.call).to eql( | ||
'Test' => '1', 'Ok' => true, 'Rack.version' => true, | ||
'Rack.input' => true, 'Rack.errors' => true, 'Rack.multithread' => true, | ||
'Rack.multiprocess' => true, 'Rack.run-Once' => true, 'Request-Method' => true, | ||
'Server-Name' => true, 'Server-Port' => true, 'Query-String' => true, | ||
'Path-Info' => true, 'Rack.url-Scheme' => true, 'Https' => true, | ||
'Script-Name' => true, 'Content-Length' => true, 'X-Forwarded-For' => '1.2.3.4', | ||
'Cookie' => true | ||
) | ||
expect(headers['Accept']).to eq(true) | ||
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