Skip to content

Commit 0d7b6d5

Browse files
committed
Add request body rewind preparation
1 parent e0143db commit 0d7b6d5

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎lib/protocol/http/request.rb‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,9 @@ def idempotent?
153153
return false
154154
end
155155

156-
# Prepare the request body to be sent again, if it is safe to retry.
157-
# @returns [Boolean] Whether the request was prepared for retry.
158-
def retry!
159-
# Only idempotent request methods can be retried safely.
160-
if @method == Methods::POST || @method == Methods::PATCH || @method == Methods::CONNECT
161-
return false
162-
end
163-
156+
# Rewind the request body so it can be sent again.
157+
# @returns [Boolean] Whether the request body was rewound.
158+
def rewind!
164159
# Requests without a body can be sent again immediately.
165160
if body = @body
166161
# Empty, non-rewindable bodies have nothing left to send.
@@ -181,6 +176,17 @@ def retry!
181176
return true
182177
end
183178

179+
# Prepare the request body to be sent again, if it is safe to retry.
180+
# @returns [Boolean] Whether the request was prepared for retry.
181+
def retry!
182+
# Only idempotent request methods can be retried safely.
183+
if @method == Methods::POST || @method == Methods::PATCH || @method == Methods::CONNECT
184+
return false
185+
end
186+
187+
return self.rewind!
188+
end
189+
184190
# Convert the request to a hash, suitable for serialization.
185191
#
186192
# @returns [Hash] The request as a hash.

‎test/protocol/http/request.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@
160160
end
161161
end
162162

163+
with "#rewind!" do
164+
it "allows requests without a body" do
165+
expect(request.rewind!).to be == true
166+
end
167+
168+
with "request with a rewindable body" do
169+
let(:request) {subject["POST", "/submit", body: "content"]}
170+
171+
it "rewinds the body" do
172+
expect(request.body.read).to be == "content"
173+
174+
expect(request.rewind!).to be == true
175+
expect(request.body.read).to be == "content"
176+
end
177+
end
178+
179+
with "request with a non-rewindable body" do
180+
let(:body) {Protocol::HTTP::Body::Readable.new}
181+
let(:request) {subject.new(nil, nil, "PUT", "/resource", nil, headers, body)}
182+
183+
it "does not rewind" do
184+
expect(request.rewind!).to be == false
185+
end
186+
end
187+
end
188+
163189
with "#retry!" do
164190
it "allows idempotent requests without a body" do
165191
expect(request.retry!).to be == true

0 commit comments

Comments
 (0)