Skip to content

Commit

Permalink
Improve Cookie testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Sivia authored and perlun committed Apr 21, 2017
1 parent d016695 commit a519e56
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rack/test/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "/"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/rack/test/cookie_jar_spec.rb
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions spec/rack/test/cookie_object_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a519e56

Please sign in to comment.