spec_cascade.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require 'rack/cascade'
  2. require 'rack/file'
  3. require 'rack/lint'
  4. require 'rack/urlmap'
  5. require 'rack/mock'
  6. describe Rack::Cascade do
  7. def cascade(*args)
  8. Rack::Lint.new Rack::Cascade.new(*args)
  9. end
  10. docroot = File.expand_path(File.dirname(__FILE__))
  11. app1 = Rack::File.new(docroot)
  12. app2 = Rack::URLMap.new("/crash" => lambda { |env| raise "boom" })
  13. app3 = Rack::URLMap.new("/foo" => lambda { |env|
  14. [200, { "Content-Type" => "text/plain"}, [""]]})
  15. should "dispatch onward on 404 and 405 by default" do
  16. cascade = cascade([app1, app2, app3])
  17. Rack::MockRequest.new(cascade).get("/cgi/test").should.be.ok
  18. Rack::MockRequest.new(cascade).get("/foo").should.be.ok
  19. Rack::MockRequest.new(cascade).get("/toobad").should.be.not_found
  20. Rack::MockRequest.new(cascade).get("/cgi/../..").should.be.client_error
  21. # Put is not allowed by Rack::File so it'll 405.
  22. Rack::MockRequest.new(cascade).put("/foo").should.be.ok
  23. end
  24. should "dispatch onward on whatever is passed" do
  25. cascade = cascade([app1, app2, app3], [404, 403])
  26. Rack::MockRequest.new(cascade).get("/cgi/../bla").should.be.not_found
  27. end
  28. should "return 404 if empty" do
  29. Rack::MockRequest.new(cascade([])).get('/').should.be.not_found
  30. end
  31. should "append new app" do
  32. cascade = Rack::Cascade.new([], [404, 403])
  33. Rack::MockRequest.new(cascade).get('/').should.be.not_found
  34. cascade << app2
  35. Rack::MockRequest.new(cascade).get('/cgi/test').should.be.not_found
  36. Rack::MockRequest.new(cascade).get('/cgi/../bla').should.be.not_found
  37. cascade << app1
  38. Rack::MockRequest.new(cascade).get('/cgi/test').should.be.ok
  39. Rack::MockRequest.new(cascade).get('/cgi/../..').should.be.client_error
  40. Rack::MockRequest.new(cascade).get('/foo').should.be.not_found
  41. cascade << app3
  42. Rack::MockRequest.new(cascade).get('/foo').should.be.ok
  43. end
  44. end