diff --git a/lib/remote_resource/request.rb b/lib/remote_resource/request.rb index d5de7d5..1d44d97 100644 --- a/lib/remote_resource/request.rb +++ b/lib/remote_resource/request.rb @@ -81,6 +81,9 @@ def body case http_action when :put, :patch, :post attributes.to_json + when :delete + body_attributes = connection_options[:body] + body_attributes.present? ? body_attributes.to_json : nil when :get connection_options[:params].to_json if connection_options[:force_get_params_in_body] else diff --git a/spec/integration/destroy_spec.rb b/spec/integration/destroy_spec.rb index da770b0..d8d555e 100644 --- a/spec/integration/destroy_spec.rb +++ b/spec/integration/destroy_spec.rb @@ -180,6 +180,20 @@ class Post end end + describe 'with connection_options[:body]' do + let!(:expected_request) do + mock_request = stub_request(:delete, 'https://www.example.com/posts/12.json') + mock_request.with(body: { pseudonym: 'pseudonym' }.to_json, headers: expected_default_headers.merge('Content-Type' => 'application/json')) + mock_request.to_return(status: 204, body: response_body.to_json) + mock_request + end + + it 'performs the correct HTTP DELETE request' do + resource.destroy(body: { pseudonym: 'pseudonym' }) + expect(expected_request).to have_been_requested + end + end + describe 'with connection_options[:headers]' do let!(:expected_request) do mock_request = stub_request(:delete, 'https://www.example.com/posts/12.json') diff --git a/spec/lib/remote_resource/querying/persistence_methods_spec.rb b/spec/lib/remote_resource/querying/persistence_methods_spec.rb index 16c6806..ee815cb 100644 --- a/spec/lib/remote_resource/querying/persistence_methods_spec.rb +++ b/spec/lib/remote_resource/querying/persistence_methods_spec.rb @@ -415,6 +415,15 @@ class Post end end + it 'sends the body when connection_options[:body] is present' do + expected_request = stub_request(:delete, 'https://www.example.com/posts/12.json') + .with(body: { pseudonym: 'pseudonym' }.to_json) + .to_return(status: 204, body: response_body.to_json) + + resource.destroy(body: { pseudonym: 'pseudonym' }) + expect(expected_request).to have_been_requested + end + it 'does NOT change the given connection_options' do connection_options = { headers: { 'Foo' => 'Bar' } } diff --git a/spec/lib/remote_resource/request_spec.rb b/spec/lib/remote_resource/request_spec.rb index b324d05..685006b 100644 --- a/spec/lib/remote_resource/request_spec.rb +++ b/spec/lib/remote_resource/request_spec.rb @@ -267,7 +267,55 @@ class RequestDummyWithCollectionPrefix < RequestDummy let(:expected_body) { nil } let(:expected_connection_options) { request.connection_options } - it 'makes a DELETE request with the connection_options[:params] as query' do + it 'makes a DELETE request without a body' do + expect(connection).to receive(:delete).with(expected_request_url, params: expected_params, + body: expected_body, headers: expected_headers, + connecttimeout: 30, timeout: 120).and_call_original + request.perform + end + + include_examples 'a conditional construct for the response' + end + + context 'when the http_action is :delete with connection_options[:body]' do + let(:http_action) { 'delete' } + let(:attributes) do + { id: 15 } + end + let(:connection_options) do + { params: { pseudonym: 'pseudonym' }, body: { pseudonym: 'pseudonym', labels: [1, '2', 'three'] } } + end + + let(:expected_request_url) { 'http://www.foobar.com/request_dummy/15.json' } + let(:expected_params) { RemoteResource::Util.encode_params_to_query({ pseudonym: 'pseudonym' }) } + let(:expected_headers) { described_class::DEFAULT_HEADERS.merge(described_class::DEFAULT_CONTENT_TYPE) } + let(:expected_body) { JSON.generate(connection_options[:body]) } + let(:expected_connection_options) { request.connection_options } + + it 'makes a DELETE request with the body from connection_options[:body]' do + expect(connection).to receive(:delete).with(expected_request_url, params: expected_params, + body: expected_body, headers: expected_headers, + connecttimeout: 30, timeout: 120).and_call_original + request.perform + end + + include_examples 'a conditional construct for the response' + end + + context 'when the http_action is :delete without attributes' do + let(:http_action) { 'delete' } + let(:attributes) { {} } + let(:connection_options) do + { id: 15, params: { pseudonym: 'pseudonym' } } + end + + let(:expected_request_url) { 'http://www.foobar.com/request_dummy/15.json' } + let(:expected_params) { RemoteResource::Util.encode_params_to_query({ pseudonym: 'pseudonym' }) } + let(:expected_headers) { described_class::DEFAULT_HEADERS } + let(:expected_body) { nil } + let(:expected_connection_options) { request.connection_options } + + it 'makes a DELETE request without body' do expect(connection).to receive(:delete).with(expected_request_url, params: expected_params, body: expected_body, headers: expected_headers, connecttimeout: 30, timeout: 120).and_call_original @@ -460,6 +508,30 @@ class RequestDummyWithCollectionPrefix < RequestDummy expect(request.body).to be_nil end end + + context 'when the http_action is :delete and connection_options[:body] is present' do + let(:http_action) { :delete } + let(:connection_options) do + { body: { pseudonym: 'pseudonym', labels: [1, '2', 'three'] } } + end + + let(:expected_body) do + '{"pseudonym":"pseudonym","labels":[1,"2","three"]}' + end + + it 'returns the JSON-encoded connection_options[:body]' do + expect(request.body).to eql expected_body + end + end + + context 'when the http_action is :delete and connection_options[:body] is not present' do + let(:http_action) { :delete } + + it 'returns nil' do + expect(request.body).to be_nil + end + end + end describe '#attributes' do