-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathclient_spec.rb
277 lines (242 loc) · 9.36 KB
/
client_spec.rb
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
require 'spec_helper'
require 'logger'
describe Twilio::REST::ObsoleteClient do
it 'raise an exception' do
expect { Twilio::REST::ObsoleteClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::BaseClient do
it 'raise an exception' do
expect { Twilio::REST::BaseClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::IpMessagingClient do
it 'raise an exception' do
expect { Twilio::REST::IpMessagingClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::LookupsClient do
it 'raise an exception' do
expect { Twilio::REST::LookupsClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::MonitorClient do
it 'raise an exception' do
expect { Twilio::REST::MonitorClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::PricingClient do
it 'raise an exception' do
expect { Twilio::REST::PricingClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::TaskRouterClient do
it 'raise an exception' do
expect { Twilio::REST::TaskRouterClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::TrunkingClient do
it 'raise an exception' do
expect { Twilio::REST::TrunkingClient.new }.to raise_error(Twilio::REST::ObsoleteError)
end
end
describe Twilio::REST::Client do
context 'configuration' do
before do
Twilio.configure do |config|
config.account_sid = 'someSid'
config.auth_token = 'someToken'
config.http_client = 'someClient'
config.region = 'someRegion'
config.edge = 'someEdge'
config.logger = 'someLogger'
end
end
it 'uses the global configuration by default' do
@client = Twilio::REST::Client.new
expect(@client.account_sid).to eq('someSid')
expect(@client.auth_token).to eq('someToken')
expect(@client.http_client).to eq('someClient')
expect(@client.region).to eq('someRegion')
expect(@client.edge).to eq('someEdge')
expect(@client.logger).to eq('someLogger')
end
it 'uses the arguments over global configuration' do
@client = Twilio::REST::Client.new('myUser', 'myPassword', nil, 'myRegion', 'myClient', 'myLogger')
@client.edge = 'myEdge'
expect(@client.account_sid).to eq('myUser')
expect(@client.auth_token).to eq('myPassword')
expect(@client.http_client).to eq('myClient')
expect(@client.region).to eq('myRegion')
expect(@client.edge).to eq('myEdge')
expect(@client.logger).to eq('myLogger')
end
class MyVersion < Twilio::REST::Version
def initialize(domain)
super
@version = 'v1'
end
end
class MyDomain < Twilio::REST::Domain
def initialize(client)
super
@host = 'twilio.com'
@base_url = 'https://twilio.com'
@port = 443
end
end
end
context 'validation' do
before :all do
Twilio.configure do |config|
config.account_sid = 'someSid'
config.auth_token = 'someToken'
config.http_client = 'someClient'
config.region = nil
config.edge = nil
config.logger = nil
end
end
it 'successfully validates the working SSL certificate' do
@holodeck.mock Twilio::Response.new(200, '')
expect { @client.validate_ssl_certificate }.not_to raise_error
end
it 'fails to validate broken SSL certificates' do
@holodeck.mock Twilio::Response.new(504, '')
expect { @client.validate_ssl_certificate }.to raise_error(Twilio::REST::RestError)
end
it 'translates bad request error params' do
@domain = MyDomain.new(@client)
@version = MyVersion.new(@domain)
@error_message = '{
"code": 20001,
"message": "Bad request",
"more_info": "https://www.twilio.com/docs/errors/20001",
"status": 400,
"details": {
"foo":"bar"
}}'
@holodeck.mock Twilio::Response.new(400, @error_message)
expect {
@version.fetch('GET', 'http://foobar.com')
}.to raise_error { |error|
expect(error).to be_a(Twilio::REST::RestError)
expect(error.status_code).to eq(400)
expect(error.code).to eq(20_001)
expect(error.details).to eq({ 'foo' => 'bar' })
expect(error.error_message).to eq('Bad request')
expect(error.more_info).to eq('https://www.twilio.com/docs/errors/20001')
}
end
end
context 'logging' do
it 'logs the call details' do
@client.logger = Logger.new(STDOUT)
@holodeck.mock Twilio::Response.new(200, {})
expect {
@client.request('host', 'port', 'GET', 'http://foobar.com')
}.to output(/Host:foobar.com/).to_stdout_from_any_process
end
it 'does not log when the logger instance is not passed' do
@holodeck.mock Twilio::Response.new(200, {})
expect {
@client.request('host', 'port', 'GET', 'http://foobar.com')
}.to_not output(/Host:foobar.com/).to_stdout_from_any_process
end
end
describe '#build_uri' do
before(:all) do
Twilio.configure do |config|
config.account_sid = 'username'
config.auth_token = 'password'
config.region = nil
config.edge = nil
end
end
context 'no region or edge in url' do
it "doesn't set region or edge" do
@client = Twilio::REST::Client.new
expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.twilio.com')
end
it 'uses the default region if edge set' do
@client = Twilio::REST::Client.new
@client.edge = 'edge'
expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.edge.us1.twilio.com')
end
it 'sets region' do
@client = Twilio::REST::Client.new
@client.region = 'region'
expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.region.twilio.com')
end
it 'sets region and edge' do
@client = Twilio::REST::Client.new
@client.region = 'region'
@client.edge = 'edge'
expect(@client.build_uri('https://api.twilio.com')).to eq('https://api.edge.region.twilio.com')
end
end
context 'region in url' do
it 'uses url region' do
@client = Twilio::REST::Client.new
expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.urlRegion.twilio.com')
end
it 'uses client edge and url region' do
@client = Twilio::REST::Client.new
@client.edge = 'edge'
expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.edge.urlRegion.twilio.com')
end
it 'prefers client region' do
@client = Twilio::REST::Client.new
@client.region = 'region'
expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.region.twilio.com')
end
it 'uses client edge and prefers client region' do
@client = Twilio::REST::Client.new
@client.region = 'region'
@client.edge = 'edge'
expect(@client.build_uri('https://api.urlRegion.twilio.com')).to eq('https://api.edge.region.twilio.com')
end
end
context 'region and edge in url' do
it 'uses url region and edge' do
@client = Twilio::REST::Client.new
expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.urlEdge.urlRegion.twilio.com')
end
it 'prefers client edge' do
@client = Twilio::REST::Client.new
@client.edge = 'edge'
expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.edge.urlRegion.twilio.com')
end
it 'prefers client region' do
@client = Twilio::REST::Client.new
@client.region = 'region'
expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.urlEdge.region.twilio.com')
end
it 'prefers client region and edge' do
@client = Twilio::REST::Client.new
@client.region = 'region'
@client.edge = 'edge'
expect(@client.build_uri('https://api.urlEdge.urlRegion.twilio.com')).to eq('https://api.edge.region.twilio.com')
end
end
end
context 'user agent in headers' do
before do
@client.http_client = Twilio::HTTP::Client.new
@connection = Faraday::Connection.new
expect(Faraday).to receive(:new).and_yield(@connection).and_return(@connection)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))
end
it 'use default user agent format' do
@client.request('host', 'port', 'GET', 'https://api.twilio.com')
expect(@client.http_client.last_request.headers['User-Agent']).to match %r{^twilio-ruby/[0-9.]+(-rc\.[0-9]+)?\s\([\w-]+\s\w+\)\sRuby/[^\s]+$}
end
it 'add user agent extensions' do
extensions = ['twilio-run/2.0.0-test', 'flex-plugin/3.4.0']
@client.user_agent_extensions = extensions
@client.request('host', 'port', 'GET', 'https://api.twilio.com')
actual_extensions = @client.http_client.last_request.headers['User-Agent'].split(/ /).last(extensions.size)
expect(actual_extensions).to match_array(extensions)
end
end
end