-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathhttp_client_spec.rb
More file actions
188 lines (151 loc) · 8.03 KB
/
http_client_spec.rb
File metadata and controls
188 lines (151 loc) · 8.03 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
require 'spec_helper'
require 'rack/mock'
describe Twilio::HTTP::Client do
before do
@client = Twilio::HTTP::Client.new
end
it 'should allow adding a request configuration block' do
@client = Twilio::HTTP::Client.new
@connection = Faraday::Connection.new
blocks_spy = spy('blocks')
@client.configure_connection do |f|
blocks_spy.first_block_called(f)
end
@client.configure_connection do |f|
blocks_spy.second_block_called(f)
end
allow(Faraday).to receive(:new).and_yield(@connection).and_return(@connection)
allow(@connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])
expect(blocks_spy).to have_received(:first_block_called).with(@connection)
expect(blocks_spy).to have_received(:second_block_called).with(@connection)
end
it 'should allow the configuration block to set the connection adapter' do
@client = Twilio::HTTP::Client.new
@connection = Faraday::Connection.new
stub_const('TestAdapter', Class.new(Faraday::Adapter))
Faraday::Adapter.register_middleware test_adapter: TestAdapter
@client.configure_connection do |f|
f.adapter :test_adapter
end
allow(Faraday).to receive(:new).and_yield(@connection).and_return(@connection)
allow(@connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])
expect(@connection.adapter).to eq TestAdapter
end
it 'should allow setting a global timeout' do
@client = Twilio::HTTP::Client.new(timeout: 10)
@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: {}))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])
expect(@client.timeout).to eq(10)
expect(@connection.options.open_timeout).to eq(10)
expect(@connection.options.timeout).to eq(10)
end
it 'should allow overriding timeout per request' do
@client = Twilio::HTTP::Client.new(timeout: 10)
@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: {}))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'], 20)
expect(@client.timeout).to eq(10)
expect(@connection.options.open_timeout).to eq(20)
expect(@connection.options.timeout).to eq(20)
end
it 'should contain a last response' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: { something: '1' }))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])
expect(@client.last_response).to_not be_nil
expect(@client.last_response.is_a?(Twilio::Response)).to be(true)
expect(@client.last_response.status_code).to eq(301)
expect(@client.last_response.headers).to eq(something: '1')
end
it 'should contain a last request' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))
@client.request('host',
'port',
'GET',
'url',
{ 'param-key' => 'param-value' },
{ 'data-key' => 'data-value' },
{ 'header-key' => 'header-value' },
['a', 'b'],
'timeout')
expect(@client.last_request).to_not be_nil
expect(@client.last_request.is_a?(Twilio::Request)).to be(true)
expect(@client.last_request.host).to eq('host')
expect(@client.last_request.port).to eq('port')
expect(@client.last_request.method).to eq('GET')
expect(@client.last_request.url).to eq('url')
expect(@client.last_request.params).to eq('param-key' => 'param-value')
expect(@client.last_request.data).to eq('data-key' => 'data-value')
expect(@client.last_request.headers).to eq('header-key' => 'header-value')
expect(@client.last_request.auth).to eq(['a', 'b'])
expect(@client.last_request.timeout).to eq('timeout')
end
it 'should contain a last response for 5XX status classes' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 500, body: {}, headers: {}))
@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])
expect(@client.last_response).to_not be_nil
expect(@client.last_request.host).to eq('host')
expect(@client.last_request.port).to eq('port')
expect(@client.last_request.method).to eq('GET')
expect(@client.last_request.url).to eq('url')
expect(@client.last_request.params).to be_nil
expect(@client.last_request.data).to be_nil
expect(@client.last_request.headers).to eq({})
expect(@client.last_request.auth).to eq(['a', 'b'])
expect(@client.last_request.timeout).to be_nil
expect(@client.last_response.is_a?(Twilio::Response)).to be(true)
expect(@client.last_response.status_code).to eq(500)
end
it 'should contain a last_response but no response on a connection error' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_raise(Faraday::ConnectionFailed.new('BOOM'))
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Twilio::REST::TwilioError)
expect(@client.last_response).to be_nil
expect(@client.last_request).to_not be_nil
expect(@client.last_request.host).to eq('host')
expect(@client.last_request.port).to eq('port')
expect(@client.last_request.method).to eq('GET')
expect(@client.last_request.url).to eq('url')
expect(@client.last_request.params).to be_nil
expect(@client.last_request.data).to be_nil
expect(@client.last_request.headers).to eq({})
expect(@client.last_request.auth).to eq(['a', 'b'])
expect(@client.last_request.timeout).to be_nil
end
it 'should contain params in Connection object' do
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: { something: '1' }))
params = Twilio::Values.of({ 'abc' => 'xyz' })
@client.request('host', 'port', 'DELETE', 'url', params, nil, {}, ['a', 'b'])
expect(@client.connection.params).to eq({ 'abc' => 'xyz' })
end
describe 'last_response' do
let(:last_response) { Twilio::Response.new(200, 'body') }
end
it 'previous last_response should be cleared' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_raise(Faraday::ConnectionFailed.new('BOOM'))
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Twilio::REST::TwilioError)
expect(@client.last_response).to be_nil
end
context 'when there is a server error' do
let(:faraday_connection) { Faraday::Connection.new }
let(:twilio_response) { @client.request('host', 'port', 'GET', 'url') }
before do
allow(Faraday).to receive(:new).and_return(faraday_connection)
end
(500..599).each do |status_code|
it 'generates a JSON-parseable body' do
allow(faraday_connection).to receive(:send).and_return(double('response', status: status_code, body: { message: 'any message' }, headers: {}))
expect(twilio_response.body).to eq({ 'message' => "Server error (#{status_code})", 'code' => status_code })
expect(twilio_response.status_code).to eq status_code
end
end
end
end