From 603dce8628246a17009c3a5f30cb57e21b146672 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 14 Aug 2013 08:12:39 +0200 Subject: add request method override for browsers Browsers are not able to send put, delete or any other request from a plain html form. This limits the possibilities with APIs so an override was introduced in many frameworks in the form, that `_method` could be defined in a post payload. With this, zero also supports `_method` in the post payload to make it possible to use all functions of the API with javascript through plain html. --- spec/unit/zero/request/method_spec.rb | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'spec/unit') diff --git a/spec/unit/zero/request/method_spec.rb b/spec/unit/zero/request/method_spec.rb index 42ea56a..b9352e1 100644 --- a/spec/unit/zero/request/method_spec.rb +++ b/spec/unit/zero/request/method_spec.rb @@ -5,4 +5,47 @@ describe Zero::Request, '#method' do let(:env) { EnvGenerator.get('/foo') } its(:method) { should == :get } + + context 'with post requests' do + context 'and _method defined' do + let(:env) do + EnvGenerator.post('/foo', { + :input => '_method=put', + 'CONTENT_TYPE' => 'multipart/form-data' + }) + end + it 'uses _method from the payload to change the method' do + expect(subject.method).to be(:put) + end + end + + context 'and _method not defined' do + let(:env) do + EnvGenerator.post('/foo', { + :input => 'foo=bar', + 'CONTENT_TYPE' => 'multipart/form-data' + }) + end + its(:method) { should == :post } + end + + context 'and _method has wrong content' do + let(:env) do + EnvGenerator.post('/foo', { + :input => '_method=foobar', + 'CONTENT_TYPE' => 'multipart/form-data' + }) + end + its(:method) { should == :post } + end + + context 'and no payload' do + let(:env) do + EnvGenerator.post('/foo', { + 'CONTENT_TYPE' => 'multipart/form-data' + }) + end + its(:method) { should == :post } + end + end end -- cgit v1.2.3-70-g09d2