Class: MiddlewareStackTest

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

Defined Under Namespace

Classes: Apple, Banana, Broken, Handler, Orange, Strawberry

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


19
20
21
22
# File 'test/middleware_stack_test.rb', line 19

def setup
  @conn = Faraday::Connection.new
  @builder = @conn.builder
end

#test_allows_extendingObject


37
38
39
40
41
# File 'test/middleware_stack_test.rb', line 37

def test_allows_extending
  build_stack Apple
  @conn.use Orange
  assert_handlers %w[Apple Orange]
end

#test_allows_rebuildingObject


31
32
33
34
35
# File 'test/middleware_stack_test.rb', line 31

def test_allows_rebuilding
  build_stack Apple
  build_stack Orange
  assert_handlers %w[Orange]
end

#test_builder_is_passed_to_new_faraday_connectionObject


43
44
45
46
# File 'test/middleware_stack_test.rb', line 43

def test_builder_is_passed_to_new_faraday_connection
  new_conn = Faraday::Connection.new :builder => @builder
  assert_equal @builder, new_conn.builder
end

#test_delete_handlerObject


66
67
68
69
70
# File 'test/middleware_stack_test.rb', line 66

def test_delete_handler
  build_stack Apple, Orange
  @builder.delete Apple
  assert_handlers %w[Orange]
end

#test_duped_stack_is_unlockedObject


83
84
85
86
87
88
89
90
91
92
# File 'test/middleware_stack_test.rb', line 83

def test_duped_stack_is_unlocked
  build_stack Apple
  assert !@builder.locked?
  @builder.lock!
  assert @builder.locked?

  duped_connection = @conn.dup
  assert_equal @builder, duped_connection.builder
  assert !duped_connection.builder.locked?
end

#test_handler_comparisonObject


94
95
96
97
98
99
# File 'test/middleware_stack_test.rb', line 94

def test_handler_comparison
  build_stack Apple
  assert_equal @builder.handlers.first, Apple
  assert_equal @builder.handlers[0,1], [Apple]
  assert_equal @builder.handlers.first, Faraday::RackBuilder::Handler.new(Apple)
end

#test_insert_afterObject


54
55
56
57
58
# File 'test/middleware_stack_test.rb', line 54

def test_insert_after
  build_stack Apple, Orange
  @builder.insert_after Apple, Banana
  assert_handlers %w[Apple Banana Orange]
end

#test_insert_beforeObject


48
49
50
51
52
# File 'test/middleware_stack_test.rb', line 48

def test_insert_before
  build_stack Apple, Orange
  @builder.insert_before Apple, Banana
  assert_handlers %w[Banana Apple Orange]
end

#test_missing_dependenciesObject


137
138
139
140
141
142
143
144
# File 'test/middleware_stack_test.rb', line 137

def test_missing_dependencies
  build_stack Broken
  err = assert_raises RuntimeError do
    @conn.get('/')
  end
  assert_match "missing dependency for MiddlewareStackTest::Broken: ", err.message
  assert_match "zomg/i_dont/exist", err.message
end

#test_registered_symbolObject


106
107
108
109
110
111
112
113
114
# File 'test/middleware_stack_test.rb', line 106

def test_registered_symbol
  Faraday::Middleware.register_middleware :apple => Apple
  begin
    build_stack :apple
    assert_handlers %w[Apple]
  ensure
    unregister_middleware Faraday::Middleware, :apple
  end
end

#test_registered_symbol_with_arrayObject


126
127
128
129
130
131
132
133
134
135
# File 'test/middleware_stack_test.rb', line 126

def test_registered_symbol_with_array
  Faraday::Middleware.register_middleware File.expand_path("..", __FILE__),
    :strawberry => [lambda { Strawberry }, 'strawberry']
  begin
    build_stack :strawberry
    assert_handlers %w[Strawberry]
  ensure
    unregister_middleware Faraday::Middleware, :strawberry
  end
end

#test_registered_symbol_with_procObject


116
117
118
119
120
121
122
123
124
# File 'test/middleware_stack_test.rb', line 116

def test_registered_symbol_with_proc
  Faraday::Middleware.register_middleware :apple => lambda { Apple }
  begin
    build_stack :apple
    assert_handlers %w[Apple]
  ensure
    unregister_middleware Faraday::Middleware, :apple
  end
end

#test_sets_default_adapter_if_none_setObject


24
25
26
27
28
29
# File 'test/middleware_stack_test.rb', line 24

def test_sets_default_adapter_if_none_set
  default_middleware = Faraday::Request.lookup_middleware :url_encoded
  default_adapter_klass = Faraday::Adapter.lookup_middleware Faraday.default_adapter
  assert @builder[0] == default_middleware
  assert @builder[1] == default_adapter_klass
end

#test_stack_is_locked_after_making_requestsObject


72
73
74
75
76
77
78
79
80
81
# File 'test/middleware_stack_test.rb', line 72

def test_stack_is_locked_after_making_requests
  build_stack Apple
  assert !@builder.locked?
  @conn.get('/')
  assert @builder.locked?

  assert_raises Faraday::RackBuilder::StackLocked do
    @conn.use Orange
  end
end

#test_swap_handlersObject


60
61
62
63
64
# File 'test/middleware_stack_test.rb', line 60

def test_swap_handlers
  build_stack Apple, Orange
  @builder.swap Apple, Banana
  assert_handlers %w[Banana Orange]
end

#test_unregistered_symbolObject


101
102
103
104
# File 'test/middleware_stack_test.rb', line 101

def test_unregistered_symbol
  err = assert_raises(Faraday::Error){ build_stack :apple }
  assert_equal ":apple is not registered on Faraday::Middleware", err.message
end