mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
less code = great code
This commit is contained in:
parent
becd6d8ab0
commit
d41cccae7b
2 changed files with 16 additions and 25 deletions
|
@ -134,12 +134,14 @@ module Sinatra
|
|||
|
||||
module ResponseHelpers
|
||||
|
||||
def redirect(path)
|
||||
throw :halt, Redirect.new(path)
|
||||
def redirect(path, *args)
|
||||
status(302)
|
||||
headers 'Location' => path
|
||||
throw :halt, *args
|
||||
end
|
||||
|
||||
def send_file(filename)
|
||||
throw :halt, SendFile.new(filename)
|
||||
throw :halt, File.read(filename)
|
||||
end
|
||||
|
||||
def headers(header = nil)
|
||||
|
@ -258,28 +260,6 @@ module Sinatra
|
|||
|
||||
end
|
||||
|
||||
class Redirect
|
||||
def initialize(path)
|
||||
@path = path
|
||||
end
|
||||
|
||||
def to_result(cx, *args)
|
||||
cx.status(302)
|
||||
cx.header.merge!('Location' => @path)
|
||||
cx.body = ''
|
||||
end
|
||||
end
|
||||
|
||||
class SendFile
|
||||
def initialize(filename)
|
||||
@filename = filename
|
||||
end
|
||||
|
||||
def to_result(cx, *args)
|
||||
cx.body = File.read(@filename)
|
||||
end
|
||||
end
|
||||
|
||||
class Application
|
||||
|
||||
attr_reader :events, :layouts, :default_options, :filters
|
||||
|
|
|
@ -45,6 +45,17 @@ context "Sinatra" do
|
|||
body.should.equal 'Mizerany'
|
||||
end
|
||||
|
||||
specify "renders a body with a redirect" do
|
||||
Sinatra::EventContext.any_instance.expects(:foo).returns('blah')
|
||||
get "/" do
|
||||
redirect 'foo', :foo
|
||||
end
|
||||
get_it '/'
|
||||
should.be.redirection
|
||||
headers['Location'].should.equal 'foo'
|
||||
body.should.equal 'blah'
|
||||
end
|
||||
|
||||
specify "body sets content and ends event" do
|
||||
|
||||
Sinatra::EventContext.any_instance.expects(:foo).never
|
||||
|
|
Loading…
Reference in a new issue