diff options
author | Gibheer <gibheer@gmail.com> | 2013-10-28 14:37:24 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2013-10-28 14:37:24 +0100 |
commit | a0422c10546b2e0dea252e68d1870f362095cdab (patch) | |
tree | 1574d6466c5b0895911bfb3055cea88d02a5d670 /spec/unit | |
parent | 290165c440caceb53fd12ad6aa460069852a26ec (diff) |
add cookie support to response
This commit adds support for response cookies. Response now has a method
cookie to fetch the current cookie. One cookie has multiple crumbs which
represent a key value pair. For each crumb multiple options can be set
according to the specs.
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/zero/response/cookie/add_crumb_spec.rb | 54 | ||||
-rw-r--r-- | spec/unit/zero/response/cookie/get_crumb_spec.rb | 21 | ||||
-rw-r--r-- | spec/unit/zero/response/cookie/to_header_spec.rb | 29 | ||||
-rw-r--r-- | spec/unit/zero/response/to_a_spec.rb | 7 |
4 files changed, 111 insertions, 0 deletions
diff --git a/spec/unit/zero/response/cookie/add_crumb_spec.rb b/spec/unit/zero/response/cookie/add_crumb_spec.rb new file mode 100644 index 0000000..3c8dbee --- /dev/null +++ b/spec/unit/zero/response/cookie/add_crumb_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe Zero::Response::Cookie, '#add_crumb' do + let(:cookie) { Zero::Response::Cookie.new } + subject { cookie.add_crumb(key, value, options) } + let(:options) { {} } + let(:key) { 'key' } + let(:value) { 'value' } + + before :each do + subject + end + + context 'with no argument' do + it 'adds a new crumb' do + expect(cookie.get_crumb(key).key).to be(key) + end + end + + context 'with flags' do + let(:options) { {:flags => [:secure, :http_only]} } + + it 'adds a crumb with secure header' do + expect(cookie.get_crumb(key).secure).to be(true) + end + + it 'adds a crumb with http_only header' do + expect(cookie.get_crumb(key).http_only).to be(true) + end + end + + context 'with expire' do + let(:time) { Time.now } + let(:options) { {:expire => time} } + + it 'adds a crumb with expire header' do + expect(cookie.get_crumb(key).expire).to be(time) + end + end + + context 'with domain and path' do + let(:domain) { 'libzero.org' } + let(:path) { '/admin' } + let(:options) { {:domain => domain, :path => path} } + + it 'adds a crumb with domain header' do + expect(cookie.get_crumb(key).domain).to be(domain) + end + + it 'adds a crumb with path header' do + expect(cookie.get_crumb(key).path).to be(path) + end + end +end diff --git a/spec/unit/zero/response/cookie/get_crumb_spec.rb b/spec/unit/zero/response/cookie/get_crumb_spec.rb new file mode 100644 index 0000000..3c315db --- /dev/null +++ b/spec/unit/zero/response/cookie/get_crumb_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Zero::Response::Cookie, '#add_crumb' do + let(:cookie) { Zero::Response::Cookie.new } + subject { cookie.add_crumb(key, value, options) } + let(:options) { {} } + let(:key) { 'key' } + let(:value) { 'value' } + + before :each do + subject + end + + it 'returns the crumb when the crumb exists' do + expect(cookie.get_crumb(key).key).to be(key) + end + + it 'returns nil for the wrong key' do + expect(cookie.get_crumb('wrong key')).to be(nil) + end +end diff --git a/spec/unit/zero/response/cookie/to_header_spec.rb b/spec/unit/zero/response/cookie/to_header_spec.rb new file mode 100644 index 0000000..7cdc278 --- /dev/null +++ b/spec/unit/zero/response/cookie/to_header_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe Zero::Response::Cookie, '#add_crumb' do + let(:cookie) { Zero::Response::Cookie.new } + subject { cookie.add_crumb(key, value, options) } + let(:options) { { + :domain => domain, + :path => path, + :expire => time, + :flags => flags + } } + let(:key) { 'key' } + let(:value) { 'value' } + let(:time) { Time.new } + let(:domain) { 'libzero.org' } + let(:path) { '/admin' } + let(:flags) { [:secure, :http_only] } + + before :each do + subject + end + + it 'returns the header line' do + expect(cookie.to_header).to eq( + {'Set-Cookie' => "#{key}=#{value}; Expires=#{time.rfc2822};" + + " Path=#{path}; Domain=#{domain}; HttpOnly; Secure"} + ) + end +end diff --git a/spec/unit/zero/response/to_a_spec.rb b/spec/unit/zero/response/to_a_spec.rb index b016eb9..b547f98 100644 --- a/spec/unit/zero/response/to_a_spec.rb +++ b/spec/unit/zero/response/to_a_spec.rb @@ -69,5 +69,12 @@ describe Zero::Response do value[1].should eq({}) # Headers value[2].should eq([]) # Body end + + it "adds the cookie to the headers" do + key = 'key' + value = 'value' + subject.cookie.add_crumb(key, value) + expect(subject.to_a[1]['Set-Cookie']).to eq("#{key}=#{value}") + end end end |