PageRenderTime 406ms CodeModel.GetById 117ms app.highlight 102ms RepoModel.GetById 110ms app.codeStats 1ms

/spec/httparty/cookie_hash_spec.rb

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