rack-try_static 0.0.4 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rake/rdoctask'
2
+ require 'rake/testtask'
3
+
4
+ desc "Run all the tests"
5
+ task :default => [:test]
6
+
7
+ desc "Generate RDox"
8
+ task "RDOX" do
9
+ sh "specrb -Ilib:test -a --rdox >RDOX"
10
+ end
11
+
12
+ desc "Run specs with test/unit style output"
13
+ task :test do
14
+ sh "specrb -Ilib:test -w #{ENV['TEST'] || '-a'} #{ENV['TESTOPTS']}"
15
+ end
16
+
17
+ desc "Run specs with specdoc style output"
18
+ task :spec do
19
+ sh "specrb -Ilib:test -s -w #{ENV['TEST'] || '-a'} #{ENV['TESTOPTS']}"
20
+ end
21
+
22
+ desc "Run all the tests"
23
+ task :fulltest do
24
+ sh "specrb -Ilib:test -w #{ENV['TEST'] || '-a'} #{ENV['TESTOPTS']}"
25
+ end
26
+
@@ -1,24 +1,24 @@
1
1
  require 'rack/static'
2
2
 
3
3
  module ::Rack
4
- class TryStatic < Static
4
+ class TryStatic
5
5
 
6
6
  def initialize(app, options)
7
- super
8
- @try = ([''] + Array(options.delete(:try)) + [''])
7
+ @app = app
8
+ @try = ['', *options.delete(:try)]
9
+ @static = ::Rack::Static.new(
10
+ lambda { [404, {}, []] },
11
+ options)
9
12
  end
10
13
 
11
14
  def call(env)
12
- @next = 0
13
- while @next < @try.size && 404 == (resp = super(try_next(env)))[0]
14
- @next += 1
15
- end
16
- 404 == resp[0] ? @app.call : resp
17
- end
18
-
19
- private
20
- def try_next(env)
21
- env.merge('PATH_INFO' => env['PATH_INFO'] + @try[@next])
15
+ orig_path = env['PATH_INFO']
16
+ found = nil
17
+ @try.each do |path|
18
+ resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
19
+ break if 404 != resp[0] && found = resp
20
+ end
21
+ found or @app.call(env.merge!('PATH_INFO' => orig_path))
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env gem build
2
+ # encoding: utf-8
3
+ # vim: ft=ruby
4
+
5
+ require "base64"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "rack-try_static"
9
+ s.version = "0.1.1"
10
+ s.date = Time.now.strftime '%Y-%m-%d'
11
+
12
+ s.authors = ["gmarik"]
13
+ s.email = Base64.decode64("Z21hcmlrQGdtYWlsLmNvbQ==\n")
14
+
15
+ s.homepage = "http://github.com/gmarik/rack-try_static"
16
+ s.summary = "rack middleware to serve static files with specified patterns"
17
+ s.description = "#{s.summary}. Check out http://gmarik.info/blog/2010/05/10/blogging-with-jekyll-and-heroku-for-free for more..."
18
+
19
+ s.cert_chain = nil
20
+
21
+ # files
22
+ s.files = `git ls-files`.split("\n")
23
+
24
+ #Dir["bin/*"].map(&File.method(:basename))
25
+ #s.default_executable = "example"
26
+ s.require_paths = ["lib"]
27
+
28
+ # Ruby version
29
+ s.required_ruby_version = ::Gem::Requirement.new(">= 1.8.6")
30
+
31
+ # dependencies
32
+ # RubyGems has runtime dependencies (add_dependency) and
33
+ s.add_dependency 'rack', '1.1.0'
34
+
35
+ s.has_rdoc = false
36
+ #s.extra_rdoc_files = %w[README.rdoc COPYING]
37
+
38
+ # development dependencies (add_development_dependency)
39
+ s.add_development_dependency "test-spec"
40
+
41
+ s.require_paths = %w[lib]
42
+ s.test_files = s.files.select {|path| path =~ /^test\/spec_.*\.rb/}
43
+
44
+ #s.post_install_message = s.name + ' installed'
45
+
46
+ # RubyForge
47
+ #s.rubyforge_project = "example"
48
+ end
49
+
@@ -0,0 +1 @@
1
+ index.htm
@@ -0,0 +1 @@
1
+ index.html
@@ -0,0 +1,51 @@
1
+ require 'test/spec'
2
+
3
+ require 'rack'
4
+ require 'rack/contrib/try_static'
5
+ require 'rack/mock'
6
+
7
+ class DummyApp
8
+ def call(env)
9
+ [200, {}, ["Hello World"]]
10
+ end
11
+ end
12
+
13
+
14
+ def request(options = {})
15
+ options.merge!({
16
+ :urls => %w[/],
17
+ :root => ::File.expand_path(::File.dirname(__FILE__)),
18
+ })
19
+
20
+ @request =
21
+ Rack::MockRequest.new(
22
+ Rack::TryStatic.new(
23
+ DummyApp.new, options))
24
+ end
25
+
26
+ describe "Rack::TryStatic" do
27
+ context 'when tries fail' do
28
+ it 'should not serve' do
29
+ res = request(:try => ['html']).get('/documents')
30
+ res.should.be.ok
31
+ res.body.should == "Hello World"
32
+ end
33
+ end
34
+
35
+ context 'when tries succeed' do
36
+ it 'should serve first found' do
37
+ res = request(:try => ['.html', '/index.html', '/index.htm']).get('/documents')
38
+ res.should.be.ok
39
+ res.body.strip.should == "index.html"
40
+ end
41
+ end
42
+
43
+ context 'when tries not needed' do
44
+ it 'should serve existing' do
45
+ res = request(:try => ['/index.html']).get('/documents/existing.html')
46
+ res.should.be.ok
47
+ res.body.strip.should == "existing.html"
48
+ end
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-try_static
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - gmarik
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain:
17
- date: 2010-09-18 00:00:00 -05:00
17
+ date: 2010-09-19 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -34,7 +34,7 @@ dependencies:
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
37
- name: bundler
37
+ name: test-spec
38
38
  prerelease: false
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
@@ -57,7 +57,12 @@ extra_rdoc_files: []
57
57
 
58
58
  files:
59
59
  - README.md
60
+ - Rakefile
60
61
  - lib/rack/contrib/try_static.rb
62
+ - rack-try_static.gemspec
63
+ - test/documents/index.htm
64
+ - test/documents/index.html
65
+ - test/spec_rack_try_static.rb
61
66
  has_rdoc: true
62
67
  homepage: http://github.com/gmarik/rack-try_static
63
68
  licenses: []
@@ -94,5 +99,5 @@ rubygems_version: 1.3.7
94
99
  signing_key:
95
100
  specification_version: 3
96
101
  summary: rack middleware to serve static files with specified patterns
97
- test_files: []
98
-
102
+ test_files:
103
+ - test/spec_rack_try_static.rb