4 it { is_expected.not_to eq(nil) }
5 it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
7 describe 'when calling with valid arguments' do
9 allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}')
10 allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}')
13 context 'when a non-existing file is specified' do
15 if Puppet::Util::Platform.windows?
23 allow(File).to receive(:exists?).with(filename).and_return(false).once
24 allow(PSON).to receive(:load).never
26 it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
27 it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
28 it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
31 context 'when an existing file is specified' do
33 if Puppet::Util::Platform.windows?
39 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
40 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
43 allow(File).to receive(:exists?).with(filename).and_return(true).once
44 allow(File).to receive(:read).with(filename).and_return(json).once
45 allow(File).to receive(:read).with(filename).and_return(json).once
46 allow(PSON).to receive(:load).with(json).and_return(data).once
48 it { is_expected.to run.with_params(filename).and_return(data) }
51 context 'when the file could not be parsed' do
53 if Puppet::Util::Platform.windows?
59 let(:json) { '{"key":"value"}' }
62 allow(File).to receive(:exists?).with(filename).and_return(true).once
63 allow(File).to receive(:read).with(filename).and_return(json).once
64 allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
66 it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
69 context 'when an existing URL is specified' do
71 'https://example.local/myhash.json'
73 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
74 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
75 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
78 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
79 expect(PSON).to receive(:load).with(json).and_return(data).once
80 is_expected.to run.with_params(filename).and_return(data)
84 context 'when an existing URL (with username and password) is specified' do
86 'https://user1:pass1@example.local/myhash.json'
88 let(:url_no_auth) { 'https://example.local/myhash.json' }
89 let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
90 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
91 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
94 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
95 expect(PSON).to receive(:load).with(json).and_return(data).once
96 is_expected.to run.with_params(filename).and_return(data)
100 context 'when an existing URL (with username) is specified' do
102 'https://user1@example.local/myhash.json'
104 let(:url_no_auth) { 'https://example.local/myhash.json' }
105 let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
106 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
107 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
110 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
111 expect(PSON).to receive(:load).with(json).and_return(data).once
112 is_expected.to run.with_params(filename).and_return(data)
116 context 'when the URL output could not be parsed, with default specified' do
118 'https://example.local/myhash.json'
120 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
121 let(:json) { ',;{"key":"value"}' }
124 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
125 expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
126 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
130 context 'when the URL does not exist, with default specified' do
132 'https://example.local/myhash.json'
134 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
137 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
138 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')