mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
PUT and DELETE working
This commit is contained in:
parent
f0bc2312d7
commit
49538259f2
4 changed files with 50 additions and 2 deletions
|
@ -34,6 +34,7 @@ SINATRA_ROOT = File.dirname(__FILE__) + '/..'
|
|||
require File.dirname(__FILE__) + '/sinatra/loader'
|
||||
|
||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/core_ext/*.rb')
|
||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/rack_ext/*.rb')
|
||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb')
|
||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
|
||||
|
||||
|
|
15
lib/sinatra/rack_ext/request.rb
Normal file
15
lib/sinatra/rack_ext/request.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module Rack
|
||||
|
||||
class Request
|
||||
|
||||
def request_method
|
||||
if @env['REQUEST_METHOD'] == 'POST' && %w(PUT DELETE).include?(params['_method'])
|
||||
params['_method'].upcase
|
||||
else
|
||||
@env['REQUEST_METHOD']
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -76,5 +76,16 @@ describe "When a dispatcher receives a request" do
|
|||
get_it '/test/blake'
|
||||
body.should.equal 'blake'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it "should respond to DELETE and PUT" do
|
||||
Sinatra::Event.new(:delete, '/') do
|
||||
request.request_method
|
||||
end
|
||||
|
||||
# Browser only know GET and POST. DELETE and PUT are signaled by passing in a _method paramater
|
||||
post_it '/', :_method => 'DELETE'
|
||||
status.should.equal 200
|
||||
text.should.equal 'DELETE'
|
||||
end
|
||||
|
||||
end
|
||||
|
|
21
test/sinatra/request_test.rb
Normal file
21
test/sinatra/request_test.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require File.dirname(__FILE__) + '/../helper'
|
||||
|
||||
describe "Rack::Request" do
|
||||
it "should return PUT and DELETE based on _method param" do
|
||||
env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=DELETE')}
|
||||
Rack::Request.new(env).request_method.should.equal 'DELETE'
|
||||
|
||||
env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=PUT')}
|
||||
Rack::Request.new(env).request_method.should.equal 'PUT'
|
||||
end
|
||||
|
||||
it "should not allow faking" do
|
||||
env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=GET')}
|
||||
Rack::Request.new(env).request_method.should.equal 'POST'
|
||||
|
||||
env = {'REQUEST_METHOD' => 'GET', 'rack.input' => StringIO.new('_method=POST')}
|
||||
Rack::Request.new(env).request_method.should.equal 'GET'
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in a new issue