File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
test/rack/conform/streaming Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 5
5
6
6
require_relative 'conform/version'
7
7
require_relative 'conform/application'
8
+
9
+ require_relative 'conform/chunked'
Original file line number Diff line number Diff line change
1
+
2
+ module Rack
3
+ module Conform
4
+ module Chunked
5
+ class Body # :nodoc:
6
+ TERM = "\r \n "
7
+ TAIL = "0#{ TERM } "
8
+
9
+ # Store the response body to be chunked.
10
+ def initialize ( body )
11
+ @body = body
12
+ end
13
+
14
+ # For each element yielded by the response body, yield
15
+ # the element in chunked encoding.
16
+ def each ( &block )
17
+ term = TERM
18
+ @body . each do |chunk |
19
+ size = chunk . bytesize
20
+ next if size == 0
21
+
22
+ yield [ size . to_s ( 16 ) , term , chunk . b , term ] . join
23
+ end
24
+ yield TAIL
25
+ yield term
26
+ end
27
+
28
+ # Close the response body if the response body supports it.
29
+ def close
30
+ @body . close if @body . respond_to? ( :close )
31
+ end
32
+ end
33
+ end
34
+
35
+ class Application
36
+ def test_chunked_body ( env )
37
+ [ 200 , { 'transfer-encoding' => 'chunked' } , Chunked ::Body . new ( env [ 'rack.input' ] ) ]
38
+ end
39
+ end
40
+ end
41
+ end
Original file line number Diff line number Diff line change 18
18
response &.finish
19
19
end
20
20
end
21
+
22
+ it 'can stream a chunked response' do
23
+ body = Protocol ::HTTP ::Body ::Buffered . new ( [
24
+ "Hello " ,
25
+ "World!"
26
+ ] )
27
+
28
+ response = client . post ( "/chunked/body" , { } , body )
29
+
30
+ expect ( response . status ) . to be == 200
31
+ expect ( response . read ) . to be == "Hello World"
32
+ ensure
33
+ response &.finish
34
+ end
You can’t perform that action at this time.
0 commit comments