-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapi_client_spec.rb
More file actions
280 lines (232 loc) · 10.7 KB
/
api_client_spec.rb
File metadata and controls
280 lines (232 loc) · 10.7 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
=begin
#Datadog API V1 Collection
#Collection of all Datadog Public endpoints.
The version of the OpenAPI document: 1.0
Contact: support@datadoghq.com
Generated by: https://openapi-generator.tech
Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
This product includes software developed at Datadog (https://www.datadoghq.com/).
Copyright 2020-Present Datadog, Inc.
=end
require 'spec_helper'
describe DatadogAPIClient::APIClient do
context 'initialization' do
context 'URL stuff' do
context 'host' do
it 'removes http from host' do
DatadogAPIClient.configure { |c| c.host = 'http://example.com' }
expect(DatadogAPIClient::Configuration.default.host).to eq('example.com')
end
it 'removes https from host' do
DatadogAPIClient.configure { |c| c.host = 'https://wookiee.com' }
expect(DatadogAPIClient::APIClient.default.config.host).to eq('wookiee.com')
end
it 'removes trailing path from host' do
DatadogAPIClient.configure { |c| c.host = 'hobo.com/v4' }
expect(DatadogAPIClient::Configuration.default.host).to eq('hobo.com')
end
end
context 'base_path' do
it "prepends a slash to base_path" do
DatadogAPIClient.configure { |c| c.base_path = 'v4/dog' }
expect(DatadogAPIClient::Configuration.default.base_path).to eq('/v4/dog')
end
it "doesn't prepend a slash if one is already there" do
DatadogAPIClient.configure { |c| c.base_path = '/v4/dog' }
expect(DatadogAPIClient::Configuration.default.base_path).to eq('/v4/dog')
end
it "ends up as a blank string if nil" do
DatadogAPIClient.configure { |c| c.base_path = nil }
expect(DatadogAPIClient::Configuration.default.base_path).to eq('')
end
end
end
end
describe 'timeout in #build_request' do
let(:config) { DatadogAPIClient::Configuration.new }
let(:api_client) { DatadogAPIClient::APIClient.new(config) }
it 'defaults to nil' do
expect(DatadogAPIClient::Configuration.default.timeout).to eq(nil)
expect(config.timeout).to eq(nil)
request = api_client.build_request(Net::HTTP::Get, '/test')
expect(request.options[:timeout]).to eq(nil)
end
it 'can be customized' do
config.timeout = 100
request = api_client.build_request(Net::HTTP::Get, '/test')
expect(request.options[:timeout]).to eq(100)
end
end
describe '#deserialize' do
it "handles Array<Integer>" do
api_client = DatadogAPIClient::APIClient.new
headers = { 'Content-Type' => 'application/json' }
response = double('response', headers: headers, body: '[12, 34]')
data = api_client.deserialize("V1", response, 'Array<Integer>')
expect(data).to be_instance_of(Array)
expect(data).to eq([12, 34])
end
it 'handles Array<Array<Integer>>' do
api_client = DatadogAPIClient::APIClient.new
headers = { 'Content-Type' => 'application/json' }
response = double('response', headers: headers, body: '[[12, 34], [56]]')
data = api_client.deserialize("V1", response, 'Array<Array<Integer>>')
expect(data).to be_instance_of(Array)
expect(data).to eq([[12, 34], [56]])
end
it 'handles Hash<String, String>' do
api_client = DatadogAPIClient::APIClient.new
headers = { 'Content-Type' => 'application/json' }
response = double('response', headers: headers, body: '{"message": "Hello"}')
data = api_client.deserialize("V1", response, 'Hash<String, String>')
expect(data).to be_instance_of(Hash)
expect(data).to eq(:message => 'Hello')
end
end
describe "#object_to_hash" do
it 'ignores nils and includes empty arrays' do
# uncomment below to test object_to_hash for model
# api_client = DatadogAPIClient::APIClient.new
# _model = DatadogAPIClient::ModelName.new
# update the model attribute below
# _model.id = 1
# update the expected value (hash) below
# expected = {id: 1, name: '', tags: []}
# expect(api_client.object_to_hash(_model)).to eq(expected)
end
end
describe '#build_collection_param' do
let(:param) { ['aa', 'bb', 'cc'] }
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'works for csv' do
expect(api_client.build_collection_param(param, :csv)).to eq('aa,bb,cc')
end
it 'works for ssv' do
expect(api_client.build_collection_param(param, :ssv)).to eq('aa bb cc')
end
it 'works for tsv' do
expect(api_client.build_collection_param(param, :tsv)).to eq("aa\tbb\tcc")
end
it 'works for pipes' do
expect(api_client.build_collection_param(param, :pipes)).to eq('aa|bb|cc')
end
it 'works for multi' do
expect(api_client.build_collection_param(param, :multi)).to eq(['aa', 'bb', 'cc'])
end
it 'fails for invalid collection format' do
expect { api_client.build_collection_param(param, :INVALID) }.to raise_error(RuntimeError, 'unknown collection format: :INVALID')
end
end
describe '#json_mime?' do
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'works' do
expect(api_client.json_mime?(nil)).to eq false
expect(api_client.json_mime?('')).to eq false
expect(api_client.json_mime?('application/json')).to eq true
expect(api_client.json_mime?('application/json; charset=UTF8')).to eq true
expect(api_client.json_mime?('APPLICATION/JSON')).to eq true
expect(api_client.json_mime?('application/xml')).to eq false
expect(api_client.json_mime?('text/plain')).to eq false
end
end
describe '#select_header_accept' do
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'works' do
expect(api_client.select_header_accept(nil)).to be_nil
expect(api_client.select_header_accept([])).to be_nil
expect(api_client.select_header_accept(['application/json'])).to eq('application/json')
expect(api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8'])).to eq('application/xml, application/json; charset=UTF8')
expect(api_client.select_header_accept(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON, text/html')
expect(api_client.select_header_accept(['application/xml'])).to eq('application/xml')
expect(api_client.select_header_accept(['text/html', 'application/xml'])).to eq('text/html, application/xml')
end
end
describe '#select_header_content_type' do
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'works' do
expect(api_client.select_header_content_type(nil)).to eq('application/json')
expect(api_client.select_header_content_type([])).to eq('application/json')
expect(api_client.select_header_content_type(['application/json'])).to eq('application/json')
expect(api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
expect(api_client.select_header_content_type(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
expect(api_client.select_header_content_type(['application/xml'])).to eq('application/xml')
expect(api_client.select_header_content_type(['text/plain', 'application/xml'])).to eq('text/plain')
end
end
describe '#sanitize_filename' do
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'works' do
expect(api_client.sanitize_filename('sun')).to eq('sun')
expect(api_client.sanitize_filename('sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('../sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('/var/tmp/sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('./sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('..\sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('\var\tmp\sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('c:\var\tmp\sun.gif')).to eq('sun.gif')
expect(api_client.sanitize_filename('.\sun.gif')).to eq('sun.gif')
end
end
describe '#update_params_for_auth!' do
let(:config) { DatadogAPIClient::Configuration.new }
let(:api_client) { DatadogAPIClient::APIClient.new(config) }
context 'when all auth credentials are configured' do
before do
config.api_key = 'test_api_key'
config.application_key = 'test_app_key'
config.access_token = 'ddpat_test_pat'
end
it 'sends all configured auth headers simultaneously' do
header_params = {}
query_params = {}
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
expect(header_params['Authorization']).to eq('Bearer ddpat_test_pat')
expect(header_params['DD-API-KEY']).to eq('test_api_key')
expect(header_params['DD-APPLICATION-KEY']).to eq('test_app_key')
end
end
context 'when only bearer token is configured' do
before do
config.access_token = 'ddpat_test_pat'
end
it 'sends only Bearer header, skips empty API key and app key' do
header_params = {}
query_params = {}
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
expect(header_params['Authorization']).to eq('Bearer ddpat_test_pat')
expect(header_params).not_to have_key('DD-API-KEY')
expect(header_params).not_to have_key('DD-APPLICATION-KEY')
end
end
context 'when only API key and app key are configured' do
before do
config.api_key = 'test_api_key'
config.application_key = 'test_app_key'
end
it 'sends API key and app key, no Bearer header' do
header_params = {}
query_params = {}
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
expect(header_params['DD-API-KEY']).to eq('test_api_key')
expect(header_params['DD-APPLICATION-KEY']).to eq('test_app_key')
expect(header_params).not_to have_key('Authorization')
end
end
end
describe '#sanitize_request_header' do
let(:api_client) { DatadogAPIClient::APIClient.new }
it 'redacts sensitive headers including Authorization' do
headers = {
'DD-API-KEY' => 'secret_api_key',
'DD-APPLICATION-KEY' => 'secret_app_key',
'Authorization' => 'Bearer ddapp_secret_pat',
'Content-Type' => 'application/json'
}
sanitized = api_client.sanitize_request_header(headers)
expect(sanitized['DD-API-KEY']).to eq('REDACTED')
expect(sanitized['DD-APPLICATION-KEY']).to eq('REDACTED')
expect(sanitized['Authorization']).to eq('REDACTED')
expect(sanitized['Content-Type']).to eq('application/json')
end
end
end