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"}')
11 # Additional modules used by litmus which are identified while running these dues to being in fixtures
12 allow(File).to receive(:read).with(%r{\/(provision|puppet_agent|facts)\/metadata.json}, :encoding => 'utf-8')
15 context 'when a non-existing file is specified' do
17 if Puppet::Util::Platform.windows?
25 allow(File).to receive(:exists?).with(filename).and_return(false).once
26 allow(PSON).to receive(:load).never
28 it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
29 it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
30 it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
33 context 'when an existing file is specified' do
35 if Puppet::Util::Platform.windows?
41 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
42 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
45 allow(File).to receive(:exists?).with(filename).and_return(true).once
46 allow(File).to receive(:read).with(filename).and_return(json).once
47 allow(File).to receive(:read).with(filename).and_return(json).once
48 allow(PSON).to receive(:load).with(json).and_return(data).once
50 it { is_expected.to run.with_params(filename).and_return(data) }
53 context 'when the file could not be parsed' do
55 if Puppet::Util::Platform.windows?
61 let(:json) { '{"key":"value"}' }
64 allow(File).to receive(:exists?).with(filename).and_return(true).once
65 allow(File).to receive(:read).with(filename).and_return(json).once
66 allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
68 it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
71 context 'when an existing URL is specified' do
73 'https://example.local/myhash.json'
75 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
76 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
77 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
80 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
81 expect(PSON).to receive(:load).with(json).and_return(data).once
82 is_expected.to run.with_params(filename).and_return(data)
86 context 'when an existing URL (with username and password) is specified' do
88 'https://user1:pass1@example.local/myhash.json'
90 let(:url_no_auth) { 'https://example.local/myhash.json' }
91 let(:basic_auth) { { :http_basic_authentication => ['user1', 'pass1'] } }
92 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
93 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
96 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
97 expect(PSON).to receive(:load).with(json).and_return(data).once
98 is_expected.to run.with_params(filename).and_return(data)
102 context 'when an existing URL (with username) is specified' do
104 'https://user1@example.local/myhash.json'
106 let(:url_no_auth) { 'https://example.local/myhash.json' }
107 let(:basic_auth) { { :http_basic_authentication => ['user1', ''] } }
108 let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
109 let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' }
112 expect(OpenURI).to receive(:open_uri).with(url_no_auth, basic_auth).and_return(json)
113 expect(PSON).to receive(:load).with(json).and_return(data).once
114 is_expected.to run.with_params(filename).and_return(data)
118 context 'when the URL output could not be parsed, with default specified' do
120 'https://example.local/myhash.json'
122 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
123 let(:json) { ',;{"key":"value"}' }
126 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_return(json)
127 expect(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!'
128 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')
132 context 'when the URL does not exist, with default specified' do
134 'https://example.local/myhash.json'
136 let(:basic_auth) { { :http_basic_authentication => ['', ''] } }
139 expect(OpenURI).to receive(:open_uri).with(filename, basic_auth).and_raise OpenURI::HTTPError, '404 File not Found'
140 is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value')