aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/middleware/static_test.rb
blob: 9a0a5f72656b37dc86258de7325b253681c20108 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
require "isolation/abstract_unit"
require "rack/test"

module ApplicationTests
  class MiddlewareStaticTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation
    include Rack::Test::Methods

    def setup
      build_app
      FileUtils.rm_rf "#{app_path}/config/environments"
    end

    def teardown
      teardown_app
    end

    # Regression test to #8907
    # See https://github.com/rails/rails/commit/9cc82b77196d21a5c7021f6dca59ab9b2b158a45#commitcomment-2416514
    test "doesn't set Cache-Control header when it is nil" do
      app_file "public/foo.html", "static"

      require "#{app_path}/config/environment"

      get "foo"

      assert_not last_response.headers.has_key?("Cache-Control"), "Cache-Control should not be set"
    end

    test "headers for static files are configurable" do
      app_file "public/about.html", "static"
      add_to_config <<-CONFIG
        config.public_file_server.headers = {
          "Access-Control-Allow-Origin" => "http://rubyonrails.org",
          "Cache-Control"               => "public, max-age=60"
        }
      CONFIG

      require "#{app_path}/config/environment"

      get "/about.html"

      assert_equal "http://rubyonrails.org", last_response.headers["Access-Control-Allow-Origin"]
      assert_equal "public, max-age=60",     last_response.headers["Cache-Control"]
    end

    test "public_file_server.index_name defaults to 'index'" do
      app_file "public/index.html", "/index.html"

      require "#{app_path}/config/environment"

      get "/"

      assert_equal "/index.html\n", last_response.body
    end

    test "public_file_server.index_name configurable" do
      app_file "public/other-index.html", "/other-index.html"
      add_to_config "config.public_file_server.index_name = 'other-index'"

      require "#{app_path}/config/environment"

      get "/"

      assert_equal "/other-index.html\n", last_response.body
    end
  end
end