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
|
require File.expand_path(File.join(File.dirname(__FILE__), '../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
end
describe 'with other class' do
it "should error" do
expect {
@cookie_hash.add_cookies([])
}.to raise_error
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 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
end
end
|