|
4 | 4 | # Copyright, 2018-2023, by Samuel Williams.
|
5 | 5 |
|
6 | 6 | require 'protocol/http/body/writable'
|
7 |
| -require 'protocol/http/body/a_writable_body' |
| 7 | +require 'protocol/http/body/deflate' |
8 | 8 |
|
9 | 9 | describe Protocol::HTTP::Body::Writable do
|
10 | 10 | let(:body) {subject.new}
|
11 | 11 |
|
12 |
| - it_behaves_like Protocol::HTTP::Body::AWritableBody |
13 |
| - |
14 | 12 | with "#length" do
|
15 | 13 | it "should be unspecified by default" do
|
16 | 14 | expect(body.length).to be_nil
|
|
79 | 77 | body.write("Hello")
|
80 | 78 | end.to raise_exception(Protocol::HTTP::Body::Writable::Closed)
|
81 | 79 | end
|
| 80 | + |
| 81 | + it "can write and read data" do |
| 82 | + 3.times do |i| |
| 83 | + body.write("Hello World #{i}") |
| 84 | + expect(body.read).to be == "Hello World #{i}" |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + it "can buffer data in order" do |
| 89 | + 3.times do |i| |
| 90 | + body.write("Hello World #{i}") |
| 91 | + end |
| 92 | + |
| 93 | + 3.times do |i| |
| 94 | + expect(body.read).to be == "Hello World #{i}" |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + with '#join' do |
| 100 | + it "can join chunks" do |
| 101 | + 3.times do |i| |
| 102 | + body.write("#{i}") |
| 103 | + end |
| 104 | + |
| 105 | + body.close |
| 106 | + |
| 107 | + expect(body.join).to be == "012" |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + with '#each' do |
| 112 | + it "can read all data in order" do |
| 113 | + 3.times do |i| |
| 114 | + body.write("Hello World #{i}") |
| 115 | + end |
| 116 | + |
| 117 | + body.close |
| 118 | + |
| 119 | + 3.times do |i| |
| 120 | + chunk = body.read |
| 121 | + expect(chunk).to be == "Hello World #{i}" |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + it "can propagate failures" do |
| 126 | + body.write("Beep boop") # This will cause a failure. |
| 127 | + |
| 128 | + expect do |
| 129 | + body.each do |chunk| |
| 130 | + raise RuntimeError.new("It was too big!") |
| 131 | + end |
| 132 | + end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 133 | + |
| 134 | + expect do |
| 135 | + body.write("Beep boop") # This will fail. |
| 136 | + end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 137 | + end |
| 138 | + |
| 139 | + it "can propagate failures in nested bodies" do |
| 140 | + nested = ::Protocol::HTTP::Body::Deflate.for(body) |
| 141 | + |
| 142 | + body.write("Beep boop") # This will cause a failure. |
| 143 | + |
| 144 | + expect do |
| 145 | + nested.each do |chunk| |
| 146 | + raise RuntimeError.new("It was too big!") |
| 147 | + end |
| 148 | + end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 149 | + |
| 150 | + expect do |
| 151 | + body.write("Beep boop") # This will fail. |
| 152 | + end.to raise_exception(RuntimeError, message: be =~ /big/) |
| 153 | + end |
| 154 | + |
| 155 | + it "will stop after finishing" do |
| 156 | + body.write("Hello World!") |
| 157 | + body.close |
| 158 | + |
| 159 | + expect(body).not.to be(:empty?) |
| 160 | + |
| 161 | + body.each do |chunk| |
| 162 | + expect(chunk).to be == "Hello World!" |
| 163 | + end |
| 164 | + |
| 165 | + expect(body).to be(:empty?) |
| 166 | + end |
82 | 167 | end
|
83 | 168 | end
|
0 commit comments