spec_webrick.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. require 'rack/mock'
  2. require File.expand_path('../testrequest', __FILE__)
  3. Thread.abort_on_exception = true
  4. describe Rack::Handler::WEBrick do
  5. extend TestRequest::Helpers
  6. @server = WEBrick::HTTPServer.new(:Host => @host='127.0.0.1',
  7. :Port => @port=9202,
  8. :Logger => WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),
  9. :AccessLog => [])
  10. @server.mount "/test", Rack::Handler::WEBrick,
  11. Rack::Lint.new(TestRequest.new)
  12. Thread.new { @server.start }
  13. trap(:INT) { @server.shutdown }
  14. should "respond" do
  15. lambda {
  16. GET("/test")
  17. }.should.not.raise
  18. end
  19. should "be a WEBrick" do
  20. GET("/test")
  21. status.should.equal 200
  22. response["SERVER_SOFTWARE"].should =~ /WEBrick/
  23. response["HTTP_VERSION"].should.equal "HTTP/1.1"
  24. response["SERVER_PROTOCOL"].should.equal "HTTP/1.1"
  25. response["SERVER_PORT"].should.equal "9202"
  26. response["SERVER_NAME"].should.equal "127.0.0.1"
  27. end
  28. should "have rack headers" do
  29. GET("/test")
  30. response["rack.version"].should.equal [1,1]
  31. response["rack.multithread"].should.be.true
  32. response["rack.multiprocess"].should.be.false
  33. response["rack.run_once"].should.be.false
  34. end
  35. should "have CGI headers on GET" do
  36. GET("/test")
  37. response["REQUEST_METHOD"].should.equal "GET"
  38. response["SCRIPT_NAME"].should.equal "/test"
  39. response["REQUEST_PATH"].should.equal "/test"
  40. response["PATH_INFO"].should.be.equal ""
  41. response["QUERY_STRING"].should.equal ""
  42. response["test.postdata"].should.equal ""
  43. GET("/test/foo?quux=1")
  44. response["REQUEST_METHOD"].should.equal "GET"
  45. response["SCRIPT_NAME"].should.equal "/test"
  46. response["REQUEST_PATH"].should.equal "/test/foo"
  47. response["PATH_INFO"].should.equal "/foo"
  48. response["QUERY_STRING"].should.equal "quux=1"
  49. GET("/test/foo%25encoding?quux=1")
  50. response["REQUEST_METHOD"].should.equal "GET"
  51. response["SCRIPT_NAME"].should.equal "/test"
  52. response["REQUEST_PATH"].should.equal "/test/foo%25encoding"
  53. response["PATH_INFO"].should.equal "/foo%25encoding"
  54. response["QUERY_STRING"].should.equal "quux=1"
  55. end
  56. should "have CGI headers on POST" do
  57. POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'})
  58. status.should.equal 200
  59. response["REQUEST_METHOD"].should.equal "POST"
  60. response["SCRIPT_NAME"].should.equal "/test"
  61. response["REQUEST_PATH"].should.equal "/test"
  62. response["PATH_INFO"].should.equal ""
  63. response["QUERY_STRING"].should.equal ""
  64. response["HTTP_X_TEST_HEADER"].should.equal "42"
  65. response["test.postdata"].should.equal "rack-form-data=23"
  66. end
  67. should "support HTTP auth" do
  68. GET("/test", {:user => "ruth", :passwd => "secret"})
  69. response["HTTP_AUTHORIZATION"].should.equal "Basic cnV0aDpzZWNyZXQ="
  70. end
  71. should "set status" do
  72. GET("/test?secret")
  73. status.should.equal 403
  74. response["rack.url_scheme"].should.equal "http"
  75. end
  76. should "correctly set cookies" do
  77. @server.mount "/cookie-test", Rack::Handler::WEBrick,
  78. Rack::Lint.new(lambda { |req|
  79. res = Rack::Response.new
  80. res.set_cookie "one", "1"
  81. res.set_cookie "two", "2"
  82. res.finish
  83. })
  84. Net::HTTP.start(@host, @port) { |http|
  85. res = http.get("/cookie-test")
  86. res.code.to_i.should.equal 200
  87. res.get_fields("set-cookie").should.equal ["one=1", "two=2"]
  88. }
  89. end
  90. should "provide a .run" do
  91. block_ran = false
  92. catch(:done) {
  93. Rack::Handler::WEBrick.run(lambda {},
  94. {
  95. :Host => '127.0.0.1',
  96. :Port => 9210,
  97. :Logger => WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),
  98. :AccessLog => []}) { |server|
  99. block_ran = true
  100. server.should.be.kind_of WEBrick::HTTPServer
  101. @s = server
  102. throw :done
  103. }
  104. }
  105. block_ran.should.be.true
  106. @s.shutdown
  107. end
  108. should "return repeated headers" do
  109. @server.mount "/headers", Rack::Handler::WEBrick,
  110. Rack::Lint.new(lambda { |req|
  111. [
  112. 401,
  113. { "Content-Type" => "text/plain",
  114. "WWW-Authenticate" => "Bar realm=X\nBaz realm=Y" },
  115. [""]
  116. ]
  117. })
  118. Net::HTTP.start(@host, @port) { |http|
  119. res = http.get("/headers")
  120. res.code.to_i.should.equal 401
  121. res["www-authenticate"].should.equal "Bar realm=X, Baz realm=Y"
  122. }
  123. end
  124. @server.shutdown
  125. end