Skip to content

Commit

Permalink
Use the read method on the return of open_url to actually get the con…
Browse files Browse the repository at this point in the history
…tent.
  • Loading branch information
gerases authored and Sergei Gerasenko committed Mar 15, 2024
1 parent 802eb9a commit fa8339c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/loadjson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Puppet::Parser::Functions
url = args[0]
end
begin
contents = OpenURI.open_uri(url, http_options)
contents = OpenURI.open_uri(url, http_options).read
rescue OpenURI::HTTPError => e
res = e.io
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
Expand Down
9 changes: 4 additions & 5 deletions spec/functions/loadjson_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with(filename).and_return(true).once
allow(File).to receive(:read).with(filename).and_return(json).once
allow(File).to receive(:read).with(filename).and_return(json).once
if Puppet::PUPPETVERSION[0].to_i < 8
allow(PSON).to receive(:load).with(json).and_return(data).once
else
Expand Down Expand Up @@ -98,7 +97,7 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(StringIO.new(json))
if Puppet::PUPPETVERSION[0].to_i < 8
expect(PSON).to receive(:load).with(json).and_return(data).once
else
Expand All @@ -118,7 +117,7 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(StringIO.new(json))
if Puppet::PUPPETVERSION[0].to_i < 8
expect(PSON).to receive(:load).with(json).and_return(data).once
else
Expand All @@ -138,7 +137,7 @@
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }

it {
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(StringIO.new(json))
if Puppet::PUPPETVERSION[0].to_i < 8
expect(PSON).to receive(:load).with(json).and_return(data).once
else
Expand All @@ -155,7 +154,7 @@
let(:json) { ',;{"key":"value"}' }

it {
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(StringIO.new(json))
if Puppet::PUPPETVERSION[0].to_i < 8
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
else
Expand Down

0 comments on commit fa8339c

Please sign in to comment.