|
10 | 10 | AnEchoServer = Sus::Shared("an echo server") do
|
11 | 11 | let(:app) do
|
12 | 12 | ::Protocol::HTTP::Middleware.for do |request|
|
13 |
| - body = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
14 |
| - # $stderr.puts "Server stream: #{stream.inspect}" |
15 |
| - |
| 13 | + streamable = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
| 14 | + Console.debug(self, "Echoing chunks...") |
16 | 15 | while chunk = stream.readpartial(1024)
|
17 |
| - # $stderr.puts "Server reading chunk: #{chunk.inspect}" |
| 16 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
18 | 17 | stream.write(chunk)
|
19 | 18 | end
|
20 | 19 | rescue EOFError
|
| 20 | + Console.debug(self, "EOF.") |
21 | 21 | # Ignore.
|
22 | 22 | ensure
|
23 |
| - # $stderr.puts "Server closing stream." |
| 23 | + Console.debug(self, "Closing stream.") |
24 | 24 | stream.close
|
25 | 25 | end
|
26 | 26 |
|
27 |
| - ::Protocol::HTTP::Response[200, {}, body] |
| 27 | + ::Protocol::HTTP::Response[200, {}, streamable] |
28 | 28 | end
|
29 | 29 | end
|
30 | 30 |
|
31 | 31 | it "should echo the request body" do
|
32 | 32 | chunks = ["Hello,", "World!"]
|
33 | 33 | response_chunks = Queue.new
|
34 | 34 |
|
35 |
| - body = ::Protocol::HTTP::Body::Streamable.request do |stream| |
36 |
| - # $stderr.puts "Client stream: #{stream.inspect}" |
37 |
| - |
| 35 | + output = ::Protocol::HTTP::Body::Writable.new |
| 36 | + response = client.post("/", body: output) |
| 37 | + stream = ::Protocol::HTTP::Body::Stream.new(response.body, output) |
| 38 | + |
| 39 | + begin |
| 40 | + Console.debug(self, "Echoing chunks...") |
38 | 41 | chunks.each do |chunk|
|
39 |
| - # $stderr.puts "Client writing chunk: #{chunk.inspect}" |
| 42 | + Console.debug(self, "Writing chunk:", chunk: chunk) |
40 | 43 | stream.write(chunk)
|
41 | 44 | end
|
42 | 45 |
|
43 |
| - # $stderr.puts "Client closing write." |
| 46 | + Console.debug(self, "Closing write.") |
44 | 47 | stream.close_write
|
45 | 48 |
|
46 |
| - # $stderr.puts "Client reading chunks..." |
| 49 | + Console.debug(self, "Reading chunks...") |
47 | 50 | while chunk = stream.readpartial(1024)
|
48 |
| - # $stderr.puts "Client reading chunk: #{chunk.inspect}" |
| 51 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
49 | 52 | response_chunks << chunk
|
50 | 53 | end
|
51 | 54 | rescue EOFError
|
| 55 | + Console.debug(self, "EOF.") |
52 | 56 | # Ignore.
|
53 | 57 | ensure
|
54 |
| - # $stderr.puts "Client closing stream." |
| 58 | + Console.debug(self, "Closing stream.") |
55 | 59 | stream.close
|
56 | 60 | response_chunks.close
|
57 | 61 | end
|
58 | 62 |
|
59 |
| - response = client.post("/", body: body) |
60 |
| - body.stream(response.body) |
61 |
| - |
62 | 63 | chunks.each do |chunk|
|
63 | 64 | expect(response_chunks.pop).to be == chunk
|
64 | 65 | end
|
|
71 | 72 |
|
72 | 73 | let(:app) do
|
73 | 74 | ::Protocol::HTTP::Middleware.for do |request|
|
74 |
| - body = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
| 75 | + streamable = ::Protocol::HTTP::Body::Streamable.response(request) do |stream| |
| 76 | + Console.debug(self, "Echoing chunks...") |
75 | 77 | chunks.each do |chunk|
|
76 | 78 | stream.write(chunk)
|
77 | 79 | end
|
78 | 80 |
|
| 81 | + Console.debug(self, "Closing write.") |
79 | 82 | stream.close_write
|
80 | 83 |
|
| 84 | + Console.debug(self, "Reading chunks...") |
81 | 85 | while chunk = stream.readpartial(1024)
|
| 86 | + Console.debug(self, "Reading chunk:", chunk: chunk) |
82 | 87 | response_chunks << chunk
|
83 | 88 | end
|
84 | 89 | rescue EOFError
|
| 90 | + Console.debug(self, "EOF.") |
85 | 91 | # Ignore.
|
86 | 92 | ensure
|
87 |
| - # $stderr.puts "Server closing stream." |
| 93 | + Console.debug(self, "Closing stream.") |
88 | 94 | stream.close
|
89 | 95 | end
|
90 | 96 |
|
91 |
| - ::Protocol::HTTP::Response[200, {}, body] |
| 97 | + ::Protocol::HTTP::Response[200, {}, streamable] |
92 | 98 | end
|
93 | 99 | end
|
94 | 100 |
|
95 | 101 | it "should echo the response body" do
|
96 |
| - body = ::Protocol::HTTP::Body::Streamable.request do |stream| |
| 102 | + output = ::Protocol::HTTP::Body::Writable.new |
| 103 | + response = client.post("/", body: output) |
| 104 | + stream = ::Protocol::HTTP::Body::Stream.new(response.body, output) |
| 105 | + |
| 106 | + begin |
| 107 | + Console.debug(self, "Echoing chunks...") |
97 | 108 | while chunk = stream.readpartial(1024)
|
98 | 109 | stream.write(chunk)
|
99 | 110 | end
|
100 | 111 | rescue EOFError
|
| 112 | + Console.debug(self, "EOF.") |
101 | 113 | # Ignore.
|
102 | 114 | ensure
|
| 115 | + Console.debug(self, "Closing stream.") |
103 | 116 | stream.close
|
104 | 117 | end
|
105 | 118 |
|
106 |
| - response = client.post("/", body: body) |
107 |
| - body.stream(response.body) |
108 |
| - |
109 | 119 | chunks.each do |chunk|
|
110 | 120 | expect(response_chunks.pop).to be == chunk
|
111 | 121 | end
|
112 | 122 | end
|
113 | 123 | end
|
114 | 124 |
|
115 |
| -[Async::HTTP::Protocol::HTTP1].each do |protocol| |
116 |
| - describe protocol do |
| 125 | +[Async::HTTP::Protocol::HTTP1, Async::HTTP::Protocol::HTTP2].each do |protocol| |
| 126 | + describe protocol, unique: protocol.name do |
117 | 127 | include Sus::Fixtures::Async::HTTP::ServerContext
|
118 | 128 |
|
119 | 129 | let(:protocol) {subject}
|
120 | 130 |
|
121 | 131 | it_behaves_like AnEchoServer
|
122 |
| - # it_behaves_like AnEchoClient |
| 132 | + it_behaves_like AnEchoClient |
123 | 133 | end
|
124 | 134 | end
|
0 commit comments