From 6d2b3acf564c5378de1cac80c9cd283038cf74cf Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 17 Jun 2025 21:25:53 +0900 Subject: [PATCH] `Head.for(nil)` -> `nil`. --- lib/protocol/http/body/head.rb | 13 +++++++++---- test/protocol/http/body/head.rb | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/protocol/http/body/head.rb b/lib/protocol/http/body/head.rb index 0e89ee3..a6f0d40 100644 --- a/lib/protocol/http/body/head.rb +++ b/lib/protocol/http/body/head.rb @@ -12,12 +12,17 @@ module Body # Represents a body suitable for HEAD requests, in other words, a body that is empty and has a known length. class Head < Readable # Create a head body for the given body, capturing its length and then closing it. + # + # @parameter body [Readable | Nil] the body to create a head for. + # @returns [Head | Nil] the head body, or nil if the body is nil. def self.for(body) - head = self.new(body.length) - - body.close + if body + head = self.new(body.length) + body.close + return head + end - return head + return nil end # Initialize the head body with the given length. diff --git a/test/protocol/http/body/head.rb b/test/protocol/http/body/head.rb index 7aee400..58acc99 100644 --- a/test/protocol/http/body/head.rb +++ b/test/protocol/http/body/head.rb @@ -56,4 +56,11 @@ body.close end end + + with ".for with nil body" do + it "returns nil when body is nil" do + body = subject.for(nil) + expect(body).to be_nil + end + end end