cachecontrol_test.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. require "#{File.dirname(__FILE__)}/spec_setup"
  2. require 'rack/cache/cachecontrol'
  3. describe 'Rack::Cache::CacheControl' do
  4. it 'takes no args and initializes with an empty set of values' do
  5. cache_control = Rack::Cache::CacheControl.new
  6. cache_control.should.be.empty
  7. cache_control.to_s.should.equal ''
  8. end
  9. it 'takes a String and parses it into a Hash when created' do
  10. cache_control = Rack::Cache::CacheControl.new('max-age=600, foo')
  11. cache_control['max-age'].should.equal '600'
  12. cache_control['foo'].should.be.true
  13. end
  14. it 'takes a String with a single name=value pair' do
  15. cache_control = Rack::Cache::CacheControl.new('max-age=600')
  16. cache_control['max-age'].should.equal '600'
  17. end
  18. it 'takes a String with multiple name=value pairs' do
  19. cache_control = Rack::Cache::CacheControl.new('max-age=600, max-stale=300, min-fresh=570')
  20. cache_control['max-age'].should.equal '600'
  21. cache_control['max-stale'].should.equal '300'
  22. cache_control['min-fresh'].should.equal '570'
  23. end
  24. it 'takes a String with a single flag value' do
  25. cache_control = Rack::Cache::CacheControl.new('no-cache')
  26. cache_control.should.include 'no-cache'
  27. cache_control['no-cache'].should.be.true
  28. end
  29. it 'takes a String with a bunch of all kinds of stuff' do
  30. cache_control =
  31. Rack::Cache::CacheControl.new('max-age=600,must-revalidate,min-fresh=3000,foo=bar,baz')
  32. cache_control['max-age'].should.equal '600'
  33. cache_control['must-revalidate'].should.be.true
  34. cache_control['min-fresh'].should.equal '3000'
  35. cache_control['foo'].should.equal 'bar'
  36. cache_control['baz'].should.be.true
  37. end
  38. it 'strips leading and trailing spaces from header value' do
  39. cache_control = Rack::Cache::CacheControl.new(' public, max-age = 600 ')
  40. cache_control.should.include 'public'
  41. cache_control.should.include 'max-age'
  42. cache_control['max-age'].should.equal '600'
  43. end
  44. it 'strips blank segments' do
  45. cache_control = Rack::Cache::CacheControl.new('max-age=600,,max-stale=300')
  46. cache_control['max-age'].should.equal '600'
  47. cache_control['max-stale'].should.equal '300'
  48. end
  49. it 'removes all directives with #clear' do
  50. cache_control = Rack::Cache::CacheControl.new('max-age=600, must-revalidate')
  51. cache_control.clear
  52. cache_control.should.be.empty
  53. end
  54. it 'converts self into header String with #to_s' do
  55. cache_control = Rack::Cache::CacheControl.new
  56. cache_control['public'] = true
  57. cache_control['max-age'] = '600'
  58. cache_control.to_s.split(', ').sort.should.equal ['max-age=600', 'public']
  59. end
  60. it 'sorts alphabetically with boolean directives before value directives' do
  61. cache_control = Rack::Cache::CacheControl.new('foo=bar, z, x, y, bling=baz, zoom=zib, b, a')
  62. cache_control.to_s.should.equal 'a, b, x, y, z, bling=baz, foo=bar, zoom=zib'
  63. end
  64. it 'responds to #max_age with an integer when max-age directive present' do
  65. cache_control = Rack::Cache::CacheControl.new('public, max-age=600')
  66. cache_control.max_age.should.equal 600
  67. end
  68. it 'responds to #max_age with nil when no max-age directive present' do
  69. cache_control = Rack::Cache::CacheControl.new('public')
  70. cache_control.max_age.should.be.nil
  71. end
  72. it 'responds to #shared_max_age with an integer when s-maxage directive present' do
  73. cache_control = Rack::Cache::CacheControl.new('public, s-maxage=600')
  74. cache_control.shared_max_age.should.equal 600
  75. end
  76. it 'responds to #shared_max_age with nil when no s-maxage directive present' do
  77. cache_control = Rack::Cache::CacheControl.new('public')
  78. cache_control.shared_max_age.should.be.nil
  79. end
  80. it 'responds to #public? truthfully when public directive present' do
  81. cache_control = Rack::Cache::CacheControl.new('public')
  82. cache_control.should.be.public
  83. end
  84. it 'responds to #public? non-truthfully when no public directive present' do
  85. cache_control = Rack::Cache::CacheControl.new('private')
  86. cache_control.should.not.be.public
  87. end
  88. it 'responds to #private? truthfully when private directive present' do
  89. cache_control = Rack::Cache::CacheControl.new('private')
  90. cache_control.should.be.private
  91. end
  92. it 'responds to #private? non-truthfully when no private directive present' do
  93. cache_control = Rack::Cache::CacheControl.new('public')
  94. cache_control.should.not.be.private
  95. end
  96. it 'responds to #no_cache? truthfully when no-cache directive present' do
  97. cache_control = Rack::Cache::CacheControl.new('no-cache')
  98. cache_control.should.be.no_cache
  99. end
  100. it 'responds to #no_cache? non-truthfully when no no-cache directive present' do
  101. cache_control = Rack::Cache::CacheControl.new('max-age=600')
  102. cache_control.should.not.be.no_cache
  103. end
  104. it 'responds to #must_revalidate? truthfully when must-revalidate directive present' do
  105. cache_control = Rack::Cache::CacheControl.new('must-revalidate')
  106. cache_control.should.be.must_revalidate
  107. end
  108. it 'responds to #must_revalidate? non-truthfully when no must-revalidate directive present' do
  109. cache_control = Rack::Cache::CacheControl.new('max-age=600')
  110. cache_control.should.not.be.no_cache
  111. end
  112. it 'responds to #proxy_revalidate? truthfully when proxy-revalidate directive present' do
  113. cache_control = Rack::Cache::CacheControl.new('proxy-revalidate')
  114. cache_control.should.be.proxy_revalidate
  115. end
  116. it 'responds to #proxy_revalidate? non-truthfully when no proxy-revalidate directive present' do
  117. cache_control = Rack::Cache::CacheControl.new('max-age=600')
  118. cache_control.should.not.be.no_cache
  119. end
  120. end