aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/zero/response.rb9
-rw-r--r--spec/unit/response/response_spec.rb28
2 files changed, 35 insertions, 2 deletions
diff --git a/lib/zero/response.rb b/lib/zero/response.rb
index 74b2e28..b8aeff0 100644
--- a/lib/zero/response.rb
+++ b/lib/zero/response.rb
@@ -35,7 +35,14 @@ module Zero
# TODO Set content length, if not already set
# TODO Set content type, if not already set
- [@status, @header, @body]
+ [status, header, body]
+ end
+
+ # Sets the content length header to the current length of the body
+ # Also creates one, if it does not exists
+ #
+ def content_length
+ header['Content-Length'] = body.join.bytesize
end
end
diff --git a/spec/unit/response/response_spec.rb b/spec/unit/response/response_spec.rb
index c314567..e58b0d0 100644
--- a/spec/unit/response/response_spec.rb
+++ b/spec/unit/response/response_spec.rb
@@ -1,9 +1,10 @@
+# encoding: UTF-8
require 'spec_helper'
describe Zero::Response do
subject { Zero::Response.new() }
- describe '#finish' do
+ describe '#to_a' do
it "returns an array within status header and body" do
subject.status = 200
subject.header = {}
@@ -16,6 +17,9 @@ describe Zero::Response do
value[1].should eq({}) # Headers
value[2].should eq([]) # Body
end
+
+ it "returns the content length in the header" do
+ end
end
describe '#status' do
@@ -44,4 +48,26 @@ describe Zero::Response do
end
end
+ describe '#content_length' do
+ it "sets the content_length to 0, if there is no content" do
+ subject.content_length
+
+ subject.header['Content-Length'].should eq(0)
+ end
+
+ it "sets the content_length to the size of the message body" do
+ subject.body = ['foo', 'bar']
+ subject.content_length
+
+ subject.header['Content-Length'].should eq(6)
+ end
+
+ it "sets the content_length to the bytesize of the message body" do
+ subject.body = ['föö', 'bär']
+ subject.content_length
+
+ subject.header['Content-Length'].should eq(9)
+ end
+ end
+
end \ No newline at end of file