aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/new_base/render_streaming_test.rb
blob: ffc4b331ec8fa6649cc60550473e52eaa00dc45c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'abstract_unit'

module RenderStreaming
  class BasicController < ActionController::Base
    self.view_paths = [ActionView::FixtureResolver.new(
      "render_streaming/basic/hello_world.html.erb" => "Hello world",
      "layouts/application.html.erb" => "<%= yield %>, I'm here!"
    )]

    layout "application"
    stream :only => [:hello_world, :skip]

    def hello_world
    end

    def skip
      render :action => "hello_world", :stream => false
    end

    def explicit
      render :action => "hello_world", :stream => true
    end

    def no_layout
      render :action => "hello_world", :stream => true, :layout => false
    end

    def explicit_cache
      headers["Cache-Control"] = "private"
      render :action => "hello_world", :stream => true
    end
  end

  class StreamingTest < Rack::TestCase
    test "rendering with streaming enabled at the class level" do
      get "/render_streaming/basic/hello_world"
      assert_body "b\r\nHello world\r\nb\r\n, I'm here!\r\n0\r\n\r\n"
      assert_streaming!
    end

    test "rendering with streaming given to render" do
      get "/render_streaming/basic/explicit"
      assert_body "b\r\nHello world\r\nb\r\n, I'm here!\r\n0\r\n\r\n"
      assert_streaming!
    end

    test "rendering with streaming do not override explicit cache control given to render" do
      get "/render_streaming/basic/explicit_cache"
      assert_body "b\r\nHello world\r\nb\r\n, I'm here!\r\n0\r\n\r\n"
      assert_streaming! "private"
    end

    test "rendering with streaming no layout" do
      get "/render_streaming/basic/no_layout"
      assert_body "b\r\nHello world\r\n0\r\n\r\n"
      assert_streaming!
    end

    test "skip rendering with streaming at render level" do
      get "/render_streaming/basic/skip"
      assert_body "Hello world, I'm here!"
    end

    def assert_streaming!(cache="no-cache")
      assert_status 200
      assert_equal nil, headers["Content-Length"]
      assert_equal "chunked", headers["Transfer-Encoding"]
      assert_equal cache, headers["Cache-Control"]
    end
  end
end if defined?(Fiber)