/
body_spec.rb
205 lines (160 loc) · 4.56 KB
/
body_spec.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# frozen_string_literal: true
RSpec.describe HTTP::Request::Body do
let(:body) { "" }
subject { HTTP::Request::Body.new(body) }
describe "#initialize" do
context "when body is nil" do
let(:body) { nil }
it "does not raise an error" do
expect { subject }.not_to raise_error
end
end
context "when body is a string" do
let(:body) { "string body" }
it "does not raise an error" do
expect { subject }.not_to raise_error
end
end
context "when body is an IO" do
let(:body) { FakeIO.new("IO body") }
it "does not raise an error" do
expect { subject }.not_to raise_error
end
end
context "when body is an Enumerable" do
let(:body) { %w[bees cows] }
it "does not raise an error" do
expect { subject }.not_to raise_error
end
end
context "when body is of unrecognized type" do
let(:body) { 123 }
it "raises an error" do
expect { subject }.to raise_error(HTTP::RequestError)
end
end
end
describe "#source" do
it "returns the original object" do
expect(subject.source).to eq ""
end
end
describe "#size" do
context "when body is nil" do
let(:body) { nil }
it "returns zero" do
expect(subject.size).to eq 0
end
end
context "when body is a string" do
let(:body) { "Привет, мир!" }
it "returns string bytesize" do
expect(subject.size).to eq 21
end
end
context "when body is an IO with size" do
let(:body) { FakeIO.new("content") }
it "returns IO size" do
expect(subject.size).to eq 7
end
end
context "when body is an IO without size" do
let(:body) { IO.pipe[0] }
it "raises a RequestError" do
expect { subject.size }.to raise_error(HTTP::RequestError)
end
end
context "when body is an Enumerable" do
let(:body) { %w[bees cows] }
it "raises a RequestError" do
expect { subject.size }.to raise_error(HTTP::RequestError)
end
end
end
describe "#each" do
let(:chunks) do
chunks = []
subject.each { |chunk| chunks << chunk.dup }
chunks
end
context "when body is nil" do
let(:body) { nil }
it "yields nothing" do
expect(chunks).to eq []
end
end
context "when body is a string" do
let(:body) { "content" }
it "yields the string" do
expect(chunks).to eq %w[content]
end
end
context "when body is a non-Enumerable IO" do
let(:body) { FakeIO.new("a" * 16 * 1024 + "b" * 10 * 1024) }
it "yields chunks of content" do
expect(chunks.inject("", :+)).to eq "a" * 16 * 1024 + "b" * 10 * 1024
end
end
context "when body is a pipe" do
let(:ios) { IO.pipe }
let(:body) { ios[0] }
before do
Thread.new(ios[1]) do |io|
16_384.times { io << "abcdef" }
io.close
end
end
it "yields chunks of content" do
expect(chunks.inject("", :+)).to eq("abcdef" * 16_384)
end
end
context "when body is an Enumerable IO" do
let(:data) { "a" * 16 * 1024 + "b" * 10 * 1024 }
let(:body) { StringIO.new data }
it "yields chunks of content" do
expect(chunks.inject("", :+)).to eq data
end
it "allows to enumerate multiple times" do
results = []
2.times do
result = ""
subject.each { |chunk| result += chunk }
results << result
end
aggregate_failures do
expect(results.count).to eq 2
expect(results).to all eq data
end
end
end
context "when body is an Enumerable" do
let(:body) { %w[bees cows] }
it "yields elements" do
expect(chunks).to eq %w[bees cows]
end
end
end
describe "#==" do
context "when sources are equivalent" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { HTTP::Request::Body.new("content") }
it "returns true" do
expect(body1).to eq body2
end
end
context "when sources are not equivalent" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { HTTP::Request::Body.new(nil) }
it "returns false" do
expect(body1).not_to eq body2
end
end
context "when objects are not of the same class" do
let(:body1) { HTTP::Request::Body.new("content") }
let(:body2) { "content" }
it "returns false" do
expect(body1).not_to eq body2
end
end
end
end