File: spec_rack_recursive.rb

package info (click to toggle)
librack-ruby 1.1.0-4%2Bsqueeze3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 3,204 kB
  • ctags: 1,101
  • sloc: ruby: 8,704; makefile: 9
file content (77 lines) | stat: -rw-r--r-- 2,122 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'test/spec'

require 'rack/recursive'
require 'rack/urlmap'
require 'rack/response'
require 'rack/mock'

context "Rack::Recursive" do
  setup do

    @app1 = lambda { |env|
      res = Rack::Response.new
      res["X-Path-Info"] = env["PATH_INFO"]
      res["X-Query-String"] = env["QUERY_STRING"]
      res.finish do |res|
        res.write "App1"
      end
    }

    @app2 = lambda { |env|
      Rack::Response.new.finish do |res|
        res.write "App2"
        _, _, body = env['rack.recursive.include'].call(env, "/app1")
        body.each { |b|
          res.write b
        }
      end
    }

    @app3 = lambda { |env|
      raise Rack::ForwardRequest.new("/app1")
    }

    @app4 = lambda { |env|
      raise Rack::ForwardRequest.new("http://example.org/app1/quux?meh")
    }

  end

  specify "should allow for subrequests" do
    res = Rack::MockRequest.new(Rack::Recursive.new(
                                  Rack::URLMap.new("/app1" => @app1,
                                                   "/app2" => @app2))).
      get("/app2")

    res.should.be.ok
    res.body.should.equal "App2App1"    
  end

  specify "should raise error on requests not below the app" do
    app = Rack::URLMap.new("/app1" => @app1,
                           "/app" => Rack::Recursive.new(
                              Rack::URLMap.new("/1" => @app1,
                                               "/2" => @app2)))

    lambda {
      Rack::MockRequest.new(app).get("/app/2")
    }.should.raise(ArgumentError).
      message.should =~ /can only include below/
  end

  specify "should support forwarding" do
    app = Rack::Recursive.new(Rack::URLMap.new("/app1" => @app1,
                                               "/app3" => @app3,
                                               "/app4" => @app4))

    res = Rack::MockRequest.new(app).get("/app3")
    res.should.be.ok
    res.body.should.equal "App1"

    res = Rack::MockRequest.new(app).get("/app4")
    res.should.be.ok
    res.body.should.equal "App1"
    res["X-Path-Info"].should.equal "/quux"
    res["X-Query-String"].should.equal "meh"
  end
end