2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "concurrent/atomic/count_down_latch"
|
2012-07-29 20:26:07 -04:00
|
|
|
|
|
|
|
module ActionController
|
|
|
|
module Live
|
|
|
|
class ResponseTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@response = Live::Response.new
|
2015-12-01 14:28:01 -05:00
|
|
|
@response.request = ActionDispatch::Request.empty
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
|
2012-08-13 13:57:26 -04:00
|
|
|
def test_header_merge
|
2016-08-06 12:54:50 -04:00
|
|
|
header = @response.header.merge("Foo" => "Bar")
|
2012-08-13 13:57:26 -04:00
|
|
|
assert_kind_of(ActionController::Live::Response::Header, header)
|
2012-12-28 18:49:41 -05:00
|
|
|
assert_not_equal header, @response.header
|
2012-08-13 13:57:26 -04:00
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
{ "omg" => "g" }
|
2012-08-13 14:20:04 -04:00
|
|
|
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
|
2015-07-13 14:22:54 -04:00
|
|
|
latch = Concurrent::CountDownLatch.new
|
2012-07-29 20:26:07 -04:00
|
|
|
|
|
|
|
t = Thread.new {
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.stream.write "foo"
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.wait
|
2012-07-29 20:26:07 -04:00
|
|
|
@response.stream.close
|
|
|
|
}
|
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
@response.await_commit
|
2012-07-29 20:26:07 -04:00
|
|
|
@response.each do |part|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo", part
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.count_down
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
assert t.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setting_body_populates_buffer
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.body = "omg"
|
2012-07-29 20:26:07 -04:00
|
|
|
@response.close
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal ["omg"], @response.body_parts
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
2012-07-29 20:29:20 -04:00
|
|
|
|
|
|
|
def test_cache_control_is_set
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.stream.write "omg"
|
|
|
|
assert_equal "no-cache", @response.headers["Cache-Control"]
|
2012-07-29 20:29:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_content_length_is_removed
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.headers["Content-Length"] = "1234"
|
|
|
|
@response.stream.write "omg"
|
|
|
|
assert_nil @response.headers["Content-Length"]
|
2012-07-29 20:29:20 -04:00
|
|
|
end
|
2012-07-29 20:55:39 -04:00
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
def test_headers_cannot_be_written_after_webserver_reads
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.stream.write "omg"
|
2015-07-13 14:22:54 -04:00
|
|
|
latch = Concurrent::CountDownLatch.new
|
2012-07-29 20:55:39 -04:00
|
|
|
|
2014-03-12 20:40:08 -04:00
|
|
|
t = Thread.new {
|
2016-03-03 01:43:18 -05:00
|
|
|
@response.each do
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.count_down
|
2014-03-12 20:40:08 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2015-07-13 14:22:54 -04:00
|
|
|
latch.wait
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @response.headers, :frozen?
|
2012-07-29 20:55:39 -04:00
|
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.headers["Content-Length"] = "zomg"
|
2012-07-29 20:55:39 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "header already sent", e.message
|
2014-03-12 20:40:08 -04:00
|
|
|
@response.stream.close
|
|
|
|
t.join
|
2012-07-29 20:55:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_headers_cannot_be_written_after_close
|
|
|
|
@response.stream.close
|
2015-10-05 19:50:37 -04:00
|
|
|
# we can add data until it's actually written, which happens on `each`
|
|
|
|
@response.each { |x| }
|
2012-07-29 20:55:39 -04:00
|
|
|
|
|
|
|
e = assert_raises(ActionDispatch::IllegalStateError) do
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.headers["Content-Length"] = "zomg"
|
2012-07-29 20:55:39 -04:00
|
|
|
end
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "header already sent", e.message
|
2012-07-29 20:55:39 -04:00
|
|
|
end
|
2012-07-29 20:26:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|