Permalink
Browse files

Add some basic Rack::Server tests, nothing like the oldies, but faster

  • Loading branch information...
1 parent b97d30f commit 923e9339007f1893321480d60423f0176e7028a4 @raggi raggi committed May 3, 2011
Showing with 75 additions and 5 deletions.
  1. +1 −1 Rakefile
  2. +8 −4 lib/rack/server.rb
  3. 0 test/.bacon
  4. +66 −0 test/spec_server.rb
View
@@ -69,7 +69,7 @@ desc "Run all the fast tests"
task :test => 'SPEC' do
opts = ENV['TEST'] || '-a'
specopts = ENV['TESTOPTS'] ||
- "-q -t '^(?!Rack::Adapter|Rack::Session::Memcache|rackup)'"
+ "-q -t '^(?!Rack::Adapter|Rack::Session::Memcache|Rack::Server)'"
sh "bacon -I./lib:./test -w #{opts} #{specopts}"
end
View
@@ -57,7 +57,7 @@ def parse!(args)
}
opts.on("-P", "--pid FILE", "file to store PID (default: rack.pid)") { |f|
- options[:pid] = f
+ options[:pid] = ::File.expand_path(f)
}
opts.separator ""
@@ -173,15 +173,19 @@ def app
end
end
+ def self.logging_middleware
+ lambda { |server|
+ server.server.name =~ /CGI/ ? nil : [Rack::CommonLogger, $stderr]
+ }
+ end
+
def self.middleware
@middleware ||= begin
m = Hash.new {|h,k| h[k] = []}
m["deployment"].concat [
[Rack::ContentLength],
[Rack::Chunked],
- lambda { |server|
- server.server.name =~ /CGI/ ? nil : [Rack::CommonLogger, $stderr]
- }
+ logging_middleware
]
m["development"].concat m["deployment"] + [[Rack::ShowExceptions], [Rack::Lint]]
m
View
No changes.
View
@@ -1,8 +1,74 @@
+require 'rack'
require 'rack/server'
+require 'tempfile'
+require 'socket'
+require 'open-uri'
describe Rack::Server do
+
+ def app
+ lambda { [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 }
+ until t.status == 'sleep'; t.join(0.01) end
+ 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

0 comments on commit 923e933

Please sign in to comment.