test/helpers_test.rb in sinatra-1.0 vs test/helpers_test.rb in sinatra-1.1.a
- old
+ new
@@ -1,6 +1,7 @@
require File.dirname(__FILE__) + '/helper'
+require 'date'
class HelpersTest < Test::Unit::TestCase
def test_default
assert true
end
@@ -55,11 +56,11 @@
}
get '/'
assert_equal 302, status
assert_equal '', body
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
it 'uses the code given when specified' do
mock_app {
get '/' do
@@ -69,11 +70,11 @@
}
get '/'
assert_equal 301, status
assert_equal '', body
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
it 'redirects back to request.referer when passed back' do
mock_app {
get '/try_redirect' do
@@ -82,12 +83,36 @@
}
request = Rack::MockRequest.new(@app)
response = request.get('/try_redirect', 'HTTP_REFERER' => '/foo')
assert_equal 302, response.status
- assert_equal '/foo', response['Location']
+ assert_equal 'http://example.org/foo', response['Location']
end
+
+ it 'redirects using a non-standard HTTP port' do
+ mock_app {
+ get '/' do
+ redirect '/foo'
+ end
+ }
+
+ request = Rack::MockRequest.new(@app)
+ response = request.get('/', 'SERVER_PORT' => '81')
+ assert_equal 'http://example.org:81/foo', response['Location']
+ end
+
+ it 'redirects using a non-standard HTTPS port' do
+ mock_app {
+ get '/' do
+ redirect '/foo'
+ end
+ }
+
+ request = Rack::MockRequest.new(@app)
+ response = request.get('/', 'SERVER_PORT' => '444')
+ assert_equal 'http://example.org:444/foo', response['Location']
+ end
end
describe 'error' do
it 'sets a status code and halts' do
mock_app {
@@ -264,25 +289,25 @@
'Hello World'
end
}
get '/'
- assert_equal 'text/plain', response['Content-Type']
+ assert_equal 'text/plain;charset=utf-8', response['Content-Type']
assert_equal 'Hello World', body
end
it 'takes media type parameters (like charset=)' do
mock_app {
get '/' do
- content_type 'text/html', :charset => 'utf-8'
+ content_type 'text/html', :charset => 'latin1'
"<h1>Hello, World</h1>"
end
}
get '/'
assert ok?
- assert_equal 'text/html;charset=utf-8', response['Content-Type']
+ assert_equal 'text/html;charset=latin1', response['Content-Type']
assert_equal "<h1>Hello, World</h1>", body
end
it "looks up symbols in Rack's mime types dictionary" do
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
@@ -293,11 +318,11 @@
end
}
get '/foo.xml'
assert ok?
- assert_equal 'application/foo', response['Content-Type']
+ assert_equal 'application/foo;charset=utf-8', response['Content-Type']
assert_equal 'I AM FOO', body
end
it 'fails when no mime type is registered for the argument provided' do
mock_app {
@@ -339,13 +364,25 @@
end
it 'sets the Content-Type response header if a mime-type can be located' do
send_file_app
get '/file.txt'
- assert_equal 'text/plain', response['Content-Type']
+ assert_equal 'text/plain;charset=utf-8', response['Content-Type']
end
+ it 'sets the Content-Type response header if type option is set to a file extesion' do
+ send_file_app :type => 'html'
+ get '/file.txt'
+ assert_equal 'text/html;charset=utf-8', response['Content-Type']
+ end
+
+ it 'sets the Content-Type response header if type option is set to a mime type' do
+ send_file_app :type => 'application/octet-stream'
+ get '/file.txt'
+ assert_equal 'application/octet-stream;charset=utf-8', response['Content-Type']
+ end
+
it 'sets the Content-Length response header' do
send_file_app
get '/file.txt'
assert_equal 'Hello World'.length.to_s, response['Content-Length']
end
@@ -421,45 +458,112 @@
assert_not_nil response['Expires']
end
end
describe 'last_modified' do
- setup do
- now = Time.now
- mock_app {
- get '/' do
- body { 'Hello World' }
- last_modified now
- 'Boo!'
- end
- }
- @now = now
- end
+ it 'ignores nil' do
+ mock_app do
+ get '/' do last_modified nil; 200; end
+ end
- it 'sets the Last-Modified header to a valid RFC 2616 date value' do
get '/'
- assert_equal @now.httpdate, response['Last-Modified']
+ assert ! response['Last-Modified']
end
- it 'returns a body when conditional get misses' do
- get '/'
- assert_equal 200, status
- assert_equal 'Boo!', body
- end
+ [Time, DateTime].each do |klass|
+ describe "with #{klass.name}" do
+ setup do
+ last_modified_time = klass.now
+ mock_app do
+ get '/' do
+ last_modified last_modified_time
+ 'Boo!'
+ end
+ end
+ @last_modified_time = Time.parse last_modified_time.to_s
+ end
- it 'halts when a conditional GET matches' do
- get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @now.httpdate }
- assert_equal 304, status
- assert_equal '', body
- end
+ # fixes strange missing test error when running complete test suite.
+ it("does not complain about missing tests") { }
- it 'ignores nil' do
- mock_app {
- get '/' do last_modified nil; 200; end
- }
+ context "when there's no If-Modified-Since header" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/'
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
- get '/'
- assert ! response['Last-Modified']
+ it 'conditional GET misses and returns a body' do
+ get '/'
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
+ end
+
+ context "when there's an invalid If-Modified-Since header" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'a really weird date' }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET misses and returns a body' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'a really weird date' }
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
+ end
+
+ context "when the resource has been modified since the If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time - 1).httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET misses and returns a body' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time - 1).httpdate }
+ assert_equal 200, status
+ assert_equal 'Boo!', body
+ end
+
+ it 'does not rely on string comparison' do
+ mock_app do
+ get '/compare' do
+ last_modified "Mon, 18 Oct 2010 20:57:11 GMT"
+ "foo"
+ end
+ end
+
+ get '/compare', {}, { 'HTTP_IF_MODIFIED_SINCE' => 'Sun, 26 Sep 2010 23:43:52 GMT' }
+ assert_equal 200, status
+ assert_equal 'foo', body
+ end
+ end
+
+ context "when the resource has been modified on the exact If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @last_modified_time.httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET matches and halts' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => @last_modified_time.httpdate }
+ assert_equal 304, status
+ assert_equal '', body
+ end
+ end
+
+ context "when the resource hasn't been modified since the If-Modified-Since header date" do
+ it 'sets the Last-Modified header to a valid RFC 2616 date value' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time + 1).httpdate }
+ assert_equal @last_modified_time.httpdate, response['Last-Modified']
+ end
+
+ it 'conditional GET matches and halts' do
+ get '/', {}, { 'HTTP_IF_MODIFIED_SINCE' => (@last_modified_time + 1).httpdate }
+ assert_equal 304, status
+ assert_equal '', body
+ end
+ end
+ end
end
end
describe 'etag' do
setup do