/vendor/bundle/jruby/2.1/gems/rack-1.5.2/test/spec_server.rb

https://github.com/delowong/logstash · Ruby · 143 lines · 123 code · 19 blank · 1 comment · 3 complexity · 74d6f3996abfdbb23bb9e580158ec0a3 MD5 · raw file

  1. require 'rack'
  2. require 'rack/server'
  3. require 'tempfile'
  4. require 'socket'
  5. require 'open-uri'
  6. describe Rack::Server do
  7. def app
  8. lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
  9. end
  10. def with_stderr
  11. old, $stderr = $stderr, StringIO.new
  12. yield $stderr
  13. ensure
  14. $stderr = old
  15. end
  16. it "overrides :config if :app is passed in" do
  17. server = Rack::Server.new(:app => "FOO")
  18. server.app.should.equal "FOO"
  19. end
  20. should "prefer to use :builder when it is passed in" do
  21. server = Rack::Server.new(:builder => "run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }")
  22. server.app.class.should.equal Proc
  23. Rack::MockRequest.new(server.app).get("/").body.to_s.should.equal 'success'
  24. end
  25. should "not include Rack::Lint in deployment or none environments" do
  26. server = Rack::Server.new(:app => 'foo')
  27. server.middleware['deployment'].flatten.should.not.include(Rack::Lint)
  28. server.middleware['none'].flatten.should.not.include(Rack::Lint)
  29. end
  30. should "not include Rack::ShowExceptions in deployment or none environments" do
  31. server = Rack::Server.new(:app => 'foo')
  32. server.middleware['deployment'].flatten.should.not.include(Rack::ShowExceptions)
  33. server.middleware['none'].flatten.should.not.include(Rack::ShowExceptions)
  34. end
  35. should "support CGI" do
  36. begin
  37. o, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], 'foo'
  38. server = Rack::Server.new(:app => 'foo')
  39. server.server.name =~ /CGI/
  40. Rack::Server.logging_middleware.call(server).should.eql(nil)
  41. ensure
  42. ENV['REQUEST_METHOD'] = o
  43. end
  44. end
  45. should "not force any middleware under the none configuration" do
  46. server = Rack::Server.new(:app => 'foo')
  47. server.middleware['none'].should.be.empty
  48. end
  49. should "use a full path to the pidfile" do
  50. # avoids issues with daemonize chdir
  51. opts = Rack::Server.new.send(:parse_options, %w[--pid testing.pid])
  52. opts[:pid].should.eql(::File.expand_path('testing.pid'))
  53. end
  54. should "run a server" do
  55. pidfile = Tempfile.open('pidfile') { |f| break f }.path
  56. FileUtils.rm pidfile
  57. server = Rack::Server.new(
  58. :app => app,
  59. :environment => 'none',
  60. :pid => pidfile,
  61. :Port => TCPServer.open('127.0.0.1', 0){|s| s.addr[1] },
  62. :Host => '127.0.0.1',
  63. :daemonize => false,
  64. :server => 'webrick'
  65. )
  66. t = Thread.new { server.start { |s| Thread.current[:server] = s } }
  67. t.join(0.01) until t[:server] && t[:server].status != :Stop
  68. body = open("http://127.0.0.1:#{server.options[:Port]}/") { |f| f.read }
  69. body.should.eql('success')
  70. Process.kill(:INT, $$)
  71. t.join
  72. open(pidfile) { |f| f.read.should.eql $$.to_s }
  73. end
  74. should "check pid file presence and running process" do
  75. pidfile = Tempfile.open('pidfile') { |f| f.write($$); break f }.path
  76. server = Rack::Server.new(:pid => pidfile)
  77. server.send(:pidfile_process_status).should.eql :running
  78. end
  79. should "check pid file presence and dead process" do
  80. dead_pid = `echo $$`.to_i
  81. pidfile = Tempfile.open('pidfile') { |f| f.write(dead_pid); break f }.path
  82. server = Rack::Server.new(:pid => pidfile)
  83. server.send(:pidfile_process_status).should.eql :dead
  84. end
  85. should "check pid file presence and exited process" do
  86. pidfile = Tempfile.open('pidfile') { |f| break f }.path
  87. ::File.delete(pidfile)
  88. server = Rack::Server.new(:pid => pidfile)
  89. server.send(:pidfile_process_status).should.eql :exited
  90. end
  91. should "check pid file presence and not owned process" do
  92. pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
  93. server = Rack::Server.new(:pid => pidfile)
  94. server.send(:pidfile_process_status).should.eql :not_owned
  95. end
  96. should "not write pid file when it is created after check" do
  97. pidfile = Tempfile.open('pidfile') { |f| break f }.path
  98. ::File.delete(pidfile)
  99. server = Rack::Server.new(:pid => pidfile)
  100. ::File.open(pidfile, 'w') { |f| f.write(1) }
  101. with_stderr do |err|
  102. should.raise(SystemExit) do
  103. server.send(:write_pid)
  104. end
  105. err.rewind
  106. output = err.read
  107. output.should.match(/already running/)
  108. output.should.include? pidfile
  109. end
  110. end
  111. should "inform the user about existing pidfiles with running processes" do
  112. pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
  113. server = Rack::Server.new(:pid => pidfile)
  114. with_stderr do |err|
  115. should.raise(SystemExit) do
  116. server.start
  117. end
  118. err.rewind
  119. output = err.read
  120. output.should.match(/already running/)
  121. output.should.include? pidfile
  122. end
  123. end
  124. end