spec_mongrel.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. begin
  2. require 'rack'
  3. require 'rack/handler/mongrel'
  4. require File.expand_path('../testrequest', __FILE__)
  5. require 'timeout'
  6. Thread.abort_on_exception = true
  7. $tcp_defer_accept_opts = nil
  8. $tcp_cork_opts = nil
  9. describe Rack::Handler::Mongrel do
  10. extend TestRequest::Helpers
  11. @server = Mongrel::HttpServer.new(@host='127.0.0.1', @port=9201)
  12. @server.register('/test',
  13. Rack::Handler::Mongrel.new(Rack::Lint.new(TestRequest.new)))
  14. @server.register('/stream',
  15. Rack::Handler::Mongrel.new(Rack::Lint.new(StreamingRequest)))
  16. @acc = @server.run
  17. should "respond" do
  18. lambda {
  19. GET("/test")
  20. }.should.not.raise
  21. end
  22. should "be a Mongrel" do
  23. GET("/test")
  24. status.should.equal 200
  25. response["SERVER_SOFTWARE"].should =~ /Mongrel/
  26. response["HTTP_VERSION"].should.equal "HTTP/1.1"
  27. response["SERVER_PROTOCOL"].should.equal "HTTP/1.1"
  28. response["SERVER_PORT"].should.equal "9201"
  29. response["SERVER_NAME"].should.equal "127.0.0.1"
  30. end
  31. should "have rack headers" do
  32. GET("/test")
  33. response["rack.version"].should.equal [1,1]
  34. response["rack.multithread"].should.be.true
  35. response["rack.multiprocess"].should.be.false
  36. response["rack.run_once"].should.be.false
  37. end
  38. should "have CGI headers on GET" do
  39. GET("/test")
  40. response["REQUEST_METHOD"].should.equal "GET"
  41. response["SCRIPT_NAME"].should.equal "/test"
  42. response["REQUEST_PATH"].should.equal "/test"
  43. response["PATH_INFO"].should.be.equal ""
  44. response["QUERY_STRING"].should.equal ""
  45. response["test.postdata"].should.equal ""
  46. GET("/test/foo?quux=1")
  47. response["REQUEST_METHOD"].should.equal "GET"
  48. response["SCRIPT_NAME"].should.equal "/test"
  49. response["REQUEST_PATH"].should.equal "/test/foo"
  50. response["PATH_INFO"].should.equal "/foo"
  51. response["QUERY_STRING"].should.equal "quux=1"
  52. end
  53. should "have CGI headers on POST" do
  54. POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'})
  55. status.should.equal 200
  56. response["REQUEST_METHOD"].should.equal "POST"
  57. response["SCRIPT_NAME"].should.equal "/test"
  58. response["REQUEST_PATH"].should.equal "/test"
  59. response["QUERY_STRING"].should.equal ""
  60. response["HTTP_X_TEST_HEADER"].should.equal "42"
  61. response["test.postdata"].should.equal "rack-form-data=23"
  62. end
  63. should "support HTTP auth" do
  64. GET("/test", {:user => "ruth", :passwd => "secret"})
  65. response["HTTP_AUTHORIZATION"].should.equal "Basic cnV0aDpzZWNyZXQ="
  66. end
  67. should "set status" do
  68. GET("/test?secret")
  69. status.should.equal 403
  70. response["rack.url_scheme"].should.equal "http"
  71. end
  72. should "provide a .run" do
  73. block_ran = false
  74. Thread.new {
  75. Rack::Handler::Mongrel.run(lambda {}, {:Host => '127.0.0.1', :Port => 9211}) { |server|
  76. server.should.be.kind_of Mongrel::HttpServer
  77. block_ran = true
  78. }
  79. }
  80. sleep 1
  81. block_ran.should.be.true
  82. end
  83. should "provide a .run that maps a hash" do
  84. block_ran = false
  85. Thread.new {
  86. map = {'/'=>lambda{},'/foo'=>lambda{}}
  87. Rack::Handler::Mongrel.run(map, :map => true, :Host => '127.0.0.1', :Port => 9221) { |server|
  88. server.should.be.kind_of Mongrel::HttpServer
  89. server.classifier.uris.size.should.equal 2
  90. server.classifier.uris.should.not.include '/arf'
  91. server.classifier.uris.should.include '/'
  92. server.classifier.uris.should.include '/foo'
  93. block_ran = true
  94. }
  95. }
  96. sleep 1
  97. block_ran.should.be.true
  98. end
  99. should "provide a .run that maps a urlmap" do
  100. block_ran = false
  101. Thread.new {
  102. map = Rack::URLMap.new({'/'=>lambda{},'/bar'=>lambda{}})
  103. Rack::Handler::Mongrel.run(map, {:map => true, :Host => '127.0.0.1', :Port => 9231}) { |server|
  104. server.should.be.kind_of Mongrel::HttpServer
  105. server.classifier.uris.size.should.equal 2
  106. server.classifier.uris.should.not.include '/arf'
  107. server.classifier.uris.should.include '/'
  108. server.classifier.uris.should.include '/bar'
  109. block_ran = true
  110. }
  111. }
  112. sleep 1
  113. block_ran.should.be.true
  114. end
  115. should "provide a .run that maps a urlmap restricting by host" do
  116. block_ran = false
  117. Thread.new {
  118. map = Rack::URLMap.new({
  119. '/' => lambda{},
  120. '/foo' => lambda{},
  121. '/bar' => lambda{},
  122. 'http://127.0.0.1/' => lambda{},
  123. 'http://127.0.0.1/bar' => lambda{},
  124. 'http://falsehost/arf' => lambda{},
  125. 'http://falsehost/qux' => lambda{}
  126. })
  127. opt = {:map => true, :Port => 9241, :Host => '127.0.0.1'}
  128. Rack::Handler::Mongrel.run(map, opt) { |server|
  129. server.should.be.kind_of Mongrel::HttpServer
  130. server.classifier.uris.should.include '/'
  131. server.classifier.handler_map['/'].size.should.equal 2
  132. server.classifier.uris.should.include '/foo'
  133. server.classifier.handler_map['/foo'].size.should.equal 1
  134. server.classifier.uris.should.include '/bar'
  135. server.classifier.handler_map['/bar'].size.should.equal 2
  136. server.classifier.uris.should.not.include '/qux'
  137. server.classifier.uris.should.not.include '/arf'
  138. server.classifier.uris.size.should.equal 3
  139. block_ran = true
  140. }
  141. }
  142. sleep 1
  143. block_ran.should.be.true
  144. end
  145. should "stream #each part of the response" do
  146. body = ''
  147. begin
  148. Timeout.timeout(1) do
  149. Net::HTTP.start(@host, @port) do |http|
  150. get = Net::HTTP::Get.new('/stream')
  151. http.request(get) do |response|
  152. response.read_body { |part| body << part }
  153. end
  154. end
  155. end
  156. rescue Timeout::Error
  157. end
  158. body.should.not.be.empty
  159. end
  160. @acc.raise Mongrel::StopServer
  161. end
  162. rescue LoadError
  163. warn "Skipping Rack::Handler::Mongrel tests (Mongrel is required). `gem install mongrel` and try again."
  164. end