aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
authorYuki Nishijima <mail@yukinishijima.net>2015-05-01 19:12:45 -0700
committerYuki Nishijima <mail@yukinishijima.net>2015-06-13 09:30:23 -0700
commit52260581638406d910e09e8d2e66b51acb76c5c6 (patch)
tree489fda2ba90d2a39abedf87890db19bcd7c1bc6a /actionpack/test
parentdfc9269517d787d187ee46c798a4ea0893de0381 (diff)
downloadrails-52260581638406d910e09e8d2e66b51acb76c5c6.tar.gz
rails-52260581638406d910e09e8d2e66b51acb76c5c6.tar.bz2
rails-52260581638406d910e09e8d2e66b51acb76c5c6.zip
Add the ability of returning arbitrary headers to ActionDispatch::Static
Now ActionDispatch::Static can accept HTTP headers so that developers will have control of returning arbitrary headers like 'Access-Control-Allow-Origin' when a response is delivered. They can be configured through `#config.public_file_server.headers`: config.public_file_server.headers = { "Cache-Control" => "public, max-age=60", "Access-Control-Allow-Origin" => "http://rubyonrails.org" } Also deprecate `config.static_cache_control` in favor of `config.public_file_server.headers`.
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/dispatch/static_test.rb35
1 files changed, 27 insertions, 8 deletions
diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb
index 95971b3a0e..191932a469 100644
--- a/actionpack/test/dispatch/static_test.rb
+++ b/actionpack/test/dispatch/static_test.rb
@@ -2,6 +2,10 @@ require 'abstract_unit'
require 'zlib'
module StaticTests
+ DummyApp = lambda { |env|
+ [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
+ }
+
def setup
silence_warnings do
@default_internal_encoding = Encoding.default_internal
@@ -37,7 +41,11 @@ module StaticTests
end
def test_sets_cache_control
- response = get("/index.html")
+ app = assert_deprecated do
+ ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60")
+ end
+ response = Rack::MockRequest.new(app).request("GET", "/index.html")
+
assert_html "/index.html", response
assert_equal "public, max-age=60", response.headers["Cache-Control"]
end
@@ -180,6 +188,21 @@ module StaticTests
assert_equal nil, response.headers['Vary']
end
+ def test_serves_files_with_headers
+ headers = {
+ "Access-Control-Allow-Origin" => 'http://rubyonrails.org',
+ "Cache-Control" => 'public, max-age=60',
+ "X-Custom-Header" => "I'm a teapot"
+ }
+
+ app = ActionDispatch::Static.new(DummyApp, @root, headers: headers)
+ response = Rack::MockRequest.new(app).request("GET", "/foo/bar.html")
+
+ assert_equal 'http://rubyonrails.org', response.headers["Access-Control-Allow-Origin"]
+ assert_equal 'public, max-age=60', response.headers["Cache-Control"]
+ assert_equal "I'm a teapot", response.headers["X-Custom-Header"]
+ end
+
# Windows doesn't allow \ / : * ? " < > | in filenames
unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
def test_serves_static_file_with_colon
@@ -230,14 +253,10 @@ module StaticTests
end
class StaticTest < ActiveSupport::TestCase
- DummyApp = lambda { |env|
- [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]]
- }
-
def setup
super
@root = "#{FIXTURE_LOAD_PATH}/public"
- @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60")
+ @app = ActionDispatch::Static.new(DummyApp, @root, headers: {'Cache-Control' => "public, max-age=60"})
end
def public_path
@@ -263,7 +282,7 @@ class StaticTest < ActiveSupport::TestCase
end
def test_non_default_static_index
- @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60", index: "other-index")
+ @app = ActionDispatch::Static.new(DummyApp, @root, index: "other-index")
assert_html "/other-index.html", get("/other-index.html")
assert_html "/other-index.html", get("/other-index")
assert_html "/other-index.html", get("/")
@@ -280,7 +299,7 @@ class StaticEncodingTest < StaticTest
def setup
super
@root = "#{FIXTURE_LOAD_PATH}/公共"
- @app = ActionDispatch::Static.new(DummyApp, @root, "public, max-age=60")
+ @app = ActionDispatch::Static.new(DummyApp, @root, headers: {'Cache-Control' => "public, max-age=60"})
end
def public_path