spec_cgi.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. begin
  2. require File.expand_path('../testrequest', __FILE__)
  3. require 'rack/handler/cgi'
  4. describe Rack::Handler::CGI do
  5. extend TestRequest::Helpers
  6. @host = '127.0.0.1'
  7. @port = 9203
  8. if `which lighttpd` && !$?.success?
  9. raise "lighttpd not found"
  10. end
  11. # Keep this first.
  12. $pid = fork {
  13. ENV['RACK_ENV'] = 'deployment'
  14. ENV['RUBYLIB'] = [
  15. File.expand_path('../../lib', __FILE__),
  16. ENV['RUBYLIB'],
  17. ].compact.join(':')
  18. Dir.chdir(File.expand_path("../cgi", __FILE__)) do
  19. exec "lighttpd -D -f lighttpd.conf"
  20. end
  21. }
  22. should "respond" do
  23. sleep 1
  24. GET("/test")
  25. response.should.not.be.nil
  26. end
  27. should "be a lighttpd" do
  28. GET("/test")
  29. status.should.equal 200
  30. response["SERVER_SOFTWARE"].should =~ /lighttpd/
  31. response["HTTP_VERSION"].should.equal "HTTP/1.1"
  32. response["SERVER_PROTOCOL"].should.equal "HTTP/1.1"
  33. response["SERVER_PORT"].should.equal @port.to_s
  34. response["SERVER_NAME"].should.equal @host
  35. end
  36. should "have rack headers" do
  37. GET("/test")
  38. response["rack.version"].should.equal([1,1])
  39. response["rack.multithread"].should.be.false
  40. response["rack.multiprocess"].should.be.true
  41. response["rack.run_once"].should.be.true
  42. end
  43. should "have CGI headers on GET" do
  44. GET("/test")
  45. response["REQUEST_METHOD"].should.equal "GET"
  46. response["SCRIPT_NAME"].should.equal "/test"
  47. response["REQUEST_PATH"].should.equal "/"
  48. response["PATH_INFO"].should.be.nil
  49. response["QUERY_STRING"].should.equal ""
  50. response["test.postdata"].should.equal ""
  51. GET("/test/foo?quux=1")
  52. response["REQUEST_METHOD"].should.equal "GET"
  53. response["SCRIPT_NAME"].should.equal "/test"
  54. response["REQUEST_PATH"].should.equal "/"
  55. response["PATH_INFO"].should.equal "/foo"
  56. response["QUERY_STRING"].should.equal "quux=1"
  57. end
  58. should "have CGI headers on POST" do
  59. POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'})
  60. status.should.equal 200
  61. response["REQUEST_METHOD"].should.equal "POST"
  62. response["SCRIPT_NAME"].should.equal "/test"
  63. response["REQUEST_PATH"].should.equal "/"
  64. response["QUERY_STRING"].should.equal ""
  65. response["HTTP_X_TEST_HEADER"].should.equal "42"
  66. response["test.postdata"].should.equal "rack-form-data=23"
  67. end
  68. should "support HTTP auth" do
  69. GET("/test", {:user => "ruth", :passwd => "secret"})
  70. response["HTTP_AUTHORIZATION"].should.equal "Basic cnV0aDpzZWNyZXQ="
  71. end
  72. should "set status" do
  73. GET("/test?secret")
  74. status.should.equal 403
  75. response["rack.url_scheme"].should.equal "http"
  76. end
  77. # Keep this last.
  78. should "shutdown" do
  79. Process.kill 15, $pid
  80. Process.wait($pid).should == $pid
  81. end
  82. end
  83. rescue RuntimeError
  84. $stderr.puts "Skipping Rack::Handler::CGI tests (lighttpd is required). Install lighttpd and try again."
  85. rescue NotImplementedError
  86. $stderr.puts "Your Ruby implemenation or platform does not support fork. Skipping Rack::Handler::CGI tests."
  87. end