PageRenderTime 42ms CodeModel.GetById 18ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rack-1.5.2/test/spec_conditionalget.rb

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