-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathresource_reader_spec.rb
More file actions
141 lines (113 loc) · 3.48 KB
/
Copy pathresource_reader_spec.rb
File metadata and controls
141 lines (113 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
RSpec.shared_examples 'content is read' do
let(:result) { subject.read_content(resource, verify_ssl) }
it 'does not return nil' do
expect(result).not_to be_nil
end
it 'is read' do
expect(result).to eq(content)
end
end
RSpec.describe ThreeScaleToolbox::ResourceReader do
include_context :temp_dir
let(:verify_ssl) { true }
subject do
Class.new { include ThreeScaleToolbox::ResourceReader }.new
end
context '#load_resource' do
let(:resource) { tmp_dir.join('file.ext').tap { |conf| conf.write(content) } }
let(:result) { subject.load_resource(resource, verify_ssl) }
context 'valid json' do
let(:content) { '{ "some_key": "some value" }' }
it 'does not return nil' do
expect(result).not_to be_nil
end
it 'is loaded' do
expect(result).to eq('some_key' => 'some value')
end
end
context 'valid yaml' do
let(:content) do
<<~YAML
---
some_key: "some value"
YAML
end
it 'does not return nil' do
expect(result).not_to be_nil
end
it 'is loaded' do
expect(result).to eq('some_key' => 'some value')
end
end
context 'invalid yaml' do
let(:content) do
<<~YAML
---
`
YAML
end
it 'raises error' do
expect { result }.to raise_error(ThreeScaleToolbox::Error)
end
end
context 'invalid json' do
let(:content) { '{ `some }' }
it 'raises error' do
expect { result }.to raise_error(ThreeScaleToolbox::Error)
end
end
end
context '#read_content' do
let(:content) do
<<~YAML
---
swagger: "2.0"
YAML
end
context 'from file' do
let(:resource) { tmp_dir.join('file.yaml').tap { |conf| conf.write(content) } }
it_behaves_like 'content is read'
end
context 'from folder' do
let(:resource) { tmp_dir }
it 'error is raised' do
expect { subject.read_content(resource, verify_ssl) }.to raise_error(ThreeScaleToolbox::Error,
/File not found/)
end
end
context 'from URL' do
let(:resource) { 'https://example.com/petstore.yaml' }
let(:net_class_stub) { class_double(Net::HTTP).as_stubbed_const }
let(:net_client) { instance_double(Net::HTTP) }
let(:net_response) { instance_double(Net::HTTPSuccess) }
context 'on HTTP success' do
before :each do
stub_request(:get, 'https://example.com/petstore.yaml').
to_return(status: 200, body: content, headers: {})
end
it_behaves_like 'content is read'
end
context 'on HTTP error' do
before :each do
stub_request(:get, 'https://example.com/petstore.yaml').
to_return(status: 500, body: 'unexpected error', headers: {})
end
it 'error is raised' do
expect { subject.read_content(resource, verify_ssl) }.to raise_error(ThreeScaleToolbox::Error,
/could not download resource/)
end
end
end
context 'from stdin' do
let(:resource) { '-' }
before :each do
expect(STDIN).to receive(:read).and_return(content)
end
it_behaves_like 'content is read'
end
context 'from stringio' do
let(:resource) { StringIO.new content }
it_behaves_like 'content is read'
end
end
end