Skip to content

Commit

Permalink
fix(identifier): Escape identifier in put and destroy methods (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad authored Dec 11, 2023
1 parent 83dd9b9 commit 1382161
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lago-ruby-client (0.52.1.pre.beta)
lago-ruby-client (0.52.2.pre.beta)
jwt
openssl

Expand Down
4 changes: 2 additions & 2 deletions lib/lago/api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def post(body, path = uri.path)
end

def put(path = uri.path, identifier:, body:)
uri_path = identifier.nil? ? path : "#{path}/#{identifier}"
uri_path = identifier.nil? ? path : "#{path}/#{URI.encode_www_form_component(identifier)}"
response = http_client.send_request(
'PUT',
uri_path,
Expand All @@ -47,7 +47,7 @@ def get(path = uri.path, identifier:)

def destroy(path = uri.path, identifier:, options: nil)
uri_path = path
uri_path += "/#{identifier}" if identifier
uri_path += "/#{URI.encode_www_form_component(identifier)}" if identifier
uri_path += "?#{URI.encode_www_form(options)}" unless options.nil?
response = http_client.send_request(
'DELETE',
Expand Down
36 changes: 36 additions & 0 deletions spec/lago/api/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,40 @@
expect(URI).to have_received(:encode_www_form_component).with(identifier)
end
end

describe '#put' do
let(:identifier) { 'gid://app/Customer/1234' }

before do
stub_request(:put, 'https://testapi.example.org:443/gid:%2F%2Fapp%2FCustomer%2F1234')

allow(URI).to receive(:encode_www_form_component)
.with(identifier)
.and_return('gid:%2F%2Fapp%2FCustomer%2F1234')

connection.put(identifier: identifier, body: nil)
end

it 'encodes the identifier' do
expect(URI).to have_received(:encode_www_form_component).with(identifier)
end
end

describe '#destroy' do
let(:identifier) { 'gid://app/Customer/1234' }

before do
stub_request(:delete, 'https://testapi.example.org:443/gid:%2F%2Fapp%2FCustomer%2F1234')

allow(URI).to receive(:encode_www_form_component)
.with(identifier)
.and_return('gid:%2F%2Fapp%2FCustomer%2F1234')

connection.destroy(identifier: identifier)
end

it 'encodes the identifier' do
expect(URI).to have_received(:encode_www_form_component).with(identifier)
end
end
end

0 comments on commit 1382161

Please sign in to comment.