class ResponseTest

Public Instance Methods

setup() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 8
def setup
  @response = ActionDispatch::Response.create
  @response.request = ActionDispatch::Request.empty
end
test_can_wait_until_commit() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 13
def test_can_wait_until_commit
  t = Thread.new {
    @response.await_commit
  }
  @response.commit!
  assert @response.committed?
  assert t.join(0.5)
end
test_each_isnt_called_if_str_body_is_written() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 42
def test_each_isnt_called_if_str_body_is_written
  # Controller writes and reads response body
  each_counter = 0
  @response.body = Object.new.tap { |o| o.singleton_class.send(:define_method, :each) { |&block| each_counter += 1; block.call "foo" } }
  @response["X-Foo"] = @response.body

  assert_equal 1, each_counter, "#each was not called once"

  # Build response
  status, headers, body = @response.to_a

  assert_equal 200, status
  assert_equal "foo", headers["X-Foo"]
  assert_equal "foo", body.each.to_a.join

  # Show that #each was not called twice
  assert_equal 1, each_counter, "#each was not called once"
end
test_empty_content_type_returns_nil() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 115
def test_empty_content_type_returns_nil
  @response.headers["Content-Type"] = ""
  assert_nil @response.content_type
end
test_only_set_charset_still_defaults_to_text_html() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 138
def test_only_set_charset_still_defaults_to_text_html
  response = ActionDispatch::Response.new
  response.charset = "utf-16"
  _, headers, _ = response.to_a
  assert_equal "text/html; charset=utf-16", headers["Content-Type"]
end
test_read_body_during_action() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 75
def test_read_body_during_action
  @response.body = "Hello, World!"

  # even though there's no explicitly set content-type,
  assert_nil @response.content_type

  # after the action reads back @response.body,
  assert_equal "Hello, World!", @response.body

  # the response can be built.
  status, headers, body = @response.to_a
  assert_equal 200, status
  assert_equal({
    "Content-Type" => "text/html; charset=utf-8"
  }, headers)

  parts = []
  body.each { |part| parts << part }
  assert_equal ["Hello, World!"], parts
end
test_response_body_encoding() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 96
def test_response_body_encoding
  body = ["hello".encode(Encoding::UTF_8)]
  response = ActionDispatch::Response.new 200, {}, body
  response.request = ActionDispatch::Request.empty
  assert_equal Encoding::UTF_8, response.body.encoding
end
test_response_charset_writer() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 103
def test_response_charset_writer
  @response.charset = "utf-16"
  assert_equal "utf-16", @response.charset
  @response.charset = nil
  assert_equal "utf-8", @response.charset
end
test_set_header_after_read_body_during_action() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 61
def test_set_header_after_read_body_during_action
  @response.body

  # set header after the action reads back @response.body
  @response["x-header"] = "Best of all possible worlds."

  # the response can be built.
  status, headers, body = @response.to_a
  assert_equal 200, status
  assert_equal "", body.body

  assert_equal "Best of all possible worlds.", headers["x-header"]
end
test_setting_content_type_header_impacts_content_type_method() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 110
def test_setting_content_type_header_impacts_content_type_method
  @response.headers["Content-Type"] = "application/aaron"
  assert_equal "application/aaron", @response.content_type
end
test_stream_close() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 22
def test_stream_close
  @response.stream.close
  assert @response.stream.closed?
end
test_stream_write() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 27
def test_stream_write
  @response.stream.write "foo"
  @response.stream.close
  assert_equal "foo", @response.body
end
test_write_after_close() click to toggle source
# File actionpack/test/dispatch/response_test.rb, line 33
def test_write_after_close
  @response.stream.close

  e = assert_raises(IOError) do
    @response.stream.write "omg"
  end
  assert_equal "closed stream", e.message
end