2009-12-19 03:16:58 +01:00
|
|
|
require 'stringio'
|
2010-06-14 06:09:24 +02:00
|
|
|
require 'rack/methodoverride'
|
|
|
|
require 'rack/mock'
|
2009-12-19 03:16:58 +01:00
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
describe Rack::MethodOverride do
|
|
|
|
should "not affect GET requests" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/?_method=delete", :method => "GET")
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["REQUEST_METHOD"].should.equal "GET"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "modify REQUEST_METHOD for POST requests when _method parameter is set" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=put")
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["REQUEST_METHOD"].should.equal "PUT"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "modify REQUEST_METHOD for POST requests when X-HTTP-Method-Override is set" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/",
|
|
|
|
:method => "POST",
|
|
|
|
"HTTP_X_HTTP_METHOD_OVERRIDE" => "PUT"
|
|
|
|
)
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["REQUEST_METHOD"].should.equal "PUT"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "not modify REQUEST_METHOD if the method is unknown" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=foo")
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["REQUEST_METHOD"].should.equal "POST"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "not modify REQUEST_METHOD when _method is nil" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/", :method => "POST", :input => "foo=bar")
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["REQUEST_METHOD"].should.equal "POST"
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "store the original REQUEST_METHOD prior to overriding" do
|
2009-12-19 03:16:58 +01:00
|
|
|
env = Rack::MockRequest.env_for("/",
|
|
|
|
:method => "POST",
|
|
|
|
:input => "_method=options")
|
2010-06-14 06:09:24 +02:00
|
|
|
app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
|
2009-12-19 03:16:58 +01:00
|
|
|
req = app.call(env)
|
|
|
|
|
|
|
|
req.env["rack.methodoverride.original_method"].should.equal "POST"
|
|
|
|
end
|
|
|
|
end
|