Skip to content

Commit

Permalink
Do not follow Location header when followlocation option is set to fa…
Browse files Browse the repository at this point in the history
…lse.
  • Loading branch information
dimameshcharakou committed May 5, 2021
1 parent 8023c09 commit 3cc04cb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/lhs/concerns/item/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/lhs/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LHS
VERSION = '25.0.4'
VERSION = '25.1.0'
end
66 changes: 51 additions & 15 deletions spec/record/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cc04cb

Please sign in to comment.