Skip to content

Commit cab7d7e

Browse files
committed
Reverting openURI changes
1 parent 799d608 commit cab7d7e

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

lib/puppet/parser/functions/loadjson.rb

+7-14
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,16 @@ module Puppet::Parser::Functions
4343
else
4444
url = args[0]
4545
end
46+
begin
47+
contents = OpenURI.open_uri(url, http_options)
48+
rescue OpenURI::HTTPError => e
49+
res = e.io
50+
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
51+
args[1]
52+
end
4653
if Puppet::Util::Package.versioncmp(Puppet.version, '8.0.0').negative?
47-
begin
48-
contents = OpenURI.open_uri(url, **http_options)
49-
rescue OpenURI::HTTPError => e
50-
res = e.io
51-
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
52-
args[1]
53-
end
5454
PSON.load(contents) || args[1]
5555
else
56-
begin
57-
contents = URI.open(url, **http_options) # rubocop:disable Security/Open : Temporarily disabling this cop. This is a security risk and must be addressed before release.
58-
rescue URI::Error => e
59-
res = e.io
60-
warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
61-
args[1]
62-
end
6356
JSON.parse(contents) || args[1]
6457
end
6558
elsif File.exists?(args[0]) # rubocop:disable Lint/DeprecatedClassMethods : Changing to .exist? breaks the code

spec/functions/loadjson_spec.rb

+5-13
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@
9595
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
9696

9797
it {
98+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
9899
if Puppet::PUPPETVERSION[0].to_i < 8
99-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
100100
expect(PSON).to receive(:load).with(json).and_return(data).once
101101
else
102-
expect(URI).to receive(:open).with(filename).and_return(json)
103102
expect(JSON).to receive(:parse).with(json).and_return(data).once
104103
end
105104
expect(subject).to run.with_params(filename).and_return(data)
@@ -116,11 +115,10 @@
116115
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
117116

118117
it {
118+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
119119
if Puppet::PUPPETVERSION[0].to_i < 8
120-
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
121120
expect(PSON).to receive(:load).with(json).and_return(data).once
122121
else
123-
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
124122
expect(JSON).to receive(:parse).with(json).and_return(data).once
125123
end
126124
expect(subject).to run.with_params(filename).and_return(data)
@@ -137,11 +135,10 @@
137135
let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
138136

139137
it {
138+
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
140139
if Puppet::PUPPETVERSION[0].to_i < 8
141-
expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
142140
expect(PSON).to receive(:load).with(json).and_return(data).once
143141
else
144-
expect(URI).to receive(:open).with(url_no_auth, basic_auth).and_return(json)
145142
expect(JSON).to receive(:parse).with(json).and_return(data).once
146143
end
147144
expect(subject).to run.with_params(filename).and_return(data)
@@ -155,11 +152,10 @@
155152
let(:json) { ',;{"key":"value"}' }
156153

157154
it {
155+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
158156
if Puppet::PUPPETVERSION[0].to_i < 8
159-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_return(json)
160157
expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
161158
else
162-
expect(URI).to receive(:open).with(filename).and_return(json)
163159
expect(JSON).to receive(:parse).with(json).once.and_raise StandardError, 'Something terrible have happened!'
164160
end
165161
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
@@ -172,11 +168,7 @@
172168
end
173169

174170
it {
175-
if Puppet::PUPPETVERSION[0].to_i < 8
176-
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
177-
else
178-
expect(URI).to receive(:open).with(filename).and_raise URI::Error, '404 File not Found'
179-
end
171+
expect(OpenURI).to receive(:open_uri).with(filename, {}).and_raise OpenURI::HTTPError, '404 File not Found'
180172
expect(subject).to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
181173
}
182174
end

0 commit comments

Comments
 (0)