spec_recursive.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rack/recursive'
  2. require 'rack/mock'
  3. describe Rack::Recursive do
  4. @app1 = lambda { |env|
  5. res = Rack::Response.new
  6. res["X-Path-Info"] = env["PATH_INFO"]
  7. res["X-Query-String"] = env["QUERY_STRING"]
  8. res.finish do |inner_res|
  9. inner_res.write "App1"
  10. end
  11. }
  12. @app2 = lambda { |env|
  13. Rack::Response.new.finish do |res|
  14. res.write "App2"
  15. _, _, body = env['rack.recursive.include'].call(env, "/app1")
  16. body.each { |b|
  17. res.write b
  18. }
  19. end
  20. }
  21. @app3 = lambda { |env|
  22. raise Rack::ForwardRequest.new("/app1")
  23. }
  24. @app4 = lambda { |env|
  25. raise Rack::ForwardRequest.new("http://example.org/app1/quux?meh")
  26. }
  27. should "allow for subrequests" do
  28. res = Rack::MockRequest.new(Rack::Recursive.new(
  29. Rack::URLMap.new("/app1" => @app1,
  30. "/app2" => @app2))).
  31. get("/app2")
  32. res.should.be.ok
  33. res.body.should.equal "App2App1"
  34. end
  35. should "raise error on requests not below the app" do
  36. app = Rack::URLMap.new("/app1" => @app1,
  37. "/app" => Rack::Recursive.new(
  38. Rack::URLMap.new("/1" => @app1,
  39. "/2" => @app2)))
  40. lambda {
  41. Rack::MockRequest.new(app).get("/app/2")
  42. }.should.raise(ArgumentError).
  43. message.should =~ /can only include below/
  44. end
  45. should "support forwarding" do
  46. app = Rack::Recursive.new(Rack::URLMap.new("/app1" => @app1,
  47. "/app3" => @app3,
  48. "/app4" => @app4))
  49. res = Rack::MockRequest.new(app).get("/app3")
  50. res.should.be.ok
  51. res.body.should.equal "App1"
  52. res = Rack::MockRequest.new(app).get("/app4")
  53. res.should.be.ok
  54. res.body.should.equal "App1"
  55. res["X-Path-Info"].should.equal "/quux"
  56. res["X-Query-String"].should.equal "meh"
  57. end
  58. end