Skip to content

Commit

Permalink
add tests for cascade mounts, closes #796
Browse files Browse the repository at this point in the history
  • Loading branch information
ujifgc committed Oct 23, 2013
1 parent 79d8dd9 commit 0f37919
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion padrino-core/test/test_router.rb
Expand Up @@ -17,8 +17,8 @@ def setup
}
map = Padrino::Router.new(
{ :path => '/bar', :to => app },
{ :path => '/foo/bar', :to => app },
{ :path => '/foo', :to => app },
{ :path => '/foo/bar', :to => app }
)

res = Rack::MockRequest.new(map).get("/")
Expand Down Expand Up @@ -69,6 +69,57 @@ def setup
assert_equal "/", res["X-PathInfo"]
end

should "dispatch requests to cascade mounted apps" do
app = lambda { |env|
scary = !!env['PATH_INFO'].match(/scary/)
[scary ? 404 : 200, {
'X-ScriptName' => env['SCRIPT_NAME'],
'X-PathInfo' => env['PATH_INFO'],
'Content-Type' => 'text/plain'
}, [""]]
}
api = lambda { |env|
spooky = !!env['QUERY_STRING'].match(/spooky/)
[spooky ? 200 : 404, {
'X-API' => spooky,
'X-ScriptName' => env['SCRIPT_NAME'],
'X-PathInfo' => env['PATH_INFO'],
'Content-Type' => 'application/json'
}, [""]]
}
map = Padrino::Router.new(
{ :path => '/bar', :to => api },
{ :path => '/bar', :to => app },
)

res = Rack::MockRequest.new(map).get("/werewolf")
assert_equal 404, res.status
assert_equal nil, res["X-API"]
assert_equal nil, res["X-ScriptName"]
assert_equal nil, res["X-PathInfo"]

res = Rack::MockRequest.new(map).get("/bar/mitzvah")
assert res.ok?
assert_equal nil, res["X-API"]
assert_equal 'text/plain', res["Content-Type"]
assert_equal "/bar", res["X-ScriptName"]
assert_equal "/mitzvah", res["X-PathInfo"]

res = Rack::MockRequest.new(map).get("/bar?spooky")
assert res.ok?
assert_equal true, res["X-API"]
assert_equal 'application/json', res["Content-Type"]
assert_equal "/bar", res["X-ScriptName"]
assert_equal "/", res["X-PathInfo"]

res = Rack::MockRequest.new(map).get("/bar/scary")
assert_equal 404, res.status
assert_equal nil, res["X-API"]
assert_equal 'text/plain', res["Content-Type"]
assert_equal "/bar", res["X-ScriptName"]
assert_equal "/scary", res["X-PathInfo"]
end

should "dispatches hosts correctly" do
map = Padrino::Router.new(
{ :host => "foo.org", :to => lambda { |env|
Expand Down

0 comments on commit 0f37919

Please sign in to comment.