spec_methodoverride.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. require 'stringio'
  2. require 'rack/methodoverride'
  3. require 'rack/mock'
  4. describe Rack::MethodOverride do
  5. should "not affect GET requests" do
  6. env = Rack::MockRequest.env_for("/?_method=delete", :method => "GET")
  7. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  8. req = app.call(env)
  9. req.env["REQUEST_METHOD"].should.equal "GET"
  10. end
  11. should "modify REQUEST_METHOD for POST requests when _method parameter is set" do
  12. env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=put")
  13. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  14. req = app.call(env)
  15. req.env["REQUEST_METHOD"].should.equal "PUT"
  16. end
  17. should "modify REQUEST_METHOD for POST requests when X-HTTP-Method-Override is set" do
  18. env = Rack::MockRequest.env_for("/",
  19. :method => "POST",
  20. "HTTP_X_HTTP_METHOD_OVERRIDE" => "PATCH"
  21. )
  22. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  23. req = app.call(env)
  24. req.env["REQUEST_METHOD"].should.equal "PATCH"
  25. end
  26. should "not modify REQUEST_METHOD if the method is unknown" do
  27. env = Rack::MockRequest.env_for("/", :method => "POST", :input => "_method=foo")
  28. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  29. req = app.call(env)
  30. req.env["REQUEST_METHOD"].should.equal "POST"
  31. end
  32. should "not modify REQUEST_METHOD when _method is nil" do
  33. env = Rack::MockRequest.env_for("/", :method => "POST", :input => "foo=bar")
  34. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  35. req = app.call(env)
  36. req.env["REQUEST_METHOD"].should.equal "POST"
  37. end
  38. should "store the original REQUEST_METHOD prior to overriding" do
  39. env = Rack::MockRequest.env_for("/",
  40. :method => "POST",
  41. :input => "_method=options")
  42. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  43. req = app.call(env)
  44. req.env["rack.methodoverride.original_method"].should.equal "POST"
  45. end
  46. should "not modify REQUEST_METHOD when given invalid multipart form data" do
  47. input = <<EOF
  48. --AaB03x\r
  49. content-disposition: form-data; name="huge"; filename="huge"\r
  50. EOF
  51. env = Rack::MockRequest.env_for("/",
  52. "CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
  53. "CONTENT_LENGTH" => input.size,
  54. :method => "POST", :input => input)
  55. app = Rack::MethodOverride.new(lambda{|envx| Rack::Request.new(envx) })
  56. req = app.call(env)
  57. req.env["REQUEST_METHOD"].should.equal "POST"
  58. end
  59. end