Class: Adapters::TestMiddleware

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

Constant Summary collapse

Stubs =
Faraday::Adapter.lookup_middleware(:test)::Stubs

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

#setupObject


6
7
8
9
10
11
12
13
# File 'test/adapters/test_middleware_test.rb', line 6

def setup
  @stubs = Stubs.new
  @conn  = Faraday.new do |builder|
    builder.adapter :test, @stubs
  end
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
  @resp = @conn.get('/hello')
end

#test_middleware_allow_different_outcomes_for_the_same_requestObject


52
53
54
55
56
57
# File 'test/adapters/test_middleware_test.rb', line 52

def test_middleware_allow_different_outcomes_for_the_same_request
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
  @stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
  assert_equal 'hello', @conn.get("/hello").body
  assert_equal 'world', @conn.get("/hello").body
end

#test_middleware_can_be_called_several_timesObject


27
28
29
# File 'test/adapters/test_middleware_test.rb', line 27

def test_middleware_can_be_called_several_times
  assert_equal 'hello', @conn.get("/hello").body
end

#test_middleware_ignores_unspecified_get_paramsObject


36
37
38
39
40
41
42
43
# File 'test/adapters/test_middleware_test.rb', line 36

def test_middleware_ignores_unspecified_get_params
  @stubs.get('/optional?a=1') { [200, {}, 'a'] }
  assert_equal 'a', @conn.get('/optional?a=1&b=1').body
  assert_equal 'a', @conn.get('/optional?a=1').body
  assert_raises Faraday::Adapter::Test::Stubs::NotFound do
    @conn.get('/optional')
  end
end

#test_middleware_with_get_paramsObject


31
32
33
34
# File 'test/adapters/test_middleware_test.rb', line 31

def test_middleware_with_get_params
  @stubs.get('/param?a=1') { [200, {}, 'a'] }
  assert_equal 'a', @conn.get('/param?a=1').body
end

#test_middleware_with_http_headersObject


45
46
47
48
49
50
# File 'test/adapters/test_middleware_test.rb', line 45

def test_middleware_with_http_headers
  @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
  @stubs.get('/yo') { [200, {}, 'b'] }
  assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
  assert_equal 'b', @conn.get('/yo').body
end

#test_middleware_with_simple_path_sets_bodyObject


23
24
25
# File 'test/adapters/test_middleware_test.rb', line 23

def test_middleware_with_simple_path_sets_body
  assert_equal 'hello', @resp.body
end

#test_middleware_with_simple_path_sets_headersObject


19
20
21
# File 'test/adapters/test_middleware_test.rb', line 19

def test_middleware_with_simple_path_sets_headers
  assert_equal 'text/html', @resp.headers['Content-Type']
end

#test_middleware_with_simple_path_sets_statusObject


15
16
17
# File 'test/adapters/test_middleware_test.rb', line 15

def test_middleware_with_simple_path_sets_status
  assert_equal 200, @resp.status
end

#test_parses_params_with_default_encoderObject


72
73
74
75
76
77
78
79
# File 'test/adapters/test_middleware_test.rb', line 72

def test_parses_params_with_default_encoder
  @stubs.get '/hello' do |env|
    assert_equal '1', env[:params]['a']['b']
    [200, {}, 'a']
  end

  assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

#test_parses_params_with_flat_encoderObject


91
92
93
94
95
96
97
98
99
# File 'test/adapters/test_middleware_test.rb', line 91

def test_parses_params_with_flat_encoder
  @stubs.get '/hello' do |env|
    assert_equal '1', env[:params]['a[b]']
    [200, {}, 'a']
  end

  @conn.options.params_encoder = Faraday::FlatParamsEncoder
  assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

#test_parses_params_with_nested_encoderObject


81
82
83
84
85
86
87
88
89
# File 'test/adapters/test_middleware_test.rb', line 81

def test_parses_params_with_nested_encoder
  @stubs.get '/hello' do |env|
    assert_equal '1', env[:params]['a']['b']
    [200, {}, 'a']
  end

  @conn.options.params_encoder = Faraday::NestedParamsEncoder
  assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end

#test_raises_an_error_if_no_stub_is_found_for_requestObject


101
102
103
104
105
# File 'test/adapters/test_middleware_test.rb', line 101

def test_raises_an_error_if_no_stub_is_found_for_request
  assert_raises Stubs::NotFound do
    @conn.get('/invalid'){ [200, {}, []] }
  end
end

#test_raises_an_error_if_no_stub_is_found_for_request_without_this_headerObject


107
108
109
110
111
112
# File 'test/adapters/test_middleware_test.rb', line 107

def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
  @stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
  assert_raises Faraday::Adapter::Test::Stubs::NotFound do
    @conn.get('/yo')
  end
end

#test_yields_env_to_stubsObject


59
60
61
62
63
64
65
66
67
68
69
70
# File 'test/adapters/test_middleware_test.rb', line 59

def test_yields_env_to_stubs
  @stubs.get '/hello' do |env|
    assert_equal '/hello',     env[:url].path
    assert_equal 'foo.com',    env[:url].host
    assert_equal '1',          env[:params]['a']
    assert_equal 'text/plain', env[:request_headers]['Accept']
    [200, {}, 'a']
  end

  @conn.headers['Accept'] = 'text/plain'
  assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
end