/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

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