spec_conditionalget.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. require 'time'
  2. require 'rack/conditionalget'
  3. require 'rack/mock'
  4. describe Rack::ConditionalGet do
  5. def conditional_get(app)
  6. Rack::Lint.new Rack::ConditionalGet.new(app)
  7. end
  8. should "set a 304 status and truncate body when If-Modified-Since hits" do
  9. timestamp = Time.now.httpdate
  10. app = conditional_get(lambda { |env|
  11. [200, {'Last-Modified'=>timestamp}, ['TEST']] })
  12. response = Rack::MockRequest.new(app).
  13. get("/", 'HTTP_IF_MODIFIED_SINCE' => timestamp)
  14. response.status.should.equal 304
  15. response.body.should.be.empty
  16. end
  17. should "set a 304 status and truncate body when If-Modified-Since hits and is higher than current time" do
  18. app = conditional_get(lambda { |env|
  19. [200, {'Last-Modified'=>(Time.now - 3600).httpdate}, ['TEST']] })
  20. response = Rack::MockRequest.new(app).
  21. get("/", 'HTTP_IF_MODIFIED_SINCE' => Time.now.httpdate)
  22. response.status.should.equal 304
  23. response.body.should.be.empty
  24. end
  25. should "set a 304 status and truncate body when If-None-Match hits" do
  26. app = conditional_get(lambda { |env|
  27. [200, {'Etag'=>'1234'}, ['TEST']] })
  28. response = Rack::MockRequest.new(app).
  29. get("/", 'HTTP_IF_NONE_MATCH' => '1234')
  30. response.status.should.equal 304
  31. response.body.should.be.empty
  32. end
  33. should "not set a 304 status if If-Modified-Since hits but Etag does not" do
  34. timestamp = Time.now.httpdate
  35. app = conditional_get(lambda { |env|
  36. [200, {'Last-Modified'=>timestamp, 'Etag'=>'1234', 'Content-Type' => 'text/plain'}, ['TEST']] })
  37. response = Rack::MockRequest.new(app).
  38. get("/", 'HTTP_IF_MODIFIED_SINCE' => timestamp, 'HTTP_IF_NONE_MATCH' => '4321')
  39. response.status.should.equal 200
  40. response.body.should.equal 'TEST'
  41. end
  42. should "set a 304 status and truncate body when both If-None-Match and If-Modified-Since hits" do
  43. timestamp = Time.now.httpdate
  44. app = conditional_get(lambda { |env|
  45. [200, {'Last-Modified'=>timestamp, 'Etag'=>'1234'}, ['TEST']] })
  46. response = Rack::MockRequest.new(app).
  47. get("/", 'HTTP_IF_MODIFIED_SINCE' => timestamp, 'HTTP_IF_NONE_MATCH' => '1234')
  48. response.status.should.equal 304
  49. response.body.should.be.empty
  50. end
  51. should "not affect non-GET/HEAD requests" do
  52. app = conditional_get(lambda { |env|
  53. [200, {'Etag'=>'1234', 'Content-Type' => 'text/plain'}, ['TEST']] })
  54. response = Rack::MockRequest.new(app).
  55. post("/", 'HTTP_IF_NONE_MATCH' => '1234')
  56. response.status.should.equal 200
  57. response.body.should.equal 'TEST'
  58. end
  59. should "not affect non-200 requests" do
  60. app = conditional_get(lambda { |env|
  61. [302, {'Etag'=>'1234', 'Content-Type' => 'text/plain'}, ['TEST']] })
  62. response = Rack::MockRequest.new(app).
  63. get("/", 'HTTP_IF_NONE_MATCH' => '1234')
  64. response.status.should.equal 302
  65. response.body.should.equal 'TEST'
  66. end
  67. should "not affect requests with malformed HTTP_IF_NONE_MATCH" do
  68. bad_timestamp = Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
  69. app = conditional_get(lambda { |env|
  70. [200,{'Last-Modified'=>(Time.now - 3600).httpdate, 'Content-Type' => 'text/plain'}, ['TEST']] })
  71. response = Rack::MockRequest.new(app).
  72. get("/", 'HTTP_IF_MODIFIED_SINCE' => bad_timestamp)
  73. response.status.should.equal 200
  74. response.body.should.equal 'TEST'
  75. end
  76. end