File: spec_server.rb

package info (click to toggle)
ruby-rack 1.4.1-2.1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,312 kB
  • sloc: ruby: 11,357; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'rack'
require 'rack/server'
require 'tempfile'
require 'socket'
require 'open-uri'

describe Rack::Server do

  def app
    lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
  end

  it "overrides :config if :app is passed in" do
    server = Rack::Server.new(:app => "FOO")
    server.app.should == "FOO"
  end

  should "not include Rack::Lint in deployment or none environments" do
    server = Rack::Server.new(:app => 'foo')
    server.middleware['deployment'].flatten.should.not.include(Rack::Lint)
    server.middleware['none'].flatten.should.not.include(Rack::Lint)
  end

  should "not include Rack::ShowExceptions in deployment or none environments" do
    server = Rack::Server.new(:app => 'foo')
    server.middleware['deployment'].flatten.should.not.include(Rack::ShowExceptions)
    server.middleware['none'].flatten.should.not.include(Rack::ShowExceptions)
  end

  should "support CGI" do
    begin
      o, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], 'foo'
      server = Rack::Server.new(:app => 'foo')
      server.server.name =~ /CGI/
      Rack::Server.logging_middleware.call(server).should.eql(nil)
    ensure
      ENV['REQUEST_METHOD'] = o
    end
  end

  should "not force any middleware under the none configuration" do
    server = Rack::Server.new(:app => 'foo')
    server.middleware['none'].should.be.empty
  end

  should "use a full path to the pidfile" do
    # avoids issues with daemonize chdir
    opts = Rack::Server.new.send(:parse_options, %w[--pid testing.pid])
    opts[:pid].should.eql(::File.expand_path('testing.pid'))
  end

  should "run a server" do
    pidfile = Tempfile.open('pidfile') { |f| break f }.path
    FileUtils.rm pidfile
    server = Rack::Server.new(
      :app         => app,
      :environment => 'none',
      :pid         => pidfile,
      :Port        => TCPServer.open('127.0.0.1', 0){|s| s.addr[1] },
      :Host        => '127.0.0.1',
      :daemonize   => false,
      :server      => 'webrick'
    )
    t = Thread.new { server.start { |s| Thread.current[:server] = s } }
    t.join(0.01) until t[:server] && t[:server].status != :Stop
    body = open("http://127.0.0.1:#{server.options[:Port]}/") { |f| f.read }
    body.should.eql('success')

    Process.kill(:INT, $$)
    t.join
    open(pidfile) { |f| f.read.should.eql $$.to_s }
  end

end