Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #53 from tkareine/do_not_mutate_options_in_try_static

Do not mutate options in TryStatic.new
  • Loading branch information...
commit 84a4bdfe57d033466e1940635b48d25ec231d583 2 parents 73975aa + 1c13e9e
@rkh rkh authored
Showing with 21 additions and 9 deletions.
  1. +2 −2 lib/rack/contrib/try_static.rb
  2. +19 −7 test/spec_rack_try_static.rb
View
4 lib/rack/contrib/try_static.rb
@@ -17,9 +17,9 @@ class TryStatic
def initialize(app, options)
@app = app
- @try = ['', *options.delete(:try)]
+ @try = ['', *options[:try]]
@static = ::Rack::Static.new(
- lambda { [404, {}, []] },
+ lambda { |_| [404, {}, []] },
options)
end
View
26 test/spec_rack_try_static.rb
@@ -4,23 +4,25 @@
require 'rack/contrib/try_static'
require 'rack/mock'
-def request(options = {})
- options.merge!({
+def build_options(opts)
+ {
:urls => %w[/],
:root => ::File.expand_path(::File.dirname(__FILE__)),
- })
+ }.merge(opts)
+end
+def request(options = {})
@request =
Rack::MockRequest.new(
Rack::TryStatic.new(
- lambda {[200, {}, ["Hello World"]]},
+ lambda { |_| [200, {}, ["Hello World"]]},
options))
end
describe "Rack::TryStatic" do
context 'when file cannot be found' do
it 'should call call app' do
- res = request(:try => ['html']).get('/documents')
+ res = request(build_options(:try => ['html'])).get('/documents')
res.should.be.ok
res.body.should == "Hello World"
end
@@ -28,7 +30,7 @@ def request(options = {})
context 'when file can be found' do
it 'should serve first found' do
- res = request(:try => ['.html', '/index.html', '/index.htm']).get('/documents')
+ res = request(build_options(:try => ['.html', '/index.html', '/index.htm'])).get('/documents')
res.should.be.ok
res.body.strip.should == "index.html"
end
@@ -36,9 +38,19 @@ def request(options = {})
context 'when path_info maps directly to file' do
it 'should serve existing' do
- res = request(:try => ['/index.html']).get('/documents/existing.html')
+ res = request(build_options(:try => ['/index.html'])).get('/documents/existing.html')
res.should.be.ok
res.body.strip.should == "existing.html"
end
end
+
+ context 'when sharing options' do
+ it 'should not mutate given options' do
+ org_options = build_options :try => ['/index.html']
+ given_options = org_options.dup
+ request(given_options).get('/documents').should.be.ok
+ request(given_options).get('/documents').should.be.ok
+ given_options.should == org_options
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.