Permalink
Browse files

improve test coverage

  • Loading branch information...
1 parent 50999bf commit ed923caf420d69680cf88f5c1809471636db6a91 @rtomayko rtomayko committed Dec 21, 2008
Showing with 59 additions and 16 deletions.
  1. +10 −16 lib/sinatra/base.rb
  2. +12 −0 test/helpers_test.rb
  3. +17 −0 test/mapped_error_test.rb
  4. +8 −0 test/options_test.rb
  5. +12 −0 test/result_test.rb
View
@@ -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.
@@ -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
@@ -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?
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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 }

0 comments on commit ed923ca

Please sign in to comment.