Skip to content
Browse files

Add Rack::Cascade, to pass on the first non 404 result

darcs-hash:20070228200306-4fc50-493c566759d308dca533b0e5108cb14606af4edc.gz
  • Loading branch information...
1 parent be41d90 commit f0f0840cf3c617479fe59a84cded4a5d180acfd7 @chneukirchen chneukirchen committed Feb 28, 2007
Showing with 57 additions and 0 deletions.
  1. +22 −0 lib/rack/cascade.rb
  2. +35 −0 test/spec_rack_cascade.rb
View
22 lib/rack/cascade.rb
@@ -0,0 +1,22 @@
+module Rack
+ class Cascade
+ attr_reader :apps
+
+ def initialize(apps, catch=404)
+ @apps = apps
+ @catch = [*catch]
+ end
+
+ def call(env)
+ status = headers = body = nil
+ raise ArgumentError, "empty cascade" if @apps.empty?
+ @apps.each { |app|
+ begin
+ status, headers, body = app.call(env)
+ break unless @catch.include?(status.to_i)
+ end
+ }
+ [status, headers, body]
+ end
+ end
+end
View
35 test/spec_rack_cascade.rb
@@ -0,0 +1,35 @@
+require 'test/spec'
+
+require 'rack/cascade'
+require 'rack/mock'
+
+require 'rack/urlmap'
+require 'rack/file'
+
+context "Rack::Cascade" do
+ docroot = File.expand_path(File.dirname(__FILE__))
+ app1 = Rack::File.new(docroot)
+
+ app2 = Rack::URLMap.new("/crash" => lambda { |env| raise "boom" })
+
+ app3 = Rack::URLMap.new("/foo" => lambda { |env|
+ [200, { "Content-Type" => "text/plain"}, [""]]})
+
+ specify "should dispatch onward on 404 by default" do
+ cascade = Rack::Cascade.new([app1, app2, app3])
+ Rack::MockRequest.new(cascade).get("/cgi/test").should.be.ok
+ Rack::MockRequest.new(cascade).get("/foo").should.be.ok
+ Rack::MockRequest.new(cascade).get("/toobad").should.be.not_found
+ Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.forbidden
+ end
+
+ specify "should dispatch onward on whatever is passed" do
+ cascade = Rack::Cascade.new([app1, app2, app3], [404, 403])
+ Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.not_found
+ end
+
+ specify "should fail if empty" do
+ lambda { Rack::MockRequest.new(Rack::Cascade.new([])).get("/") }.
+ should.raise(ArgumentError)
+ end
+end

0 comments on commit f0f0840

Please sign in to comment.
Something went wrong with that request. Please try again.