2012-11-23 18:06:53 +01:00
|
|
|
# encoding: UTF-8
|
2012-11-16 17:56:34 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
describe Zero::Response do
|
2012-11-16 17:56:34 +01:00
|
|
|
subject { Zero::Response.new() }
|
|
|
|
|
2012-11-19 19:39:45 +01:00
|
|
|
describe '#to_a' do
|
2012-11-18 16:38:38 +01:00
|
|
|
it "returns an array within status header and body" do
|
|
|
|
subject.status = 200
|
|
|
|
subject.header = {}
|
|
|
|
subject.body = []
|
|
|
|
|
|
|
|
value = subject.to_a
|
2012-11-17 14:35:15 +01:00
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
value.should be_an_instance_of(Array)
|
|
|
|
value[0].should eq(200) # Status code
|
2012-11-23 20:05:58 +01:00
|
|
|
value[1].should be_an_instance_of(Hash) # Headers
|
2012-11-18 16:38:38 +01:00
|
|
|
value[2].should eq([]) # Body
|
|
|
|
end
|
2012-11-23 17:43:39 +01:00
|
|
|
|
|
|
|
it "returns the content length in the header" do
|
2012-11-23 19:39:56 +01:00
|
|
|
subject.body = ['foobar']
|
2012-11-23 19:35:14 +01:00
|
|
|
value = subject.to_a
|
|
|
|
|
2012-11-24 16:17:14 +01:00
|
|
|
value[1]['Content-Length'].should eq('6') # Headers
|
2012-11-23 17:43:39 +01:00
|
|
|
end
|
2012-11-23 19:39:56 +01:00
|
|
|
|
|
|
|
it "does not fix the Content-Length, if it's already set" do
|
|
|
|
subject.body = ['foobar']
|
2012-11-24 16:17:14 +01:00
|
|
|
subject.header = {'Content-Length' => '3'}
|
2012-11-23 19:39:56 +01:00
|
|
|
value = subject.to_a
|
|
|
|
|
2012-11-24 16:17:14 +01:00
|
|
|
value[1]['Content-Length'].should eq('3') # Headers
|
2012-11-23 20:05:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the Content-Type in the header" do
|
|
|
|
subject.header = {'Content-Type' => 'application/json'}
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[1]['Content-Type'].should eq('application/json') # Headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns as default 'text/html' as Content-Type" do
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[1]['Content-Type'].should eq('text/html') # Headers
|
2012-11-23 19:39:56 +01:00
|
|
|
end
|
2012-11-23 21:24:37 +01:00
|
|
|
|
|
|
|
it "removes Content-Type, Content-Length and body on status code 204" do
|
|
|
|
subject.body.push '"foobar"'
|
2012-11-24 15:35:23 +01:00
|
|
|
subject.content_type = 'application/json'
|
2012-11-23 21:24:37 +01:00
|
|
|
subject.header['Content-Length'] = 8
|
|
|
|
|
|
|
|
subject.status = 204
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[1].should eq({}) # Headers
|
|
|
|
end
|
2012-11-24 15:28:42 +01:00
|
|
|
|
|
|
|
it "removes Content-Type, Content-Length and body on status code 304" do
|
|
|
|
subject.body.push '"foobar"'
|
2012-11-24 15:35:23 +01:00
|
|
|
subject.content_type = 'application/json'
|
2012-11-24 15:28:42 +01:00
|
|
|
subject.header['Content-Length'] = 8
|
|
|
|
|
|
|
|
subject.status = 304
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[1].should eq({}) # Headers
|
|
|
|
end
|
2012-11-17 14:35:15 +01:00
|
|
|
end
|
2012-11-17 14:46:53 +01:00
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
describe '#status' do
|
|
|
|
it "must return the status always as an integer" do
|
|
|
|
subject.status = "foobar"
|
|
|
|
subject.status.should eq(0)
|
|
|
|
|
|
|
|
subject.status = 240.5
|
|
|
|
subject.status.should eq(240)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "must return 200, if no status code was set" do
|
|
|
|
subject.status.should eq(200)
|
|
|
|
end
|
2012-11-17 14:46:53 +01:00
|
|
|
end
|
2012-11-18 15:02:07 +01:00
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
describe '#header' do
|
|
|
|
it "must return an empty hash, if no header was set" do
|
|
|
|
subject.header.should eq({})
|
|
|
|
end
|
2012-11-18 15:02:07 +01:00
|
|
|
end
|
2012-11-18 16:09:41 +01:00
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
describe '#body' do
|
|
|
|
it "must return an empty array, if no body was set" do
|
|
|
|
subject.body.should eq([])
|
|
|
|
end
|
2012-11-18 16:09:41 +01:00
|
|
|
end
|
2012-11-18 16:38:38 +01:00
|
|
|
|
2012-11-23 17:43:39 +01:00
|
|
|
describe '#content_length' do
|
2012-11-24 16:17:14 +01:00
|
|
|
it "sets the Content-Length to '0', if there is no content" do
|
2012-11-23 17:43:39 +01:00
|
|
|
subject.content_length
|
|
|
|
|
2012-11-24 16:17:14 +01:00
|
|
|
subject.header['Content-Length'].should eq('0')
|
2012-11-23 17:43:39 +01:00
|
|
|
end
|
2012-11-23 17:59:33 +01:00
|
|
|
|
2012-11-23 19:54:17 +01:00
|
|
|
it "sets the Content-Length to the size of the message body" do
|
2012-11-23 17:59:33 +01:00
|
|
|
subject.body = ['foo', 'bar']
|
|
|
|
subject.content_length
|
|
|
|
|
2012-11-24 16:17:14 +01:00
|
|
|
subject.header['Content-Length'].should eq('6')
|
2012-11-23 17:59:33 +01:00
|
|
|
end
|
2012-11-23 18:06:53 +01:00
|
|
|
|
2012-11-23 19:54:17 +01:00
|
|
|
it "sets the Content-Length to the bytesize of the message body" do
|
2012-11-23 18:06:53 +01:00
|
|
|
subject.body = ['föö', 'bär']
|
|
|
|
subject.content_length
|
|
|
|
|
2012-11-24 16:17:14 +01:00
|
|
|
subject.header['Content-Length'].should eq('9')
|
2012-11-23 18:06:53 +01:00
|
|
|
end
|
2012-11-23 17:43:39 +01:00
|
|
|
end
|
|
|
|
|
2012-11-23 19:54:17 +01:00
|
|
|
describe '#content_type' do
|
|
|
|
it "sets the Content-Type to the given value" do
|
2012-11-24 15:35:23 +01:00
|
|
|
subject.content_type = 'application/json'
|
2012-11-23 19:54:17 +01:00
|
|
|
|
|
|
|
subject.header['Content-Type'].should eq('application/json')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-30 18:58:32 +01:00
|
|
|
describe '#redirect' do
|
|
|
|
it "sets the status to 302 and the given Location URL in header" do
|
|
|
|
subject.redirect 'http://foo.bar/relocated/thingy'
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[0].should eq(302)
|
|
|
|
value[1]['Location'].should eq('http://foo.bar/relocated/thingy')
|
|
|
|
end
|
2012-11-30 19:13:41 +01:00
|
|
|
|
|
|
|
it "sets the given status code and the given Location" do
|
|
|
|
subject.redirect('http://foo.bar/relocated/other_thingy', 307)
|
|
|
|
value = subject.to_a
|
|
|
|
|
|
|
|
value[0].should eq(307)
|
|
|
|
value[1]['Location'].should eq('http://foo.bar/relocated/other_thingy')
|
|
|
|
end
|
2012-11-30 18:58:32 +01:00
|
|
|
end
|
|
|
|
|
2012-11-18 16:38:38 +01:00
|
|
|
end
|