Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Dec 21, 2008
1 parent 50999bf commit ed923ca
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/sinatra/base.rb
Expand Up @@ -35,7 +35,6 @@ def block.each ; yield call ; end
else
response.body = value
end
response.body
end

# Halt processing and redirect to the URI provided.
Expand Down Expand Up @@ -136,22 +135,16 @@ def last_modified(time)
# 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
Expand Down Expand Up @@ -377,7 +370,8 @@ def error_detection
@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?
Expand Down
12 changes: 12 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -322,5 +322,17 @@ def send_file_app
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
17 changes: 17 additions & 0 deletions test/mapped_error_test.rb
Expand Up @@ -71,6 +71,23 @@ class FooError < RuntimeError
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
Expand Down
8 changes: 8 additions & 0 deletions test/options_test.rb
Expand Up @@ -92,4 +92,12 @@ def foo=(value)
@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
12 changes: 12 additions & 0 deletions test/result_test.rb
Expand Up @@ -68,6 +68,18 @@ def res.each ; yield call ; end
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 }
Expand Down

0 comments on commit ed923ca

Please sign in to comment.