Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/remote_resource/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/remote_resource/querying/persistence_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }

Expand Down
74 changes: 73 additions & 1 deletion spec/lib/remote_resource/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down