2012-07-29 20:26:07 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'active_support/concurrency/latch'
|
|
|
|
|
|
|
|
module ActionController
|
|
|
|
module Live
|
|
|
|
class ResponseTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@response = Live::Response.new
|
|
|
|
end
|
|
|
|
|
2012-08-13 13:57:26 -04:00
|
|
|
def test_header_merge
|
|
|
|
header = @response.header.merge('Foo' => 'Bar')
|
|
|
|
assert_kind_of(ActionController::Live::Response::Header, header)
|
|
|
|
refute_equal header, @response.header
|
|
|
|
end
|
|
|
|
|
2012-08-13 14:20:04 -04:00
|
|
|
def test_initialize_with_default_headers
|
|
|
|
r = Class.new(Live::Response) do
|
|
|
|
def self.default_headers
|
|
|
|
{ 'omg' => 'g' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
header = r.new.header
|
|
|
|
assert_kind_of(ActionController::Live::Response::Header, header)
|
|
|
|
end
|
|
|
|
|
2012-07-29 20:26:07 -04:00
|
|
|
def test_parallel
|
|
|
|
latch = ActiveSupport::Concurrency::Latch.new
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
@response.stream.write 'foo'
|
|
|
|
latch.await
|
|
|
|
@response.stream.close
|
|
|
|
}
|
|
|
|
|
|
|
|
@response.each do |part|
|
|
|
|
assert_equal 'foo', part
|
|
|
|
latch.release
|
|
|
|
end
|
|
|
|
assert t.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setting_body_populates_buffer
|
|
|
|
@response.body = 'omg'
|
|
|
|
@response.close
|
|
|
|
assert_equal ['omg'], @response.body_parts
|
|
|
|
end
|
2012-07-29 20:29:20 -04:00
|
|
|
|
|
|
|
def test_cache_control_is_set
|
|
|
|
@response.stream.write 'omg'
|
|
|
|
assert_equal 'no-cache', @response.headers['Cache-Control']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_length_is_removed
|
|
|
|
@response.headers['Content-Length'] = "1234"
|
|
|
|
@response.stream.write 'omg'
|
|
|
|
assert_nil @response.headers['Content-Length']
|
|
|
|
end
|
2012-07-29 20:55:39 -04:00
|
|
|
|
|
|
|
def test_headers_cannot_be_written_after_write
|
|
|
|
@response.stream.write 'omg'
|
|
|
|
|
2012-07-29 22:51:21 -04:00
|
|
|
assert @response.headers.frozen?
|
2012-07-29 20:55:39 -04:00
|
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
|
|
|
@response.headers['Content-Length'] = "zomg"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'header already sent', e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_headers_cannot_be_written_after_close
|
|
|
|
@response.stream.close
|
|
|
|
|
2012-07-29 22:51:21 -04:00
|
|
|
assert @response.headers.frozen?
|
2012-07-29 20:55:39 -04:00
|
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
|
|
|
@response.headers['Content-Length'] = "zomg"
|
|
|
|
end
|
|
|
|
assert_equal 'header already sent', e.message
|
|
|
|
end
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|