Permalink
Browse files

Automatically start the test server when specs are run

  • Loading branch information...
1 parent 746bdda commit c28e98b7c20bbe72f8bab576b35c559bbadc495c @TwP TwP committed Nov 18, 2011
Showing with 43 additions and 18 deletions.
  1. +4 −0 spec/spec_helper.rb
  2. +39 −18 script/test_server → spec/support/test_server.rb
View
@@ -26,3 +26,7 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__) + '/../ext')
require 'patron'
+
+Dir['./spec/support/**/*.rb'].each { |fn| require fn }
+
+Patron::TestServer.start
View
@@ -1,4 +1,3 @@
-#!/usr/bin/env ruby
## -------------------------------------------------------------------
##
## Patron HTTP Client: HTTP test server for integration tests
@@ -124,23 +123,45 @@ def do_GET(req, res)
end
end
-server = WEBrick::HTTPServer.new :Port => 9001
-server.mount("/test", TestServlet)
-server.mount("/testpost", TestPostBodyServlet)
-server.mount("/timeout", TimeoutServlet)
-server.mount("/redirect", RedirectServlet)
-server.mount("/picture", PictureServlet)
-server.mount("/setcookie", SetCookieServlet)
-server.mount("/repetitiveheader", RepetitiveHeaderServlet)
-server.mount("/wrongcontentlength", WrongContentLengthServlet)
-
-exit_code = lambda do
- begin
- server.shutdown unless server.nil?
- rescue Object => e
- puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
+class Patron::TestServer
+
+ def self.start( log_file = nil )
+ new(log_file).start
+ end
+
+ def initialize( log_file = nil )
+ log_file ||= StringIO.new
+ log = WEBrick::Log.new(log_file)
+
+ @server = WEBrick::HTTPServer.new(
+ :Port => 9001,
+ :Logger => log,
+ :AccessLog => [
+ [ log, WEBrick::AccessLog::COMMON_LOG_FORMAT ],
+ [ log, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
+ ]
+ )
+ @server.mount("/test", TestServlet)
+ @server.mount("/testpost", TestPostBodyServlet)
+ @server.mount("/timeout", TimeoutServlet)
+ @server.mount("/redirect", RedirectServlet)
+ @server.mount("/picture", PictureServlet)
+ @server.mount("/setcookie", SetCookieServlet)
+ @server.mount("/repetitiveheader", RepetitiveHeaderServlet)
+ @server.mount("/wrongcontentlength", WrongContentLengthServlet)
+ end
+
+ def start
+ trap('INT') {
+ begin
+ @server.shutdown unless @server.nil?
+ rescue Object => e
+ STDERR.puts "Error #{__FILE__}:#{__LINE__}\n#{e.message}"
+ end
+ }
+
+ Thread.new { @server.start }
+ Thread.pass
end
end
-trap("INT"){exit_code.call}
-server.start

0 comments on commit c28e98b

Please sign in to comment.