From a519e5615b571d7a34d92c68da5ccfffeef0822c Mon Sep 17 00:00:00 2001 From: Dennis Sivia Date: Mon, 29 Feb 2016 17:09:33 +0100 Subject: [PATCH] Improve Cookie testing. Implements [PR 133](https://github.com/brynary/rack-test/pull/133/files) --- lib/rack/test/cookie_jar.rb | 16 +++++++ spec/rack/test/cookie_jar_spec.rb | 21 +++++++++ spec/rack/test/cookie_object_spec.rb | 66 ++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 spec/rack/test/cookie_jar_spec.rb create mode 100644 spec/rack/test/cookie_object_spec.rb diff --git a/lib/rack/test/cookie_jar.rb b/lib/rack/test/cookie_jar.rb index 9750bba3..888729fd 100644 --- a/lib/rack/test/cookie_jar.rb +++ b/lib/rack/test/cookie_jar.rb @@ -48,6 +48,10 @@ def secure? @options.has_key?("secure") end + def http_only? + @options.has_key?('HttpOnly') + end + # :api: private def path @options["path"].strip || "/" @@ -87,6 +91,14 @@ def <=>(other) # Orders the cookies from least specific to most [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse] end + def to_h + @options.merge( + 'value' => @value, + 'HttpOnly' => http_only?, + 'secure' => secure?, + ) + end + alias_method :to_hash, :to_h protected @@ -115,6 +127,10 @@ def []=(name, value) merge("#{name}=#{Rack::Utils.escape(value)}") end + def get_cookie(name) + hash_for(nil).fetch(name,nil) + end + def delete(name) @cookies.reject! do |cookie| cookie.name == name diff --git a/spec/rack/test/cookie_jar_spec.rb b/spec/rack/test/cookie_jar_spec.rb new file mode 100644 index 00000000..90efd2a1 --- /dev/null +++ b/spec/rack/test/cookie_jar_spec.rb @@ -0,0 +1,21 @@ +require "spec_helper" + +describe Rack::Test::CookieJar do + subject(:jar) { Rack::Test::CookieJar.new } + + describe "#get_cookie" do + context "with a populated jar" do + let(:cookie_value) { "foo;abc" } + let(:cookie_name) { "a_cookie_name" } + + before do + jar[cookie_name] = cookie_value + end + + it "returns full cookie objects" do + cookie = jar.get_cookie(cookie_name) + expect(cookie).to be_a(Rack::Test::Cookie) + end + end + end +end diff --git a/spec/rack/test/cookie_object_spec.rb b/spec/rack/test/cookie_object_spec.rb new file mode 100644 index 00000000..f08b8277 --- /dev/null +++ b/spec/rack/test/cookie_object_spec.rb @@ -0,0 +1,66 @@ +require "spec_helper" + +describe Rack::Test::Cookie do + subject(:cookie) { Rack::Test::Cookie.new(cookie_string) } + + let(:cookie_string) { raw_cookie_string } + + let(:raw_cookie_string) { + [ + "cookie_name=" + CGI.escape(value), + "domain=" + domain, + "path=" + path, + "expires=" + expires, + ].join("; ") + } + + let(:http_only_raw_cookie_string) { + raw_cookie_string + "; HttpOnly" + } + + let(:http_only_secure_raw_cookie_string) { + http_only_raw_cookie_string + "; secure" + } + + let(:value) { "the cookie value" } + let(:domain) { "www.example.org" } + let(:path) { "/" } + let(:expires) { "Mon, 10 Aug 2015 14:40:57 0100" } + + describe "#to_h" do + let(:cookie_string) { http_only_secure_raw_cookie_string } + + it "returns the cookie value and all options" do + expect(cookie.to_h).to eq( + "value" => value, + "domain" => domain, + "path" => path, + "expires" => expires, + "HttpOnly" => true, + "secure" => true, + ) + end + end + + describe "#to_hash" do + it "is an alias for #to_h" do + expect(cookie.to_hash).to eq(cookie.to_h) + end + end + + describe "#http_only?" do + context "for a non HTTP only cookie" do + it "returns false" do + expect(cookie.http_only?).to be(false) + end + end + + context "for a HTTP only cookie" do + let(:cookie_string) { http_only_raw_cookie_string } + + it "returns true" do + expect(cookie.http_only?).to be(true) + end + end + end +end