From 13821618d9d4759bb9e10934d404050053d7a210 Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Mon, 11 Dec 2023 12:32:39 +0100 Subject: [PATCH] fix(identifier): Escape identifier in put and destroy methods (#165) --- Gemfile.lock | 2 +- lib/lago/api/connection.rb | 4 ++-- spec/lago/api/connection_spec.rb | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4ea6686..d791e9e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/lib/lago/api/connection.rb b/lib/lago/api/connection.rb index 22535ed..13a1498 100644 --- a/lib/lago/api/connection.rb +++ b/lib/lago/api/connection.rb @@ -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, @@ -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', diff --git a/spec/lago/api/connection_spec.rb b/spec/lago/api/connection_spec.rb index bf7d008..6c681e5 100644 --- a/spec/lago/api/connection_spec.rb +++ b/spec/lago/api/connection_spec.rb @@ -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