Sha256: f37d1545fef2a5cc9cc1c45fa34ee400c21477a400c26661f0a053cacf2d2595

Contents?: true

Size: 1.43 KB

Versions: 47

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "abstract_unit"
require "active_support/core_ext/integer/time"
require "active_support/core_ext/numeric/time"

class ConditionalGetApiController < ActionController::API
  before_action :handle_last_modified_and_etags, only: :two

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

  def two
    render plain: "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 ConditionalGetApiTest < ActionController::TestCase
  tests ConditionalGetApiController

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

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

  def test_request_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_predicate @response.body, :blank?
    assert_equal @last_modified, @response.headers["Last-Modified"]
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
jets-1.9.7 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.6 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.5 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.4 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.3 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.2 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.1 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.9.0 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.14 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.13 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.12 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.11 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.10 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.9 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.8 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.7 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.6 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.5 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.4 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb
jets-1.8.3 vendor/rails/actionpack/test/controller/api/conditional_get_test.rb