Skip to content

Commit 019f03e

Browse files
committed
Add tests.
1 parent 9631fde commit 019f03e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/protocol/rack/body.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
require_relative "body/streaming"
77
require_relative "body/enumerable"
88
require_relative "constants"
9+
910
require "protocol/http/body/completable"
11+
require "protocol/http/body/head"
1012

1113
module Protocol
1214
module Rack
@@ -85,12 +87,17 @@ def self.wrap(env, status, headers, body, input = nil, head = false)
8587
end
8688
end
8789

90+
# There are two main situations we need to handle:
91+
# 1. The application has the `Rack::Head` middleware in the stack, which means we should not return a body, and the application is also responsible for setting the content-length header. `Rack::Head` will result in an empty enumerable body.
92+
# 2. The application does not have `Rack::Head`, in which case it will return a body and we need to extract the length.
93+
# In both cases, we need to ensure that the body is wrapped correctly. If there is no body and we don't know the length, we also just return `nil`.
8894
if head
8995
if body
9096
body = ::Protocol::HTTP::Body::Head.for(body)
9197
elsif length
9298
body = ::Protocol::HTTP::Body::Head.new(length)
9399
end
100+
# Otherwise, body is `nil` and we don't know the length either.
94101
end
95102

96103
return body

test/protocol/rack/body.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
end
1919

2020
with "#wrap" do
21-
let(:env) { {} }
22-
let(:headers) { {} }
21+
let(:env) {Hash.new}
22+
let(:headers) {Hash.new}
2323

2424
it "handles nil body" do
2525
expect(Console).to receive(:warn).and_return(nil)
@@ -28,6 +28,42 @@
2828
expect(result).to be_nil
2929
end
3030

31+
with "head request" do
32+
it "handles head request with content-length and empty body" do
33+
headers["content-length"] = "123"
34+
35+
result = subject.wrap(env, 200, headers, [], nil, true)
36+
37+
expect(result).to be_a(Protocol::HTTP::Body::Head)
38+
expect(result.length).to be == 123
39+
end
40+
41+
it "handles head request with no content-length and empty body" do
42+
result = subject.wrap(env, 200, headers, [], nil, true)
43+
44+
expect(result).to be_a(Protocol::HTTP::Body::Head)
45+
expect(result.length).to be == 0
46+
end
47+
48+
it "handles head request with content-length and nil body" do
49+
headers["content-length"] = "123"
50+
51+
expect(Console).to receive(:warn).and_return(nil)
52+
result = subject.wrap(env, 200, headers, nil, nil, true)
53+
54+
expect(result).to be_a(Protocol::HTTP::Body::Head)
55+
expect(result.length).to be == 123
56+
end
57+
58+
it "handles head request with no content-length and nil body" do
59+
expect(Console).to receive(:warn).and_return(nil)
60+
61+
result = subject.wrap(env, 200, headers, nil, nil, true)
62+
63+
expect(result).to be_nil
64+
end
65+
end
66+
3167
with "non-empty body and no-content status" do
3268
let(:mock_body) do
3369
Protocol::HTTP::Body::Buffered.new(["content"])

0 commit comments

Comments
 (0)