Permalink
Browse files

Test coverage for rackup

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent c765e7b commit 87957c6c76f4f1cc9310667f71a4c15a3f35eb23 Yehuda Katz + Carl Lerche committed with josh Nov 21, 2009
Showing with 167 additions and 1 deletion.
  1. +25 −0 test/rackup/config.ru
  2. +126 −0 test/spec_rackup.rb
  3. +16 −1 test/testrequest.rb
View
@@ -0,0 +1,25 @@
+require "#{File.dirname(__FILE__)}/../testrequest"
+
+$stderr = StringIO.new
+
+class EnvMiddleware
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["PATH_INFO"] == "/broken_lint"
+ return [200, {}, ["Broken Lint"]]
+ end
+
+ env["test.$DEBUG"] = $DEBUG
+ env["test.$EVAL"] = BUKKIT if defined?(BUKKIT)
+ env["test.$VERBOSE"] = $VERBOSE
+ env["test.$LOAD_PATH"] = $LOAD_PATH
+ env["test.Ping"] = defined?(Ping)
+ @app.call(env)
+ end
+end
+
+use EnvMiddleware
+run TestRequest.new
View
@@ -0,0 +1,126 @@
+require 'test/spec'
+require 'testrequest'
+require 'open3'
+
+begin
+require "mongrel"
+
+context "rackup" do
+ include TestRequest::Helpers
+
+ def run_rackup(*args)
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ flags = args.first
+ @host = options[:host] || "0.0.0.0"
+ @port = options[:port] || 9292
+
+ Dir.chdir("#{root}/test/rackup") do
+ @rackup = IO.popen("#{Gem.ruby} -S #{rackup} #{flags}")
+ end
+
+ return if options[:port] == false
+
+ # Wait until the server is available
+ begin
+ GET("/")
+ rescue
+ sleep 0.05
+ retry
+ end
+ end
+
+ def output
+ @rackup.read
+ end
+
+ after do
+ Process.kill(9, @rackup.pid) if @rackup
+
+ Dir["#{root}/**/*.pid"].each do |file|
+ Process.kill(9, File.read(file).to_i)
+ File.delete(file)
+ end
+ end
+
+ specify "rackup" do
+ run_rackup
+ response["PATH_INFO"].should.equal '/'
+ response["test.$DEBUG"].should.be false
+ response["test.$EVAL"].should.be nil
+ response["test.$VERBOSE"].should.be false
+ response["test.Ping"].should.be nil
+ response["SERVER_SOFTWARE"].should.not =~ /webrick/
+ end
+
+ specify "rackup --help" do
+ run_rackup "--help", :port => false
+ output.should.match /--port/
+ end
+
+ specify "rackup --port" do
+ run_rackup "--port 9000", :port => 9000
+ response["SERVER_PORT"].should.equal "9000"
+ end
+
+ specify "rackup --debug" do
+ run_rackup "--debug"
+ response["test.$DEBUG"].should.be true
+ end
+
+ specify "rackup --eval" do
+ run_rackup %{--eval "BUKKIT = 'BUKKIT'"}
+ response["test.$EVAL"].should.equal "BUKKIT"
+ end
+
+ specify "rackup --warn" do
+ run_rackup %{--warn}
+ response["test.$VERBOSE"].should.be true
+ end
+
+ specify "rackup --include" do
+ run_rackup %{--include /foo/bar}
+ response["test.$LOAD_PATH"].should.include "/foo/bar"
+ end
+
+ specify "rackup --require" do
+ run_rackup %{--require ping}
+ response["test.Ping"].should.equal "constant"
+ end
+
+ specify "rackup --server" do
+ run_rackup %{--server webrick}
+ response["SERVER_SOFTWARE"].should =~ /webrick/i
+ end
+
+ specify "rackup --host" do
+ run_rackup %{--host 127.0.0.1}, :host => "127.0.0.1"
+ response["REMOTE_ADDR"].should.equal "127.0.0.1"
+ end
+
+ specify "rackup --pid" do
+ run_rackup %{--daemonize --pid testing.pid}
+ status.should.be 200
+ @rackup.should.be.eof?
+ Dir["#{root}/**/testing.pid"].should.not.be.empty?
+ end
+
+ specify "rackup --version" do
+ run_rackup %{--version}, :port => false
+ output.should =~ /1.0/
+ end
+
+ specify "rackup --env development includes lint" do
+ run_rackup
+ GET("/broken_lint")
+ status.should.be 500
+ end
+
+ specify "rackup --env" do
+ run_rackup %{--env deployment}
+ GET("/broken_lint")
+ status.should.be 200
+ end
+end
+rescue LoadError
+ $stderr.puts "Skipping rackup --server tests (mongrel is required). `gem install thin` and try again."
+end
View
@@ -13,6 +13,17 @@ def call(env)
module Helpers
attr_reader :status, :response
+ ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
+ ENV["RUBYOPT"] = "-I#{ROOT}/lib -rubygems"
+
+ def root
+ ROOT
+ end
+
+ def rackup
+ "#{ROOT}/bin/rackup"
+ end
+
def GET(path, header={})
Net::HTTP.start(@host, @port) { |http|
user = header.delete(:user)
@@ -22,7 +33,11 @@ def GET(path, header={})
get.basic_auth user, passwd if user && passwd
http.request(get) { |response|
@status = response.code.to_i
- @response = YAML.load(response.body)
+ begin
+ @response = YAML.load(response.body)
+ rescue ArgumentError
+ @response = nil
+ end
}
}
end

0 comments on commit 87957c6

Please sign in to comment.