improve test coverage

This commit is contained in:
Ryan Tomayko 2008-12-21 02:11:25 -08:00
parent 50999bf3fc
commit ed923caf42
5 changed files with 59 additions and 16 deletions

View File

@ -35,7 +35,6 @@ module Sinatra
else
response.body = value
end
response.body
end
# Halt processing and redirect to the URI provided.
@ -136,22 +135,16 @@ module Sinatra
# When the current request includes an 'If-None-Match' header with a
# matching etag, execution is immediately halted. If the request method is
# GET or HEAD, a '304 Not Modified' response is sent.
def etag(value, strength=:strong)
value =
case strength
when :strong then '"%s"' % value
when :weak then 'W/"%s"' % value
else raise TypeError, "strength must be one of :strong or :weak"
end
def etag(value, kind=:strong)
raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
value = '"%s"' % value
value = 'W/' + value if kind == :weak
response['ETag'] = value
# Check for If-None-Match request header and halt if match is found.
etags = (request.env['HTTP_IF_NONE_MATCH'] || '').split(/\s*,\s*/)
if etags.include?(value) || etags.include?('*')
# GET/HEAD requests: send Not Modified response
halt 304 if request.get? || request.head?
# Other requests: send Precondition Failed response
halt 412
# Conditional GET check
if etags = env['HTTP_IF_NONE_MATCH']
etags = etags.split(/\s*,\s*/)
halt 304 if etags.include?(value) || etags.include?('*')
end
end
end
@ -377,7 +370,8 @@ module Sinatra
@env['sinatra.error'] = boom
@response.status = 404
@response.body = ['<h1>Not Found</h1>']
invoke errmap[NotFound] if errmap.key?(NotFound)
handler = errmap[boom.class] || errmap[NotFound]
invoke handler unless handler.nil?
rescue ::Exception => boom
@env['sinatra.error'] = boom
raise boom if options.raise_errors?

View File

@ -322,5 +322,17 @@ describe 'Sinatra::Helpers' do
status.should.be 304
body.should.be.empty
end
it 'uses a weak etag with the :weak option' do
mock_app {
get '/' do
etag 'FOO', :weak
"that's weak, dude."
end
}
get '/'
response['ETag'].should.equal 'W/"FOO"'
end
end
end

View File

@ -71,6 +71,23 @@ describe 'Exception Mappings' do
lambda { get '/' }.should.not.raise Sinatra::NotFound
status.should.equal 404
end
class FooNotFound < Sinatra::NotFound
end
it "cascades for subclasses of Sinatra::NotFound" do
mock_app {
set :raise_errors, true
error(FooNotFound) { "foo! not found." }
get '/' do
raise FooNotFound
end
}
lambda { get '/' }.should.not.raise FooNotFound
status.should.equal 404
body.should.equal 'foo! not found.'
end
end
describe 'Custom Error Pages' do

View File

@ -92,4 +92,12 @@ describe 'Options' do
@app.foo.should.be false
@app.bar.should.be false
end
it 'enables MethodOverride middleware when :methodoverride is enabled' do
@app.set :methodoverride, true
@app.put('/') { 'okay' }
post '/', {'_method'=>'PUT'}, {}
status.should.equal 200
body.should.equal 'okay'
end
end

View File

@ -68,6 +68,18 @@ describe 'Result Handling' do
body.should.equal 'Hello World'
end
it "sets status and body when result is a two-tuple" do
mock_app {
get '/' do
[409, 'formula of']
end
}
get '/'
status.should.equal 409
body.should.equal 'formula of'
end
it "sets status when result is a Fixnum status code" do
mock_app {
get('/') { 205 }