spec_urlmap.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. require 'rack/urlmap'
  2. require 'rack/mock'
  3. describe Rack::URLMap do
  4. it "dispatches paths correctly" do
  5. app = lambda { |env|
  6. [200, {
  7. 'X-ScriptName' => env['SCRIPT_NAME'],
  8. 'X-PathInfo' => env['PATH_INFO'],
  9. 'Content-Type' => 'text/plain'
  10. }, [""]]
  11. }
  12. map = Rack::URLMap.new({
  13. 'http://foo.org/bar' => app,
  14. '/foo' => app,
  15. '/foo/bar' => app
  16. })
  17. res = Rack::MockRequest.new(map).get("/")
  18. res.should.be.not_found
  19. res = Rack::MockRequest.new(map).get("/qux")
  20. res.should.be.not_found
  21. res = Rack::MockRequest.new(map).get("/foo")
  22. res.should.be.ok
  23. res["X-ScriptName"].should.equal "/foo"
  24. res["X-PathInfo"].should.equal ""
  25. res = Rack::MockRequest.new(map).get("/foo/")
  26. res.should.be.ok
  27. res["X-ScriptName"].should.equal "/foo"
  28. res["X-PathInfo"].should.equal "/"
  29. res = Rack::MockRequest.new(map).get("/foo/bar")
  30. res.should.be.ok
  31. res["X-ScriptName"].should.equal "/foo/bar"
  32. res["X-PathInfo"].should.equal ""
  33. res = Rack::MockRequest.new(map).get("/foo/bar/")
  34. res.should.be.ok
  35. res["X-ScriptName"].should.equal "/foo/bar"
  36. res["X-PathInfo"].should.equal "/"
  37. res = Rack::MockRequest.new(map).get("/foo///bar//quux")
  38. res.status.should.equal 200
  39. res.should.be.ok
  40. res["X-ScriptName"].should.equal "/foo/bar"
  41. res["X-PathInfo"].should.equal "//quux"
  42. res = Rack::MockRequest.new(map).get("/foo/quux", "SCRIPT_NAME" => "/bleh")
  43. res.should.be.ok
  44. res["X-ScriptName"].should.equal "/bleh/foo"
  45. res["X-PathInfo"].should.equal "/quux"
  46. res = Rack::MockRequest.new(map).get("/bar", 'HTTP_HOST' => 'foo.org')
  47. res.should.be.ok
  48. res["X-ScriptName"].should.equal "/bar"
  49. res["X-PathInfo"].should.be.empty
  50. res = Rack::MockRequest.new(map).get("/bar/", 'HTTP_HOST' => 'foo.org')
  51. res.should.be.ok
  52. res["X-ScriptName"].should.equal "/bar"
  53. res["X-PathInfo"].should.equal '/'
  54. end
  55. it "dispatches hosts correctly" do
  56. map = Rack::URLMap.new("http://foo.org/" => lambda { |env|
  57. [200,
  58. { "Content-Type" => "text/plain",
  59. "X-Position" => "foo.org",
  60. "X-Host" => env["HTTP_HOST"] || env["SERVER_NAME"],
  61. }, [""]]},
  62. "http://subdomain.foo.org/" => lambda { |env|
  63. [200,
  64. { "Content-Type" => "text/plain",
  65. "X-Position" => "subdomain.foo.org",
  66. "X-Host" => env["HTTP_HOST"] || env["SERVER_NAME"],
  67. }, [""]]},
  68. "http://bar.org/" => lambda { |env|
  69. [200,
  70. { "Content-Type" => "text/plain",
  71. "X-Position" => "bar.org",
  72. "X-Host" => env["HTTP_HOST"] || env["SERVER_NAME"],
  73. }, [""]]},
  74. "/" => lambda { |env|
  75. [200,
  76. { "Content-Type" => "text/plain",
  77. "X-Position" => "default.org",
  78. "X-Host" => env["HTTP_HOST"] || env["SERVER_NAME"],
  79. }, [""]]}
  80. )
  81. res = Rack::MockRequest.new(map).get("/")
  82. res.should.be.ok
  83. res["X-Position"].should.equal "default.org"
  84. res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "bar.org")
  85. res.should.be.ok
  86. res["X-Position"].should.equal "bar.org"
  87. res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "foo.org")
  88. res.should.be.ok
  89. res["X-Position"].should.equal "foo.org"
  90. res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "subdomain.foo.org", "SERVER_NAME" => "foo.org")
  91. res.should.be.ok
  92. res["X-Position"].should.equal "subdomain.foo.org"
  93. res = Rack::MockRequest.new(map).get("http://foo.org/")
  94. res.should.be.ok
  95. res["X-Position"].should.equal "default.org"
  96. res = Rack::MockRequest.new(map).get("/", "HTTP_HOST" => "example.org")
  97. res.should.be.ok
  98. res["X-Position"].should.equal "default.org"
  99. res = Rack::MockRequest.new(map).get("/",
  100. "HTTP_HOST" => "example.org:9292",
  101. "SERVER_PORT" => "9292")
  102. res.should.be.ok
  103. res["X-Position"].should.equal "default.org"
  104. end
  105. should "be nestable" do
  106. map = Rack::URLMap.new("/foo" =>
  107. Rack::URLMap.new("/bar" =>
  108. Rack::URLMap.new("/quux" => lambda { |env|
  109. [200,
  110. { "Content-Type" => "text/plain",
  111. "X-Position" => "/foo/bar/quux",
  112. "X-PathInfo" => env["PATH_INFO"],
  113. "X-ScriptName" => env["SCRIPT_NAME"],
  114. }, [""]]}
  115. )))
  116. res = Rack::MockRequest.new(map).get("/foo/bar")
  117. res.should.be.not_found
  118. res = Rack::MockRequest.new(map).get("/foo/bar/quux")
  119. res.should.be.ok
  120. res["X-Position"].should.equal "/foo/bar/quux"
  121. res["X-PathInfo"].should.equal ""
  122. res["X-ScriptName"].should.equal "/foo/bar/quux"
  123. end
  124. should "route root apps correctly" do
  125. map = Rack::URLMap.new("/" => lambda { |env|
  126. [200,
  127. { "Content-Type" => "text/plain",
  128. "X-Position" => "root",
  129. "X-PathInfo" => env["PATH_INFO"],
  130. "X-ScriptName" => env["SCRIPT_NAME"]
  131. }, [""]]},
  132. "/foo" => lambda { |env|
  133. [200,
  134. { "Content-Type" => "text/plain",
  135. "X-Position" => "foo",
  136. "X-PathInfo" => env["PATH_INFO"],
  137. "X-ScriptName" => env["SCRIPT_NAME"]
  138. }, [""]]}
  139. )
  140. res = Rack::MockRequest.new(map).get("/foo/bar")
  141. res.should.be.ok
  142. res["X-Position"].should.equal "foo"
  143. res["X-PathInfo"].should.equal "/bar"
  144. res["X-ScriptName"].should.equal "/foo"
  145. res = Rack::MockRequest.new(map).get("/foo")
  146. res.should.be.ok
  147. res["X-Position"].should.equal "foo"
  148. res["X-PathInfo"].should.equal ""
  149. res["X-ScriptName"].should.equal "/foo"
  150. res = Rack::MockRequest.new(map).get("/bar")
  151. res.should.be.ok
  152. res["X-Position"].should.equal "root"
  153. res["X-PathInfo"].should.equal "/bar"
  154. res["X-ScriptName"].should.equal ""
  155. res = Rack::MockRequest.new(map).get("")
  156. res.should.be.ok
  157. res["X-Position"].should.equal "root"
  158. res["X-PathInfo"].should.equal "/"
  159. res["X-ScriptName"].should.equal ""
  160. end
  161. should "not squeeze slashes" do
  162. map = Rack::URLMap.new("/" => lambda { |env|
  163. [200,
  164. { "Content-Type" => "text/plain",
  165. "X-Position" => "root",
  166. "X-PathInfo" => env["PATH_INFO"],
  167. "X-ScriptName" => env["SCRIPT_NAME"]
  168. }, [""]]},
  169. "/foo" => lambda { |env|
  170. [200,
  171. { "Content-Type" => "text/plain",
  172. "X-Position" => "foo",
  173. "X-PathInfo" => env["PATH_INFO"],
  174. "X-ScriptName" => env["SCRIPT_NAME"]
  175. }, [""]]}
  176. )
  177. res = Rack::MockRequest.new(map).get("/http://example.org/bar")
  178. res.should.be.ok
  179. res["X-Position"].should.equal "root"
  180. res["X-PathInfo"].should.equal "/http://example.org/bar"
  181. res["X-ScriptName"].should.equal ""
  182. end
  183. end