Skip to content

Commit 52eccc0

Browse files
committed
Add tests.
1 parent 9631fde commit 52eccc0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/protocol/rack/body.rb

Lines changed: 2 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

test/protocol/rack/body.rb

Lines changed: 39 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,43 @@
2828
expect(result).to be_nil
2929
end
3030

31+
it "handles head request with content-length and empty body" do
32+
# Rack::Head will result in an enumerable body, and currently does not set content-length either.
33+
# So, it's up to the application to set the content-length header if they use `Rack::Head`.
34+
# Otherwise, we should basically ignore the body but retain the length if known/provided.
35+
headers["content-length"] = "123"
36+
37+
result = subject.wrap(env, 200, headers, [], nil, true)
38+
39+
expect(result).to be_a(Protocol::HTTP::Body::Head)
40+
expect(result.length).to be == 123
41+
end
42+
43+
it "handles head request with no content-length and empty body" do
44+
result = subject.wrap(env, 200, headers, [], nil, true)
45+
46+
expect(result).to be_a(Protocol::HTTP::Body::Head)
47+
expect(result.length).to be == 0
48+
end
49+
50+
it "handles head request with content-length and nil body" do
51+
headers["content-length"] = "123"
52+
53+
expect(Console).to receive(:warn).and_return(nil)
54+
result = subject.wrap(env, 200, headers, nil, nil, true)
55+
56+
expect(result).to be_a(Protocol::HTTP::Body::Head)
57+
expect(result.length).to be == 123
58+
end
59+
60+
it "handles head request with no content-length and nil body" do
61+
expect(Console).to receive(:warn).and_return(nil)
62+
63+
result = subject.wrap(env, 200, headers, nil, nil, true)
64+
65+
expect(result).to be_nil
66+
end
67+
3168
with "non-empty body and no-content status" do
3269
let(:mock_body) do
3370
Protocol::HTTP::Body::Buffered.new(["content"])

0 commit comments

Comments
 (0)