2009-02-03 19:50:22 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2009-01-19 20:58:26 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class ResponseTest < Test::Unit::TestCase
|
|
|
|
setup do
|
2009-01-19 20:58:26 -05:00
|
|
|
@response = Sinatra::Response.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "initializes with 200, text/html, and empty body" do
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal 'text/html', @response['Content-Type']
|
|
|
|
assert_equal [], @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses case insensitive headers' do
|
|
|
|
@response['content-type'] = 'application/foo'
|
|
|
|
assert_equal 'application/foo', @response['Content-Type']
|
|
|
|
assert_equal 'application/foo', @response['CONTENT-TYPE']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes to body' do
|
|
|
|
@response.body = 'Hello'
|
|
|
|
@response.write ' World'
|
2011-05-13 04:35:17 -04:00
|
|
|
assert_equal 'Hello World', @response.body.join
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
[204, 304].each do |status_code|
|
|
|
|
it "removes the Content-Type header and body when response status is #{status_code}" do
|
|
|
|
@response.status = status_code
|
|
|
|
@response.body = ['Hello World']
|
|
|
|
assert_equal [status_code, {}, []], @response.finish
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 19:50:22 -05:00
|
|
|
|
|
|
|
it 'Calculates the Content-Length using the bytesize of the body' do
|
|
|
|
@response.body = ['Hello', 'World!', '✈']
|
|
|
|
status, headers, body = @response.finish
|
|
|
|
assert_equal '14', headers['Content-Length']
|
2011-08-17 05:51:45 -04:00
|
|
|
assert_equal @response.body, body
|
2009-02-03 19:50:22 -05:00
|
|
|
end
|
2011-05-13 05:00:05 -04:00
|
|
|
|
2011-07-12 04:41:26 -04:00
|
|
|
it 'does not call #to_ary or #inject on the body' do
|
|
|
|
object = Object.new
|
|
|
|
def object.inject(*) fail 'called' end
|
|
|
|
def object.to_ary(*) fail 'called' end
|
|
|
|
def object.each(*) end
|
|
|
|
@response.body = object
|
|
|
|
assert @response.finish
|
|
|
|
end
|
|
|
|
|
2011-05-13 05:00:05 -04:00
|
|
|
it 'does not nest a Sinatra::Response' do
|
|
|
|
@response.body = Sinatra::Response.new ["foo"]
|
|
|
|
assert_equal @response.body, ["foo"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not nest a Rack::Response' do
|
|
|
|
@response.body = Rack::Response.new ["foo"]
|
|
|
|
assert_equal @response.body, ["foo"]
|
|
|
|
end
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|