2010-06-14 06:09:24 +02:00
|
|
|
require 'rack/mock'
|
|
|
|
require File.expand_path('../testrequest', __FILE__)
|
2009-12-19 03:16:58 +01:00
|
|
|
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
describe Rack::Handler::WEBrick do
|
|
|
|
extend TestRequest::Helpers
|
|
|
|
|
|
|
|
@server = WEBrick::HTTPServer.new(:Host => @host='0.0.0.0',
|
|
|
|
:Port => @port=9202,
|
|
|
|
:Logger => WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),
|
|
|
|
:AccessLog => [])
|
|
|
|
@server.mount "/test", Rack::Handler::WEBrick,
|
|
|
|
Rack::Lint.new(TestRequest.new)
|
|
|
|
Thread.new { @server.start }
|
|
|
|
trap(:INT) { @server.shutdown }
|
2009-12-19 03:16:58 +01:00
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "respond" do
|
2009-12-19 03:16:58 +01:00
|
|
|
lambda {
|
|
|
|
GET("/test")
|
|
|
|
}.should.not.raise
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "be a WEBrick" do
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test")
|
2010-06-14 06:09:24 +02:00
|
|
|
status.should.equal 200
|
2009-12-19 03:16:58 +01:00
|
|
|
response["SERVER_SOFTWARE"].should =~ /WEBrick/
|
|
|
|
response["HTTP_VERSION"].should.equal "HTTP/1.1"
|
|
|
|
response["SERVER_PROTOCOL"].should.equal "HTTP/1.1"
|
|
|
|
response["SERVER_PORT"].should.equal "9202"
|
|
|
|
response["SERVER_NAME"].should.equal "0.0.0.0"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "have rack headers" do
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test")
|
2009-12-26 21:00:18 +01:00
|
|
|
response["rack.version"].should.equal [1,1]
|
2010-06-14 06:09:24 +02:00
|
|
|
response["rack.multithread"].should.be.true
|
|
|
|
response["rack.multiprocess"].should.be.false
|
|
|
|
response["rack.run_once"].should.be.false
|
2009-12-19 03:16:58 +01:00
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "have CGI headers on GET" do
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test")
|
|
|
|
response["REQUEST_METHOD"].should.equal "GET"
|
|
|
|
response["SCRIPT_NAME"].should.equal "/test"
|
|
|
|
response["REQUEST_PATH"].should.equal "/"
|
2009-12-26 21:00:18 +01:00
|
|
|
response["PATH_INFO"].should.be.equal ""
|
2009-12-19 03:16:58 +01:00
|
|
|
response["QUERY_STRING"].should.equal ""
|
|
|
|
response["test.postdata"].should.equal ""
|
|
|
|
|
|
|
|
GET("/test/foo?quux=1")
|
|
|
|
response["REQUEST_METHOD"].should.equal "GET"
|
|
|
|
response["SCRIPT_NAME"].should.equal "/test"
|
|
|
|
response["REQUEST_PATH"].should.equal "/"
|
|
|
|
response["PATH_INFO"].should.equal "/foo"
|
|
|
|
response["QUERY_STRING"].should.equal "quux=1"
|
2009-12-26 21:00:18 +01:00
|
|
|
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test/foo%25encoding?quux=1")
|
|
|
|
response["REQUEST_METHOD"].should.equal "GET"
|
|
|
|
response["SCRIPT_NAME"].should.equal "/test"
|
|
|
|
response["REQUEST_PATH"].should.equal "/"
|
|
|
|
response["PATH_INFO"].should.equal "/foo%25encoding"
|
|
|
|
response["QUERY_STRING"].should.equal "quux=1"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "have CGI headers on POST" do
|
2009-12-19 03:16:58 +01:00
|
|
|
POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'})
|
|
|
|
status.should.equal 200
|
|
|
|
response["REQUEST_METHOD"].should.equal "POST"
|
|
|
|
response["SCRIPT_NAME"].should.equal "/test"
|
|
|
|
response["REQUEST_PATH"].should.equal "/"
|
|
|
|
response["QUERY_STRING"].should.equal ""
|
|
|
|
response["HTTP_X_TEST_HEADER"].should.equal "42"
|
|
|
|
response["test.postdata"].should.equal "rack-form-data=23"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "support HTTP auth" do
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test", {:user => "ruth", :passwd => "secret"})
|
|
|
|
response["HTTP_AUTHORIZATION"].should.equal "Basic cnV0aDpzZWNyZXQ="
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "set status" do
|
2009-12-19 03:16:58 +01:00
|
|
|
GET("/test?secret")
|
|
|
|
status.should.equal 403
|
|
|
|
response["rack.url_scheme"].should.equal "http"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "correctly set cookies" do
|
2009-12-19 03:16:58 +01:00
|
|
|
@server.mount "/cookie-test", Rack::Handler::WEBrick,
|
|
|
|
Rack::Lint.new(lambda { |req|
|
|
|
|
res = Rack::Response.new
|
|
|
|
res.set_cookie "one", "1"
|
|
|
|
res.set_cookie "two", "2"
|
|
|
|
res.finish
|
|
|
|
})
|
|
|
|
|
|
|
|
Net::HTTP.start(@host, @port) { |http|
|
|
|
|
res = http.get("/cookie-test")
|
|
|
|
res.code.to_i.should.equal 200
|
|
|
|
res.get_fields("set-cookie").should.equal ["one=1", "two=2"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "provide a .run" do
|
2009-12-19 03:16:58 +01:00
|
|
|
block_ran = false
|
|
|
|
catch(:done) {
|
|
|
|
Rack::Handler::WEBrick.run(lambda {},
|
|
|
|
{:Port => 9210,
|
|
|
|
:Logger => WEBrick::Log.new(nil, WEBrick::BasicLog::WARN),
|
|
|
|
:AccessLog => []}) { |server|
|
|
|
|
block_ran = true
|
|
|
|
server.should.be.kind_of WEBrick::HTTPServer
|
|
|
|
@s = server
|
|
|
|
throw :done
|
|
|
|
}
|
|
|
|
}
|
2010-06-14 06:09:24 +02:00
|
|
|
block_ran.should.be.true
|
2009-12-19 03:16:58 +01:00
|
|
|
@s.shutdown
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
@server.shutdown
|
2009-12-19 03:16:58 +01:00
|
|
|
end
|