1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
require 'spec_helper'
RSpec.describe HTTParty::CookieHash do
before(:each) do
@cookie_hash = HTTParty::CookieHash.new
end
describe "#add_cookies" do
describe "with a hash" do
it "should add new key/value pairs to the hash" do
@cookie_hash.add_cookies(foo: "bar")
@cookie_hash.add_cookies(rofl: "copter")
expect(@cookie_hash.length).to eql(2)
end
it "should overwrite any existing key" do
@cookie_hash.add_cookies(foo: "bar")
@cookie_hash.add_cookies(foo: "copter")
expect(@cookie_hash.length).to eql(1)
expect(@cookie_hash[:foo]).to eql("copter")
end
end
describe "with a string" do
it "should add new key/value pairs to the hash" do
@cookie_hash.add_cookies("first=one; second=two; third")
expect(@cookie_hash[:first]).to eq('one')
expect(@cookie_hash[:second]).to eq('two')
expect(@cookie_hash[:third]).to eq(nil)
end
it "should overwrite any existing key" do
@cookie_hash[:foo] = 'bar'
@cookie_hash.add_cookies("foo=tar")
expect(@cookie_hash.length).to eql(1)
expect(@cookie_hash[:foo]).to eql("tar")
end
it "should handle '=' within cookie value" do
@cookie_hash.add_cookies("first=one=1; second=two=2==")
expect(@cookie_hash.keys).to include(:first, :second)
expect(@cookie_hash[:first]).to eq('one=1')
expect(@cookie_hash[:second]).to eq('two=2==')
end
it "should handle an empty cookie parameter" do
@cookie_hash.add_cookies("first=one; domain=mydomain.com; path=/; ; SameSite; Secure")
expect(@cookie_hash.keys).to include(:first, :domain, :path, :SameSite, :Secure)
end
end
describe 'with other class' do
it "should error" do
expect {
@cookie_hash.add_cookies([])
}.to raise_error(RuntimeError)
end
end
end
# The regexen are required because Hashes aren't ordered, so a test against
# a hardcoded string was randomly failing.
describe "#to_cookie_string" do
before(:each) do
@cookie_hash.add_cookies(foo: "bar")
@cookie_hash.add_cookies(rofl: "copter")
@s = @cookie_hash.to_cookie_string
end
it "should format the key/value pairs, delimited by semi-colons" do
expect(@s).to match(/foo=bar/)
expect(@s).to match(/rofl=copter/)
expect(@s).to match(/^\w+=\w+; \w+=\w+$/)
end
it "should not include client side only cookies" do
@cookie_hash.add_cookies(path: "/")
@s = @cookie_hash.to_cookie_string
expect(@s).not_to match(/path=\//)
end
it "should not include SameSite attribute" do
@cookie_hash.add_cookies(samesite: "Strict")
@s = @cookie_hash.to_cookie_string
expect(@s).not_to match(/samesite=Strict/)
end
it "should not include client side only cookies even when attributes use camal case" do
@cookie_hash.add_cookies(Path: "/")
@s = @cookie_hash.to_cookie_string
expect(@s).not_to match(/Path=\//)
end
it "should not mutate the hash" do
original_hash = {
"session" => "91e25e8b-6e32-418d-c72f-2d18adf041cd",
"Max-Age" => "15552000",
"cart" => "91e25e8b-6e32-418d-c72f-2d18adf041cd",
"httponly" => nil,
"Path" => "/",
"secure" => nil,
}
cookie_hash = HTTParty::CookieHash[original_hash]
cookie_hash.to_cookie_string
expect(cookie_hash).to eq(original_hash)
end
end
end
|