Class: TestRequestParams

Inherits:
Faraday::TestCase show all
Defined in:
test/connection_test.rb

Instance Method Summary collapse

Methods inherited from Faraday::TestCase

#capture_warnings, jruby?, rbx?, ssl_mode?, #test_default

Methods included from Faraday::LiveServerConfig

#live_server, #live_server=, #live_server?

Instance Method Details

#assert_query_equal(expected, query) ⇒ Object


415
416
417
# File 'test/connection_test.rb', line 415

def assert_query_equal(expected, query)
  assert_equal expected, query.split('&').sort
end

#create_connection(*args) ⇒ Object


405
406
407
408
409
410
411
412
413
# File 'test/connection_test.rb', line 405

def create_connection(*args)
  @conn = Faraday::Connection.new(*args) do |conn|
    yield(conn) if block_given?
    class << conn.builder
      undef app
      def app() lambda { |env| env } end
    end
  end
end

#get(*args) ⇒ Object


516
517
518
519
520
521
# File 'test/connection_test.rb', line 516

def get(*args)
  env = @conn.get(*args) do |req|
    yield(req) if block_given?
  end
  env[:url].query
end

#test_all_overrides_connection_paramsObject


443
444
445
446
447
448
# File 'test/connection_test.rb', line 443

def test_all_overrides_connection_params
  create_connection 'http://a.co/?a=a', :params => {:c => 'c'} do |conn|
    conn.params = {'b' => 'b'}
  end
  assert_query_equal %w[b=b], get
end

#test_array_params_in_paramsObject


492
493
494
495
496
497
498
# File 'test/connection_test.rb', line 492

def test_array_params_in_params
  with_default_params_encoder(nil) do
    create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
    query = get
    assert_equal "color%5B%5D=red&color%5B%5D=blue", query
  end
end

#test_array_params_in_params_with_flat_paramsObject


508
509
510
511
512
513
514
# File 'test/connection_test.rb', line 508

def test_array_params_in_params_with_flat_params
  with_default_params_encoder(Faraday::FlatParamsEncoder) do
    create_connection 'http://a.co/page1', :params => {:color => ['red', 'blue']}
    query = get
    assert_equal "color=red&color=blue", query
  end
end

#test_array_params_in_urlObject


484
485
486
487
488
489
490
# File 'test/connection_test.rb', line 484

def test_array_params_in_url
  with_default_params_encoder(nil) do
    create_connection 'http://a.co/page1?color[]=red&color[]=blue'
    query = get
    assert_equal "color%5B%5D=red&color%5B%5D=blue", query
  end
end

#test_array_params_in_url_with_flat_paramsObject


500
501
502
503
504
505
506
# File 'test/connection_test.rb', line 500

def test_array_params_in_url_with_flat_params
  with_default_params_encoder(Faraday::FlatParamsEncoder) do
    create_connection 'http://a.co/page1?color=red&color=blue'
    query = get
    assert_equal "color=red&color=blue", query
  end
end

#test_merges_connection_and_request_paramsObject


429
430
431
432
433
# File 'test/connection_test.rb', line 429

def test_merges_connection_and_request_params
  create_connection 'http://a.co/?token=abc', :params => {'format' => 'json'}
  query = get '?page=1', :limit => 5
  assert_query_equal %w[format=json limit=5 page=1 token=abc], query
end

#test_overrides_all_request_paramsObject


472
473
474
475
476
477
478
479
480
481
482
# File 'test/connection_test.rb', line 472

def test_overrides_all_request_params
  create_connection :params => {:c => 'c'}
  query = get '?p=1&a=a', :p => 2 do |req|
    assert_equal 'a', req.params[:a]
    assert_equal 'c', req.params['c']
    assert_equal 2, req.params['p']
    req.params = {:b => 'b'}
    assert_equal 'b', req.params['b']
  end
  assert_query_equal %w[b=b], query
end

#test_overrides_connection_paramsObject


435
436
437
438
439
440
441
# File 'test/connection_test.rb', line 435

def test_overrides_connection_params
  create_connection 'http://a.co/?a=a&b=b&c=c', :params => {:a => 'A'} do |conn|
    conn.params[:b] = 'B'
    assert_equal 'c', conn.params[:c]
  end
  assert_query_equal %w[a=A b=B c=c], get
end

#test_overrides_request_paramsObject


450
451
452
453
454
# File 'test/connection_test.rb', line 450

def test_overrides_request_params
  create_connection
  query = get '?p=1&a=a', :p => 2
  assert_query_equal %w[a=a p=2], query
end

#test_overrides_request_params_blockObject


456
457
458
459
460
461
462
# File 'test/connection_test.rb', line 456

def test_overrides_request_params_block
  create_connection
  query = get '?p=1&a=a', :p => 2 do |req|
    req.params[:p] = 3
  end
  assert_query_equal %w[a=a p=3], query
end

#test_overrides_request_params_block_urlObject


464
465
466
467
468
469
470
# File 'test/connection_test.rb', line 464

def test_overrides_request_params_block_url
  create_connection
  query = get nil, :p => 2 do |req|
    req.url '?p=1&a=a', 'p' => 3
  end
  assert_query_equal %w[a=a p=3], query
end

#with_default_params_encoder(encoder) ⇒ Object


419
420
421
422
423
424
425
426
427
# File 'test/connection_test.rb', line 419

def with_default_params_encoder(encoder)
  old_encoder = Faraday::Utils.default_params_encoder
  begin
    Faraday::Utils.default_params_encoder = encoder
    yield
  ensure
    Faraday::Utils.default_params_encoder = old_encoder
  end
end