summary refs log tree commit
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-11-21 10:30:58 -0800
committerJoshua Peek <josh@joshpeek.com>2009-11-21 10:30:58 -0800
commit87957c6c76f4f1cc9310667f71a4c15a3f35eb23 (patch)
tree67c416a2467ce0b567814f6e63384cc066a9df07
parentc765e7b99f569c170f07431a6831541755e271f6 (diff)
downloadrack-87957c6c76f4f1cc9310667f71a4c15a3f35eb23.tar.gz
Test coverage for rackup
Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r--test/rackup/config.ru25
-rw-r--r--test/spec_rackup.rb126
-rw-r--r--test/testrequest.rb17
3 files changed, 167 insertions, 1 deletions
diff --git a/test/rackup/config.ru b/test/rackup/config.ru
new file mode 100644
index 00000000..2490c6ed
--- /dev/null
+++ b/test/rackup/config.ru
@@ -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
diff --git a/test/spec_rackup.rb b/test/spec_rackup.rb
new file mode 100644
index 00000000..99f48e3a
--- /dev/null
+++ b/test/spec_rackup.rb
@@ -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 \ No newline at end of file
diff --git a/test/testrequest.rb b/test/testrequest.rb
index 7b7190cb..0da2b126 100644
--- a/test/testrequest.rb
+++ b/test/testrequest.rb
@@ -13,6 +13,17 @@ class TestRequest
   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 @@ class TestRequest
         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