PageRenderTime 42ms CodeModel.GetById 15ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

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