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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
# frozen_string_literal: true
RSpec.describe Faraday::Adapter::Test do
let(:stubs) do
described_class::Stubs.new do |stub|
stub.get('http://domain.test/hello') do
[200, { 'Content-Type' => 'text/html' }, 'domain: hello']
end
stub.get('http://wrong.test/hello') do
[200, { 'Content-Type' => 'text/html' }, 'wrong: hello']
end
stub.get('http://wrong.test/bait') do
[404, { 'Content-Type' => 'text/html' }]
end
stub.get('/hello') do
[200, { 'Content-Type' => 'text/html' }, 'hello']
end
stub.get('/method-echo') do |env|
[200, { 'Content-Type' => 'text/html' }, env[:method].to_s]
end
stub.get(%r{\A/resources/\d+(?:\?|\z)}) do
[200, { 'Content-Type' => 'text/html' }, 'show']
end
stub.get(%r{\A/resources/(specified)\z}) do |_env, meta|
[200, { 'Content-Type' => 'text/html' }, "show #{meta[:match_data][1]}"]
end
end
end
let(:connection) do
Faraday.new do |builder|
builder.adapter :test, stubs
end
end
let(:response) { connection.get('/hello') }
context 'with simple path sets status' do
subject { response.status }
it { is_expected.to eq 200 }
end
context 'with simple path sets headers' do
subject { response.headers['Content-Type'] }
it { is_expected.to eq 'text/html' }
end
context 'with simple path sets body' do
subject { response.body }
it { is_expected.to eq 'hello' }
end
context 'with host points to the right stub' do
subject { connection.get('http://domain.test/hello').body }
it { is_expected.to eq 'domain: hello' }
end
describe 'can be called several times' do
subject { connection.get('/hello').body }
it { is_expected.to eq 'hello' }
end
describe 'can handle regular expression path' do
subject { connection.get('/resources/1').body }
it { is_expected.to eq 'show' }
end
describe 'can handle single parameter block' do
subject { connection.get('/method-echo').body }
it { is_expected.to eq 'get' }
end
describe 'can handle regular expression path with captured result' do
subject { connection.get('/resources/specified').body }
it { is_expected.to eq 'show specified' }
end
context 'with get params' do
subject { connection.get('/param?a=1').body }
before do
stubs.get('/param?a=1') { [200, {}, 'a'] }
end
it { is_expected.to eq 'a' }
end
describe 'ignoring unspecified get params' do
before do
stubs.get('/optional?a=1') { [200, {}, 'a'] }
end
context 'with multiple params' do
subject { connection.get('/optional?a=1&b=1').body }
it { is_expected.to eq 'a' }
end
context 'with single param' do
subject { connection.get('/optional?a=1').body }
it { is_expected.to eq 'a' }
end
context 'without params' do
subject(:request) { connection.get('/optional') }
it do
expect { request }.to raise_error(
Faraday::Adapter::Test::Stubs::NotFound
)
end
end
end
context 'with http headers' do
before do
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
stubs.get('/yo') { [200, {}, 'b'] }
end
context 'with header' do
subject do
connection.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
end
it { is_expected.to eq 'a' }
end
context 'without header' do
subject do
connection.get('/yo').body
end
it { is_expected.to eq 'b' }
end
end
describe 'different outcomes for the same request' do
def make_request
connection.get('/foo')
end
subject(:request) { make_request.body }
before do
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
stubs.get('/foo') { [200, { 'Content-Type' => 'text/html' }, 'world'] }
end
context 'the first request' do
it { is_expected.to eq 'hello' }
end
context 'the second request' do
before do
make_request
end
it { is_expected.to eq 'world' }
end
end
describe 'yielding env to stubs' do
subject { connection.get('http://foo.com/foo?a=1').body }
before do
stubs.get '/foo' do |env|
expect(env[:url].path).to eq '/foo'
expect(env[:url].host).to eq 'foo.com'
expect(env[:params]['a']).to eq '1'
expect(env[:request_headers]['Accept']).to eq 'text/plain'
[200, {}, 'a']
end
connection.headers['Accept'] = 'text/plain'
end
it { is_expected.to eq 'a' }
end
describe 'params parsing' do
subject { connection.get('http://foo.com/foo?a[b]=1').body }
context 'with default encoder' do
before do
stubs.get '/foo' do |env|
expect(env[:params]['a']['b']).to eq '1'
[200, {}, 'a']
end
end
it { is_expected.to eq 'a' }
end
context 'with nested encoder' do
before do
stubs.get '/foo' do |env|
expect(env[:params]['a']['b']).to eq '1'
[200, {}, 'a']
end
connection.options.params_encoder = Faraday::NestedParamsEncoder
end
it { is_expected.to eq 'a' }
end
context 'with flat encoder' do
before do
stubs.get '/foo' do |env|
expect(env[:params]['a[b]']).to eq '1'
[200, {}, 'a']
end
connection.options.params_encoder = Faraday::FlatParamsEncoder
end
it { is_expected.to eq 'a' }
end
end
describe 'raising an error if no stub was found' do
describe 'for request' do
subject(:request) { connection.get('/invalid') { [200, {}, []] } }
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
describe 'for specified host' do
subject(:request) { connection.get('http://domain.test/bait') }
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
describe 'for request without specified header' do
subject(:request) { connection.get('/yo') }
before do
stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
end
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
end
describe 'for request with non default params encoder' do
let(:connection) do
Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
builder.adapter :test, stubs
end
end
let(:stubs) do
described_class::Stubs.new do |stubs|
stubs.get('/path?a=x&a=y&a=z') { [200, {}, 'a'] }
end
end
context 'when all flat param values are correctly set' do
subject(:request) { connection.get('/path?a=x&a=y&a=z') }
it { expect(request.status).to eq 200 }
end
shared_examples 'raise NotFound when params do not satisfy the flat param values' do |params|
subject(:request) { connection.get('/path', params) }
context "with #{params.inspect}" do
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
end
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x] }
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x y] }
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { a: %w[x z y] } # NOTE: The order of the value is also compared.
it_behaves_like 'raise NotFound when params do not satisfy the flat param values', { b: %w[x y z] }
end
describe 'strict_mode' do
let(:stubs) do
described_class::Stubs.new(strict_mode: true) do |stubs|
stubs.get('/strict?a=12&b=xy', 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello') { [200, {}, 'a'] }
stubs.get('/with_user_agent?a=12&b=xy', authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent') { [200, {}, 'a'] }
end
end
context 'when params and headers are exactly set' do
subject(:request) { connection.get('/strict', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', x_c: 'hello' }) }
it { expect(request.status).to eq 200 }
end
context 'when params and headers are exactly set with a custom user agent' do
subject(:request) { connection.get('/with_user_agent', { a: '12', b: 'xy' }, { authorization: 'Bearer m_ck', 'User-Agent' => 'My Agent' }) }
it { expect(request.status).to eq 200 }
end
shared_examples 'raise NotFound when params do not satisfy the strict check' do |params|
subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
context "with #{params.inspect}" do
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
end
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12' }
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { b: 'xy' }
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '123', b: 'xy' }
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xyz' }
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
it_behaves_like 'raise NotFound when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
shared_examples 'raise NotFound when headers do not satisfy the strict check' do |path, headers|
subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
context "with #{headers.inspect}" do
it { expect { request }.to raise_error described_class::Stubs::NotFound }
end
end
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { 'X-C' => 'hello' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'Hi' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Basic m_ck', 'x-c': 'hello' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'Unknown' }
it_behaves_like 'raise NotFound when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
context 'when strict_mode is disabled' do
before do
stubs.strict_mode = false
end
shared_examples 'does not raise NotFound even when params do not satisfy the strict check' do |params|
subject(:request) { connection.get('/strict', params, { 'Authorization' => 'Bearer m_ck', 'X-C' => 'hello' }) }
context "with #{params.inspect}" do
it { expect(request.status).to eq 200 }
end
end
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy' }
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { a: '12', b: 'xy', c: 'hello' }
it_behaves_like 'does not raise NotFound even when params do not satisfy the strict check', { additional: 'special', a: '12', b: 'xy', c: 'hello' }
shared_examples 'does not raise NotFound even when headers do not satisfy the strict check' do |path, headers|
subject(:request) { connection.get(path, { a: 12, b: 'xy' }, headers) }
context "with #{headers.inspect}" do
it { expect(request.status).to eq 200 }
end
end
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello' }
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', x_special: 'special' }
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/strict', { authorization: 'Bearer m_ck', 'x-c': 'hello', user_agent: 'Special Agent' }
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent' }
it_behaves_like 'does not raise NotFound even when headers do not satisfy the strict check', '/with_user_agent', { authorization: 'Bearer m_ck', user_agent: 'My Agent', x_special: 'special' }
end
describe 'body_match?' do
let(:stubs) do
described_class::Stubs.new do |stubs|
stubs.post('/no_check') { [200, {}, 'ok'] }
stubs.post('/with_string', 'abc') { [200, {}, 'ok'] }
stubs.post(
'/with_proc',
->(request_body) { JSON.parse(request_body, symbolize_names: true) == { x: '!', a: [{ m: [{ a: true }], n: 123 }] } },
{ content_type: 'application/json' }
) do
[200, {}, 'ok']
end
end
end
context 'when trying without any args for body' do
subject(:without_body) { connection.post('/no_check') }
it { expect(without_body.status).to eq 200 }
end
context 'when trying with string body stubs' do
subject(:with_string) { connection.post('/with_string', 'abc') }
it { expect(with_string.status).to eq 200 }
end
context 'when trying with proc body stubs' do
subject(:with_proc) do
connection.post('/with_proc', JSON.dump(a: [{ n: 123, m: [{ a: true }] }], x: '!'), { 'Content-Type' => 'application/json' })
end
it { expect(with_proc.status).to eq 200 }
end
end
end
describe 'request timeout' do
subject(:request) do
connection.get('/sleep') do |req|
req.options.timeout = timeout
end
end
before do
stubs.get('/sleep') do
sleep(0.01)
[200, {}, '']
end
end
context 'when request is within timeout' do
let(:timeout) { 1 }
it { expect(request.status).to eq 200 }
end
context 'when request is too slow' do
let(:timeout) { 0.001 }
it 'raises an exception' do
expect { request }.to raise_error(Faraday::TimeoutError)
end
end
end
end
|