diff --git a/lib/lhs/concerns/item/save.rb b/lib/lhs/concerns/item/save.rb index 2fc56fcf..d77ba01e 100644 --- a/lib/lhs/concerns/item/save.rb +++ b/lib/lhs/concerns/item/save.rb @@ -45,7 +45,7 @@ def create_and_merge_data!(options) _data.merge_raw!(response_data.unwrap(:item_created_key)) response_headers = response_data._request.response.headers end - if response_headers && response_headers['Location'] + if options.fetch(:followlocation, true) && response_headers && response_headers['Location'] location_data = record.request(options.merge(url: response_headers['Location'], method: :get, body: nil)) _data.merge_raw!(location_data.unwrap(:item_created_key)) end diff --git a/lib/lhs/version.rb b/lib/lhs/version.rb index 0396dc8c..73f79707 100644 --- a/lib/lhs/version.rb +++ b/lib/lhs/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module LHS - VERSION = '25.0.4' + VERSION = '25.1.0' end diff --git a/spec/record/create_spec.rb b/spec/record/create_spec.rb index e7ac58c6..e4e11876 100644 --- a/spec/record/create_spec.rb +++ b/spec/record/create_spec.rb @@ -135,25 +135,61 @@ def ratings end context 'location header' do - before do - class ContactPerson < LHS::Record - endpoint 'http://datastore/contact_persons' + let(:created_at) { '2017-12-21' } + let(:location) { 'http://datastore/contact_persons/1' } + let(:name) { 'Sebastian' } + + context 'without `followlocation` option' do + before do + class ContactPerson < LHS::Record + endpoint 'http://datastore/contact_persons' + end + end + + it 'loads the data from the "Location" header after creation' do + stub_request(:post, "http://datastore/contact_persons") + .to_return(status: 204, headers: { Location: location }) + stub_request(:get, "http://datastore/contact_persons/1") + .to_return(body: { href: location, name: name, created_at: created_at }.to_json) + contact_person = ContactPerson.create!(name: name) + expect(contact_person.href).to eq location + expect(contact_person.created_at).to eq Date.parse(created_at) + expect(contact_person.name).to eq name end end - let(:location) { 'http://datastore/contact_persons/1' } - let(:created_at) { '2017-12-21' } - let(:name) { 'Sebastian' } + context 'when `followlocation` is set to `true`' do + before do + class ContactPerson < LHS::Record + endpoint 'http://datastore/contact_persons', followlocation: true + end + end + + it 'loads the data from the "Location" header after creation' do + stub_request(:post, "http://datastore/contact_persons") + .to_return(status: 204, headers: { Location: location }) + stub_request(:get, "http://datastore/contact_persons/1") + .to_return(body: { href: location, name: name, created_at: created_at }.to_json) + contact_person = ContactPerson.create!(name: name) + expect(contact_person.href).to eq location + expect(contact_person.created_at).to eq Date.parse(created_at) + expect(contact_person.name).to eq name + end + end - it 'Loads the data from the "Location" header after creation' do - stub_request(:post, "http://datastore/contact_persons") - .to_return(status: 204, headers: { Location: location }) - stub_request(:get, "http://datastore/contact_persons/1") - .to_return(body: { href: location, name: name, created_at: created_at }.to_json) - contact_person = ContactPerson.create!(name: name) - expect(contact_person.href).to eq location - expect(contact_person.created_at).to eq Date.parse(created_at) - expect(contact_person.name).to eq name + context 'when `followlocation` is set to `false`' do + before do + class ContactPerson < LHS::Record + endpoint 'http://datastore/contact_persons', followlocation: false + end + end + + it 'does not load the data from the "Location" header after creation' do + stub_request(:post, "http://datastore/contact_persons") + .to_return(status: 204, headers: { Location: location }) + contact_person = ContactPerson.create!(name: name) + expect(contact_person.name).to eq name + end end end end