1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

add helpers for dealing with status codes

This commit is contained in:
Konstantin Haase 2011-06-10 13:19:36 +02:00
parent f38927e0bc
commit f9a7338b4d
2 changed files with 135 additions and 10 deletions

View file

@ -310,6 +310,37 @@ module Sinatra
request.referer
end
# whether or not the status is set to 1xx
def informational?
status.between? 100, 199
end
# whether or not the status is set to 2xx
def success?
status.between? 200, 299
end
# whether or not the status is set to 3xx
def redirect?
status.between? 300, 399
end
# whether or not the status is set to 4xx
def client_error?
status.between? 400, 499
end
# whether or not the status is set to 5xx
def server_error?
status.between? 500, 599
end
# whether or not the status is set to 404
def not_found?
status == 404
end
private
# Ruby 1.8 has no #to_time method.

View file

@ -6,19 +6,113 @@ class HelpersTest < Test::Unit::TestCase
assert true
end
def status_app(code, &block)
block ||= proc { }
mock_app do
get '/' do
status code
instance_eval(&block).inspect
end
end
get '/'
end
describe 'status' do
setup do
mock_app {
get '/' do
status 207
nil
end
}
it 'sets the response status code' do
status_app 207
assert_equal 207, response.status
end
end
describe 'not_found?' do
it 'is true for status == 404' do
status_app(404) { not_found? }
assert_body 'true'
end
it 'sets the response status code' do
get '/'
assert_equal 207, response.status
it 'is false for status > 404' do
status_app(405) { not_found? }
assert_body 'false'
end
it 'is false for status < 404' do
status_app(403) { not_found? }
assert_body 'false'
end
end
describe 'informational?' do
it 'is true for 1xx status' do
status_app(100 + rand(100)) { informational? }
assert_body 'true'
end
it 'is false for status > 199' do
status_app(200 + rand(400)) { informational? }
assert_body 'false'
end
end
describe 'success?' do
it 'is true for 2xx status' do
status_app(200 + rand(100)) { success? }
assert_body 'true'
end
it 'is false for status < 200' do
status_app(100 + rand(100)) { success? }
assert_body 'false'
end
it 'is false for status > 299' do
status_app(300 + rand(300)) { success? }
assert_body 'false'
end
end
describe 'redirect?' do
it 'is true for 3xx status' do
status_app(300 + rand(100)) { redirect? }
assert_body 'true'
end
it 'is false for status < 300' do
status_app(200 + rand(100)) { redirect? }
assert_body 'false'
end
it 'is false for status > 399' do
status_app(400 + rand(200)) { redirect? }
assert_body 'false'
end
end
describe 'client_error?' do
it 'is true for 4xx status' do
status_app(400 + rand(100)) { client_error? }
assert_body 'true'
end
it 'is false for status < 400' do
status_app(200 + rand(200)) { client_error? }
assert_body 'false'
end
it 'is false for status > 499' do
status_app(500 + rand(100)) { client_error? }
assert_body 'false'
end
end
describe 'server_error?' do
it 'is true for 5xx status' do
status_app(500 + rand(100)) { server_error? }
assert_body 'true'
end
it 'is false for status < 500' do
status_app(200 + rand(300)) { server_error? }
assert_body 'false'
end
end