Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tomharvey committed Jun 8, 2024
1 parent fb1b50d commit d37de4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/rack/contrib/header_name_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Rack
# Middleware to change the name of a header
#
# So, if a server upstream of Rack sends {'X-Header-Name': "value"}
# you can change header to {'Whatever-You-Want': "value"}
#  you can change header to {'Whatever-You-Want': "value"}
#
# There is a specific use case when ensuring the scheme matches when
# comparing request.origin and request.base_url for CSRF checking,
Expand Down
35 changes: 25 additions & 10 deletions test/spec_rack_header_name_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
vendor_header = 'not passed in the request'
env = Rack::MockRequest.env_for('/', 'HTTP_X_FORWARDED_PROTO' => 'http')

Rack::Lint.new(Rack::HeaderNameTransformer.new(response, vendor_header, "bar")).call env
Rack::Lint.new(Rack::HeaderNameTransformer.new(response, vendor_header, 'bar')).call env

env['HTTP_X_FORWARDED_PROTO'].must_equal 'http'
end

it 'copies the value of the vendor header to a newly named header' do
env = Rack::MockRequest.env_for('/', {'HTTP_VENDOR' => 'value', 'HTTP_FOO' => 'foo'})
it 'copy the value of the vendor header to a newly named header' do
env = Rack::MockRequest.env_for('/', { 'HTTP_VENDOR' => 'value', 'HTTP_FOO' => 'foo' })

Rack::Lint.new(Rack::HeaderNameTransformer.new(response, 'Vendor', 'Standard')).call env
Rack::Lint.new(Rack::HeaderNameTransformer.new(response, 'Foo', 'Bar')).call env
Expand All @@ -26,19 +26,34 @@
end

# Real world headers and use cases
it 'copies the value of a vendor forward proto header to the standardised formward proto header' do
it 'copy the value of a vendor forward proto header to the standardised formward proto header' do
env = Rack::MockRequest.env_for('/', 'HTTP_VENDOR_FORWARDED_PROTO_HEADER' => 'https')

Rack::Lint.new(Rack::HeaderNameTransformer.new(response, 'Vendor-Forwarded-Proto-Header', 'X-Forwarded-Proto')).call env
Rack::Lint.new(
Rack::HeaderNameTransformer.new(
response,
'Vendor-Forwarded-Proto-Header',
'X-Forwarded-Proto'
)
).call env

env['HTTP_X_FORWARDED_PROTO'].must_equal 'https'
end

it 'copies the value of a vendor forward proto header to the standardised formward proto header, overwriting an old value in the request' do
env = Rack::MockRequest.env_for('/', 'HTTP_VENDOR_FORWARDED_PROTO_HEADER' => 'https',
'HTTP_X_FORWARDED_PROTO' => 'http')

Rack::Lint.new(Rack::HeaderNameTransformer.new(response, 'Vendor-Forwarded-Proto-Header', 'X-Forwarded-Proto')).call env
it 'copy the value of a vendor forward proto header to the standardised header, overwriting existing request value' do
env = Rack::MockRequest.env_for(
'/',
'HTTP_VENDOR_FORWARDED_PROTO_HEADER' => 'https',
'HTTP_X_FORWARDED_PROTO' => 'http'
)

Rack::Lint.new(
Rack::HeaderNameTransformer.new(
response,
'Vendor-Forwarded-Proto-Header',
'X-Forwarded-Proto'
)
).call env

env['HTTP_X_FORWARDED_PROTO'].must_equal 'https'
end
Expand Down

0 comments on commit d37de4b

Please sign in to comment.