Skip to content

Commit

Permalink
Set Content-Length request header when request body is nil
Browse files Browse the repository at this point in the history
When the body is a String, the Content-Length header was already set.
  • Loading branch information
Jip van Reijsen authored and ixti committed Aug 1, 2016
1 parent 0ead296 commit 39e9932
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/http/request/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def connect_through_proxy
def add_body_type_headers
if @body.is_a?(String) && !@headers[Headers::CONTENT_LENGTH]
@request_header << "#{Headers::CONTENT_LENGTH}: #{@body.bytesize}"
elsif @body.nil? && !@headers[Headers::CONTENT_LENGTH]
@request_header << "#{Headers::CONTENT_LENGTH}: 0"
elsif @body.is_a?(Enumerable) && CHUNKED != @headers[Headers::TRANSFER_ENCODING]
raise(RequestError, "invalid transfer encoding")
end
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/http/request/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@
end
end

context "when body is nil" do
let(:body) { nil }

it "properly sets Content-Length header if needed" do
writer.stream
expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 0\r\n\r\n"
end

context "when Content-Length explicitly set" do
let(:headers) { HTTP::Headers.coerce "Content-Length" => 12 }

it "keeps given value" do
writer.stream
expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 12\r\n\r\n"
end
end
end

context "when body is a unicode String" do
let(:body) { "Привет, мир!" }

Expand Down

0 comments on commit 39e9932

Please sign in to comment.