diff --git a/lib/rack/contrib/header_name_transformer.rb b/lib/rack/contrib/header_name_transformer.rb index 52a1e26..557b107 100644 --- a/lib/rack/contrib/header_name_transformer.rb +++ b/lib/rack/contrib/header_name_transformer.rb @@ -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, diff --git a/test/spec_rack_header_name_transformer.rb b/test/spec_rack_header_name_transformer.rb index 7cebf15..d289d74 100644 --- a/test/spec_rack_header_name_transformer.rb +++ b/test/spec_rack_header_name_transformer.rb @@ -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 @@ -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