aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/http/conditional_get_test.rb
blob: 70d5ef296fcf035d34ce8f3140621ae98999794e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'abstract_unit'

class ConditionalGetHTTPController < ActionController::HTTP
  before_filter :handle_last_modified_and_etags, :only => :two

  def one
    if stale?(:last_modified => Time.now.utc.beginning_of_day, :etag => [:foo, 123])
      render :text => "Hi!"
    end
  end

  def two
    render :text => "Hi!"
  end

  private

  def handle_last_modified_and_etags
    fresh_when(:last_modified => Time.now.utc.beginning_of_day, :etag => [ :foo, 123 ])
  end
end

class ConditionalGetHTTPTest < ActionController::TestCase
  tests ConditionalGetHTTPController

  def setup
    @last_modified = Time.now.utc.beginning_of_day.httpdate
  end

  def test_request_with_bang_gets_last_modified
    get :two
    assert_equal @last_modified, @response.headers['Last-Modified']
    assert_response :success
  end

  def test_request_with_bang_obeys_last_modified
    @request.if_modified_since = @last_modified
    get :two
    assert_response :not_modified
  end

  def test_last_modified_works_with_less_than_too
    @request.if_modified_since = 5.years.ago.httpdate
    get :two
    assert_response :success
  end

  def test_request_not_modified
    @request.if_modified_since = @last_modified
    get :one
    assert_equal 304, @response.status.to_i
    assert_blank @response.body
    assert_equal @last_modified, @response.headers['Last-Modified']
  end
end