Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Additional response headers for redirects (301 and 302)
  • Loading branch information
geku committed Aug 17, 2011
1 parent 79c8e38 commit d69a360
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rack/rewrite/rule.rb
Expand Up @@ -81,11 +81,12 @@ def matches?(rack_env) #:nodoc:
# (b) alter env as necessary and return true
def apply!(env) #:nodoc:
interpreted_to = self.interpret_to(env)
additional_headers = @options[:headers] || {}
case self.rule_type
when :r301
[301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}, [redirect_message(interpreted_to)]]
[301, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
when :r302
[302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}, [redirect_message(interpreted_to)]]
[302, {'Location' => interpreted_to, 'Content-Type' => Rack::Mime.mime_type(::File.extname(interpreted_to))}.merge!(additional_headers), [redirect_message(interpreted_to)]]
when :rewrite
# return [200, {}, {:content => env.inspect}]
env['REQUEST_URI'] = interpreted_to
Expand Down
8 changes: 8 additions & 0 deletions test/rule_test.rb
Expand Up @@ -86,6 +86,14 @@ def negative_lookahead_regexp
end
end

should 'set additional headers for a 301 and 302 request' do
[:r301, :r302].each do |rule_type|
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css', {:headers => {'Cache-Control' => 'no-cache'}})
env = {'PATH_INFO' => '/abc'}
assert_equal 'no-cache', rule.apply!(env)[1]['Cache-Control']
end
end

context 'Given an :x_send_file rule that matches' do
setup do
@file = File.join(TEST_ROOT, 'geminstaller.yml')
Expand Down

0 comments on commit d69a360

Please sign in to comment.