Skip to content

Commit c3a0c5c

Browse files
committed
chore: add safe_merge_body method and regression tests
1 parent 12c1a72 commit c3a0c5c

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

lib/auth0/mixins/httpproxy.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module HTTPProxy
2121
# proxying requests from instance methods to HTTP class methods
2222
%i[get post post_file post_form put patch delete delete_with_body].each do |method|
2323
define_method(method) do |uri, body = {}, extra_headers = {}|
24-
body = body.dup.delete_if { |_, v| v.nil? } if body.is_a?(Hash)
24+
body = safe_merge_body(body, extra_headers)
2525
token = get_token
2626
authorization_header(token) unless token.nil?
2727
request_with_retry(method, uri, body, extra_headers)
@@ -129,6 +129,12 @@ def call(method, url, timeout, headers, body = nil)
129129
e.response
130130
end
131131
end
132+
private
133+
def safe_merge_body(body, extra = {})
134+
return body unless body.is_a?(Hash)
135+
merged = extra.any? ? body.merge(extra) : body
136+
merged.compact
137+
end
132138
end
133139
end
134140
end

spec/lib/auth0/mixins/httpproxy_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,55 @@ def expected_payload(method, overrides = {})
272272

273273
%i(post post_form put patch).each do |http_method|
274274
context ".#{http_method}" do
275+
context 'when body is an Array' do
276+
let(:payload) { [{ permission_name: 'read:data', resource_server_identifier: 'https://api.example.com' }] }
277+
278+
if http_method == :post_form
279+
it 'sends the array as-is without wrapping in a Hash' do
280+
expect(RestClient::Request).to receive(:execute) do |args|
281+
expect(args[:payload]).to be_an(Array)
282+
expect(args[:payload]).to eq(payload)
283+
end.and_return(StubResponse.new('[]', true, 200))
284+
285+
@instance.send(http_method, '/test', payload)
286+
end
287+
else
288+
it 'sends the array as-is without wrapping in a Hash' do
289+
expect(RestClient::Request).to receive(:execute) do |args|
290+
parsed = JSON.parse(args[:payload], symbolize_names: true)
291+
expect(parsed).to be_an(Array)
292+
expect(parsed).to eq(payload)
293+
end.and_return(StubResponse.new('[]', true, 200))
294+
295+
@instance.send(http_method, '/test', payload)
296+
end
297+
end
298+
end
299+
300+
context 'when body is a Hash' do
301+
let(:payload) { { permission_name: 'read:data', resource_server_identifier: 'https://api.example.com' } }
302+
303+
if http_method == :post_form
304+
it 'sends the Hash without modification' do
305+
expect(RestClient::Request).to receive(:execute) do |args|
306+
expect(args[:payload]).to be_a(Hash)
307+
expect(args[:payload]).to include(payload)
308+
end.and_return(StubResponse.new('{}', true, 200))
309+
310+
@instance.send(http_method, '/test', payload)
311+
end
312+
else
313+
it 'sends the Hash as JSON without modification' do
314+
expect(RestClient::Request).to receive(:execute) do |args|
315+
parsed = JSON.parse(args[:payload], symbolize_names: true)
316+
expect(parsed).to be_a(Hash)
317+
expect(parsed).to eq(payload)
318+
end.and_return(StubResponse.new('{}', true, 200))
319+
320+
@instance.send(http_method, '/test', payload)
321+
end
322+
end
323+
end
275324
it { expect(@instance).to respond_to(http_method.to_sym) }
276325
it "should call send http #{http_method} method to path defined through HTTP"do
277326
expect(RestClient::Request).to receive(:execute).with(expected_payload(http_method))
@@ -465,6 +514,7 @@ def expected_payload(method, overrides = {})
465514
end
466515
end
467516
end
517+
468518
end
469519
end
470520

0 commit comments

Comments
 (0)