spec_server.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. it "overrides :config if :app is passed in" do
  11. server = Rack::Server.new(:app => "FOO")
  12. server.app.should == "FOO"
  13. end
  14. should "not include Rack::Lint in deployment or none environments" do
  15. server = Rack::Server.new(:app => 'foo')
  16. server.middleware['deployment'].flatten.should.not.include(Rack::Lint)
  17. server.middleware['none'].flatten.should.not.include(Rack::Lint)
  18. end
  19. should "not include Rack::ShowExceptions in deployment or none environments" do
  20. server = Rack::Server.new(:app => 'foo')
  21. server.middleware['deployment'].flatten.should.not.include(Rack::ShowExceptions)
  22. server.middleware['none'].flatten.should.not.include(Rack::ShowExceptions)
  23. end
  24. should "support CGI" do
  25. begin
  26. o, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], 'foo'
  27. server = Rack::Server.new(:app => 'foo')
  28. server.server.name =~ /CGI/
  29. Rack::Server.logging_middleware.call(server).should.eql(nil)
  30. ensure
  31. ENV['REQUEST_METHOD'] = o
  32. end
  33. end
  34. should "not force any middleware under the none configuration" do
  35. server = Rack::Server.new(:app => 'foo')
  36. server.middleware['none'].should.be.empty
  37. end
  38. should "use a full path to the pidfile" do
  39. # avoids issues with daemonize chdir
  40. opts = Rack::Server.new.send(:parse_options, %w[--pid testing.pid])
  41. opts[:pid].should.eql(::File.expand_path('testing.pid'))
  42. end
  43. should "run a server" do
  44. pidfile = Tempfile.open('pidfile') { |f| break f }.path
  45. FileUtils.rm pidfile
  46. server = Rack::Server.new(
  47. :app => app,
  48. :environment => 'none',
  49. :pid => pidfile,
  50. :Port => TCPServer.open('127.0.0.1', 0){|s| s.addr[1] },
  51. :Host => '127.0.0.1',
  52. :daemonize => false,
  53. :server => 'webrick'
  54. )
  55. t = Thread.new { server.start { |s| Thread.current[:server] = s } }
  56. t.join(0.01) until t[:server] && t[:server].status != :Stop
  57. body = open("http://127.0.0.1:#{server.options[:Port]}/") { |f| f.read }
  58. body.should.eql('success')
  59. Process.kill(:INT, $$)
  60. t.join
  61. open(pidfile) { |f| f.read.should.eql $$.to_s }
  62. end
  63. end