From 8019e117f849715591da3e244c8a712c797ade8a Mon Sep 17 00:00:00 2001 From: bmizerany Date: Thu, 29 Jan 2009 19:37:20 -0800 Subject: [PATCH] Sugar added: redirect back --- lib/sinatra/base.rb | 4 ++++ test/base_test.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 82db2a59..dc197afa 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -194,6 +194,10 @@ module Sinatra halt 304 if etags.include?(value) || etags.include?('*') end end + + ## Sugar for redirect (example: redirect back) + def back ; request.referer ; end + end module Templates diff --git a/test/base_test.rb b/test/base_test.rb index a6ba62cf..137c056c 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -65,4 +65,17 @@ describe 'Sinatra::Base' do assert response.ok? assert_equal 'not foo', response.body end + + it "makes redirecting back pretty" do + app = mock_app { + get '/foo' do + redirect back + end + } + + request = Rack::MockRequest.new(app) + response = request.get('/foo', 'HTTP_REFERER' => 'http://github.com') + assert response.redirect? + assert_equal "http://github.com", response.location + end end