@@ -45,44 +45,78 @@ def explicit_cache
45
45
46
46
class StreamingTest < Rack ::TestCase
47
47
test "rendering with streaming enabled at the class level" do
48
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/hello_world" )
49
+ status , headers , body = app . call ( env )
50
+ assert_streaming! ( status , headers , body )
51
+ assert_chunks [ "Hello world" , ", I'm here!" ] , body
52
+
48
53
get "/render_streaming/basic/hello_world"
49
54
assert_body "Hello world, I'm here!"
50
- assert_streaming!
51
55
end
52
56
53
57
test "rendering with streaming given to render" do
58
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/explicit" )
59
+ status , headers , body = app . call ( env )
60
+ assert_streaming! ( status , headers , body )
61
+ assert_chunks [ "Hello world" , ", I'm here!" ] , body
62
+
54
63
get "/render_streaming/basic/explicit"
55
64
assert_body "Hello world, I'm here!"
56
- assert_streaming !
65
+ assert_cache_control !
57
66
end
58
67
59
68
test "rendering with streaming do not override explicit cache control given to render" do
69
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/explicit_cache" )
70
+ status , headers , body = app . call ( env )
71
+ assert_streaming! ( status , headers , body )
72
+ assert_chunks [ "Hello world" , ", I'm here!" ] , body
73
+
60
74
get "/render_streaming/basic/explicit_cache"
61
75
assert_body "Hello world, I'm here!"
62
- assert_streaming ! "private"
76
+ assert_cache_control ! "private"
63
77
end
64
78
65
79
test "rendering with streaming no layout" do
80
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/no_layout" )
81
+ status , headers , body = app . call ( env )
82
+ assert_streaming! ( status , headers , body )
83
+ assert_chunks [ "Hello world" ] , body
84
+
66
85
get "/render_streaming/basic/no_layout"
67
86
assert_body "Hello world"
68
- assert_streaming !
87
+ assert_cache_control !
69
88
end
70
89
71
90
test "skip rendering with streaming at render level" do
91
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/skip" )
92
+ status , _ , body = app . call ( env )
93
+ assert_equal 200 , status
94
+ assert_chunks [ "Hello world, I'm here!" ] , body
95
+
72
96
get "/render_streaming/basic/skip"
73
97
assert_body "Hello world, I'm here!"
74
98
end
75
99
76
100
test "rendering with layout exception" do
101
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/layout_exception" )
102
+ status , headers , body = app . call ( env )
103
+ assert_streaming! ( status , headers , body )
104
+ assert_chunks [ "<body class=\" " , "\" ><script>window.location = \" /500.html\" </script></html>" ] , body
105
+
77
106
get "/render_streaming/basic/layout_exception"
78
107
assert_body "<body class=\" \" ><script>window.location = \" /500.html\" </script></html>"
79
- assert_streaming !
108
+ assert_cache_control !
80
109
end
81
110
82
111
test "rendering with template exception" do
112
+ env = Rack ::MockRequest . env_for ( "/render_streaming/basic/template_exception" )
113
+ status , headers , body = app . call ( env )
114
+ assert_streaming! ( status , headers , body )
115
+ assert_chunks [ "\" ><script>window.location = \" /500.html\" </script></html>" ] , body
116
+
83
117
get "/render_streaming/basic/template_exception"
84
118
assert_body "\" ><script>window.location = \" /500.html\" </script></html>"
85
- assert_streaming !
119
+ assert_cache_control !
86
120
end
87
121
88
122
test "rendering with template exception logs the exception" do
@@ -98,9 +132,28 @@ class StreamingTest < Rack::TestCase
98
132
end
99
133
end
100
134
101
- def assert_streaming! ( cache = "no-cache" )
102
- assert_status 200
103
- assert_equal cache , headers [ "cache-control" ]
135
+ def assert_streaming! ( status , headers , body )
136
+ assert_equal 200 , status
137
+
138
+ # It should not have a content length
139
+ assert_nil headers [ "content-length" ]
140
+
141
+ # The body should not respond to `#to_ary`
142
+ assert_not_respond_to body , :to_ary
143
+ end
144
+
145
+ def assert_cache_control! ( value = "no-cache" , headers : self . headers )
146
+ assert_equal value , headers [ "cache-control" ]
147
+ end
148
+
149
+ def assert_chunks ( expected , body )
150
+ index = 0
151
+ body . each do |chunk |
152
+ assert_equal expected [ index ] , chunk
153
+ index += 1
154
+ end
155
+
156
+ assert_equal expected . size , index
104
157
end
105
158
end
106
159
end
0 commit comments