faraday 0.17.0 → 0.17.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +216 -0
- data/Rakefile +13 -0
- data/lib/faraday.rb +1 -1
- data/lib/faraday/adapter/em_http.rb +9 -9
- data/lib/faraday/adapter/em_synchrony.rb +5 -5
- data/lib/faraday/adapter/excon.rb +3 -3
- data/lib/faraday/adapter/httpclient.rb +4 -4
- data/lib/faraday/adapter/net_http.rb +2 -2
- data/lib/faraday/adapter/net_http_persistent.rb +3 -3
- data/lib/faraday/adapter/patron.rb +5 -5
- data/lib/faraday/adapter/rack.rb +1 -1
- data/lib/faraday/deprecate.rb +101 -0
- data/lib/faraday/error.rb +85 -32
- data/lib/faraday/options.rb +1 -1
- data/lib/faraday/request/retry.rb +6 -5
- data/lib/faraday/response.rb +3 -3
- data/lib/faraday/response/raise_error.rb +7 -3
- data/spec/faraday/deprecate_spec.rb +69 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +95 -0
- data/spec/spec_helper.rb +104 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +30 -0
- data/test/adapters/em_synchrony_test.rb +32 -0
- data/test/adapters/excon_test.rb +30 -0
- data/test/adapters/httpclient_test.rb +34 -0
- data/test/adapters/integration.rb +263 -0
- data/test/adapters/logger_test.rb +136 -0
- data/test/adapters/net_http_persistent_test.rb +114 -0
- data/test/adapters/net_http_test.rb +79 -0
- data/test/adapters/patron_test.rb +40 -0
- data/test/adapters/rack_test.rb +38 -0
- data/test/adapters/test_middleware_test.rb +157 -0
- data/test/adapters/typhoeus_test.rb +38 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +109 -0
- data/test/connection_test.rb +738 -0
- data/test/env_test.rb +268 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +67 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +282 -0
- data/test/middleware_stack_test.rb +260 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +333 -0
- data/test/parameters_test.rb +157 -0
- data/test/request_middleware_test.rb +126 -0
- data/test/response_middleware_test.rb +72 -0
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +98 -0
- metadata +47 -5
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
require 'stringio'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Adapters
|
6
|
+
class LoggerTest < Faraday::TestCase
|
7
|
+
def conn(logger, logger_options={})
|
8
|
+
rubbles = ['Barney', 'Betty', 'Bam Bam']
|
9
|
+
|
10
|
+
Faraday.new do |b|
|
11
|
+
b.response :logger, @logger, logger_options do | logger |
|
12
|
+
logger.filter(/(soylent green is) (.+)/,'\1 tasty')
|
13
|
+
logger.filter(/(api_key:).*"(.+)."/,'\1[API_KEY]')
|
14
|
+
logger.filter(/(password)=(.+)/,'\1=[HIDDEN]')
|
15
|
+
end
|
16
|
+
b.adapter :test do |stubs|
|
17
|
+
stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
18
|
+
stubs.post('/ohai') { [200, {'Content-Type' => 'text/html'}, 'fred'] }
|
19
|
+
stubs.post('/ohyes') { [200, {'Content-Type' => 'text/html'}, 'pebbles'] }
|
20
|
+
stubs.get('/rubbles') { [200, {'Content-Type' => 'application/json'}, rubbles] }
|
21
|
+
stubs.get('/filtered_body') { [200, {'Content-Type' => 'text/html'}, 'soylent green is people'] }
|
22
|
+
stubs.get('/filtered_headers') { [200, {'Content-Type' => 'text/html'}, 'headers response'] }
|
23
|
+
stubs.get('/filtered_params') { [200, {'Content-Type' => 'text/html'}, 'params response'] }
|
24
|
+
stubs.get('/filtered_url') { [200, {'Content-Type' => 'text/html'}, 'url response'] }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@io = StringIO.new
|
31
|
+
@logger = Logger.new(@io)
|
32
|
+
@logger.level = Logger::DEBUG
|
33
|
+
|
34
|
+
@conn = conn(@logger)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_still_returns_output
|
38
|
+
resp = @conn.get '/hello', nil, :accept => 'text/html'
|
39
|
+
assert_equal 'hello', resp.body
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_logs_method_and_url
|
43
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
44
|
+
assert_match 'request: GET http:/hello', @io.string
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_logs_status_code
|
48
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
49
|
+
assert_match 'response: Status 200', @io.string
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_logs_request_headers_by_default
|
53
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
54
|
+
assert_match %(Accept: "text/html), @io.string
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_logs_response_headers_by_default
|
58
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
59
|
+
assert_match %(Content-Type: "text/html), @io.string
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_does_not_log_request_headers_if_option_is_false
|
63
|
+
app = conn(@logger, :headers => { :request => false })
|
64
|
+
app.get '/hello', nil, :accept => 'text/html'
|
65
|
+
refute_match %(Accept: "text/html), @io.string
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_does_log_response_headers_if_option_is_false
|
69
|
+
app = conn(@logger, :headers => { :response => false })
|
70
|
+
app.get '/hello', nil, :accept => 'text/html'
|
71
|
+
refute_match %(Content-Type: "text/html), @io.string
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_does_not_log_request_body_by_default
|
75
|
+
@conn.post '/ohai', 'name=Unagi', :accept => 'text/html'
|
76
|
+
refute_match %(name=Unagi), @io.string
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_does_not_log_response_body_by_default
|
80
|
+
@conn.post '/ohai', 'name=Toro', :accept => 'text/html'
|
81
|
+
refute_match %(fred), @io.string
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_log_only_request_body
|
85
|
+
app = conn(@logger, :bodies => { :request => true })
|
86
|
+
app.post '/ohyes', 'name=Tamago', :accept => 'text/html'
|
87
|
+
assert_match %(name=Tamago), @io.string
|
88
|
+
refute_match %(pebbles), @io.string
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_log_only_response_body
|
92
|
+
app = conn(@logger, :bodies => { :response => true })
|
93
|
+
app.post '/ohyes', 'name=Hamachi', :accept => 'text/html'
|
94
|
+
assert_match %(pebbles), @io.string
|
95
|
+
refute_match %(name=Hamachi), @io.string
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_log_request_and_response_body
|
99
|
+
app = conn(@logger, :bodies => true)
|
100
|
+
app.post '/ohyes', 'name=Ebi', :accept => 'text/html'
|
101
|
+
assert_match %(name=Ebi), @io.string
|
102
|
+
assert_match %(pebbles), @io.string
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_log_response_body_object
|
106
|
+
app = conn(@logger, :bodies => true)
|
107
|
+
app.get '/rubbles', nil, :accept => 'text/html'
|
108
|
+
assert_match %([\"Barney\", \"Betty\", \"Bam Bam\"]\n), @io.string
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_logs_filter_body
|
112
|
+
app = conn(@logger, :bodies => true)
|
113
|
+
app.get '/filtered_body', nil, :accept => 'text/html'
|
114
|
+
assert_match %(soylent green is), @io.string
|
115
|
+
assert_match %(tasty), @io.string
|
116
|
+
refute_match %(people), @io.string
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_logs_filter_headers
|
120
|
+
app = conn(@logger)
|
121
|
+
app.headers = {'api_key' => 'ABC123'}
|
122
|
+
app.get '/filtered_headers', nil, :accept => 'text/html'
|
123
|
+
assert_match %(api_key:), @io.string
|
124
|
+
assert_match %([API_KEY]), @io.string
|
125
|
+
refute_match %(ABC123), @io.string
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_logs_filter_url
|
129
|
+
app = conn(@logger)
|
130
|
+
app.get '/filtered_url?password=hunter2', nil, :accept => 'text/html'
|
131
|
+
assert_match %(password=[HIDDEN]), @io.string
|
132
|
+
refute_match %(hunter2), @io.string
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class NetHttpPersistentTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :net_http_persistent end
|
7
|
+
|
8
|
+
Integration.apply(self, :NonParallel) do
|
9
|
+
def setup
|
10
|
+
if defined?(Net::HTTP::Persistent)
|
11
|
+
# work around problems with mixed SSL certificates
|
12
|
+
# https://github.com/drbrain/net-http-persistent/issues/45
|
13
|
+
if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
|
14
|
+
Net::HTTP::Persistent.new(name: 'Faraday').reconnect_ssl
|
15
|
+
else
|
16
|
+
Net::HTTP::Persistent.new('Faraday').ssl_cleanup(4)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end if ssl_mode?
|
20
|
+
|
21
|
+
def test_reuses_tcp_sockets
|
22
|
+
# Ensure that requests are not reused from previous tests
|
23
|
+
Thread.current.keys
|
24
|
+
.select { |key| key.to_s =~ /\Anet_http_persistent_Faraday_/ }
|
25
|
+
.each { |key| Thread.current[key] = nil }
|
26
|
+
|
27
|
+
sockets = []
|
28
|
+
tcp_socket_open_wrapped = Proc.new do |*args, &block|
|
29
|
+
socket = TCPSocket.__minitest_stub__open(*args, &block)
|
30
|
+
sockets << socket
|
31
|
+
socket
|
32
|
+
end
|
33
|
+
|
34
|
+
TCPSocket.stub :open, tcp_socket_open_wrapped do
|
35
|
+
conn = create_connection
|
36
|
+
conn.post("/echo", :foo => "bar")
|
37
|
+
conn.post("/echo", :foo => "baz")
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal 1, sockets.count
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_does_not_reuse_tcp_sockets_when_proxy_changes
|
44
|
+
# Ensure that requests are not reused from previous tests
|
45
|
+
Thread.current.keys
|
46
|
+
.select { |key| key.to_s =~ /\Anet_http_persistent_Faraday_/ }
|
47
|
+
.each { |key| Thread.current[key] = nil }
|
48
|
+
|
49
|
+
sockets = []
|
50
|
+
tcp_socket_open_wrapped = Proc.new do |*args, &block|
|
51
|
+
socket = TCPSocket.__minitest_stub__open(*args, &block)
|
52
|
+
sockets << socket
|
53
|
+
socket
|
54
|
+
end
|
55
|
+
|
56
|
+
TCPSocket.stub :open, tcp_socket_open_wrapped do
|
57
|
+
conn = create_connection
|
58
|
+
conn.post("/echo", :foo => "bar")
|
59
|
+
conn.proxy = URI(ENV["LIVE_PROXY"])
|
60
|
+
conn.post("/echo", :foo => "bar")
|
61
|
+
end
|
62
|
+
|
63
|
+
assert_equal 2, sockets.count
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_without_custom_connection_config
|
68
|
+
url = URI('https://example.com:1234')
|
69
|
+
|
70
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new
|
71
|
+
|
72
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
73
|
+
|
74
|
+
# `pool` is only present in net_http_persistent >= 3.0
|
75
|
+
assert http.pool.size != nil if http.respond_to?(:pool)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_custom_connection_config
|
79
|
+
url = URI('https://example.com:1234')
|
80
|
+
|
81
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new(nil, {pool_size: 5})
|
82
|
+
|
83
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
84
|
+
|
85
|
+
# `pool` is only present in net_http_persistent >= 3.0
|
86
|
+
assert_equal 5, http.pool.size if http.respond_to?(:pool)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_custom_adapter_config
|
90
|
+
url = URI('https://example.com:1234')
|
91
|
+
|
92
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new do |http|
|
93
|
+
http.idle_timeout = 123
|
94
|
+
end
|
95
|
+
|
96
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
97
|
+
adapter.send(:configure_request, http, {})
|
98
|
+
|
99
|
+
assert_equal 123, http.idle_timeout
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_max_retries
|
103
|
+
url = URI('http://example.com')
|
104
|
+
|
105
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new
|
106
|
+
|
107
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
108
|
+
adapter.send(:configure_request, http, {})
|
109
|
+
|
110
|
+
# `max_retries=` is only present in Ruby 2.5
|
111
|
+
assert_equal 0, http.max_retries if http.respond_to?(:max_retries=)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
require 'ostruct'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Adapters
|
6
|
+
class NetHttpTest < Faraday::TestCase
|
7
|
+
|
8
|
+
def adapter() :net_http end
|
9
|
+
|
10
|
+
behaviors = [:NonParallel, :Compression]
|
11
|
+
|
12
|
+
Integration.apply(self, *behaviors)
|
13
|
+
|
14
|
+
def test_no_explicit_http_port_number
|
15
|
+
url = URI('http://example.com')
|
16
|
+
url.port = nil
|
17
|
+
|
18
|
+
adapter = Faraday::Adapter::NetHttp.new
|
19
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
20
|
+
|
21
|
+
assert_equal 80, http.port
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_no_explicit_https_port_number
|
25
|
+
url = URI('https://example.com')
|
26
|
+
url.port = nil
|
27
|
+
|
28
|
+
adapter = Faraday::Adapter::NetHttp.new
|
29
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
30
|
+
|
31
|
+
assert_equal 443, http.port
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_explicit_port_number
|
35
|
+
url = URI('https://example.com:1234')
|
36
|
+
|
37
|
+
adapter = Faraday::Adapter::NetHttp.new
|
38
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
39
|
+
|
40
|
+
assert_equal 1234, http.port
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_custom_adapter_config
|
44
|
+
url = URI('https://example.com:1234')
|
45
|
+
|
46
|
+
adapter = Faraday::Adapter::NetHttp.new do |http|
|
47
|
+
http.continue_timeout = 123
|
48
|
+
end
|
49
|
+
|
50
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
51
|
+
adapter.send(:configure_request, http, {})
|
52
|
+
|
53
|
+
assert_equal 123, http.continue_timeout
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_no_retries
|
57
|
+
url = URI('http://example.com')
|
58
|
+
|
59
|
+
adapter = Faraday::Adapter::NetHttp.new
|
60
|
+
|
61
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
62
|
+
adapter.send(:configure_request, http, {})
|
63
|
+
|
64
|
+
# `max_retries=` is only present in Ruby 2.5
|
65
|
+
assert_equal 0, http.max_retries if http.respond_to?(:max_retries=)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_write_timeout
|
69
|
+
url = URI('http://example.com')
|
70
|
+
|
71
|
+
adapter = Faraday::Adapter::NetHttp.new
|
72
|
+
|
73
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
74
|
+
adapter.send(:configure_request, http, { write_timeout: 10 })
|
75
|
+
|
76
|
+
assert_equal 10, http.write_timeout if http.respond_to?(:write_timeout=)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class Patron < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :patron end
|
7
|
+
|
8
|
+
unless jruby?
|
9
|
+
Integration.apply(self, :NonParallel) do
|
10
|
+
# https://github.com/toland/patron/issues/34
|
11
|
+
undef :test_PATCH_send_url_encoded_params
|
12
|
+
|
13
|
+
# https://github.com/toland/patron/issues/52
|
14
|
+
undef :test_GET_with_body
|
15
|
+
|
16
|
+
# no support for SSL peer verification
|
17
|
+
undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_custom_adapter_config
|
21
|
+
conn = create_connection do |session|
|
22
|
+
assert_kind_of ::Patron::Session, session
|
23
|
+
session.max_redirects = 10
|
24
|
+
throw :config_block_called
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_throws(:config_block_called) do
|
28
|
+
conn.get 'http://8.8.8.8:88'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_connection_timeout
|
33
|
+
conn = create_connection(:request => {:timeout => 10, :open_timeout => 1})
|
34
|
+
assert_raises Faraday::ConnectionFailed do
|
35
|
+
conn.get 'http://8.8.8.8:88'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path("../integration", __FILE__)
|
2
|
+
require File.expand_path('../../live_server', __FILE__)
|
3
|
+
|
4
|
+
module Adapters
|
5
|
+
class RackTest < Faraday::TestCase
|
6
|
+
|
7
|
+
def adapter() :rack end
|
8
|
+
|
9
|
+
def adapter_options
|
10
|
+
[Faraday::LiveServer]
|
11
|
+
end
|
12
|
+
|
13
|
+
# no Integration.apply because this doesn't require a server as a separate process
|
14
|
+
include Integration::Common
|
15
|
+
include Integration::NonParallel
|
16
|
+
|
17
|
+
# Rack::MockResponse doesn't provide any way to access the reason phrase,
|
18
|
+
# so override the shared test from Common.
|
19
|
+
def test_GET_reason_phrase
|
20
|
+
response = get('echo')
|
21
|
+
assert_nil response.reason_phrase
|
22
|
+
end
|
23
|
+
|
24
|
+
# not using shared test because error is swallowed by Sinatra
|
25
|
+
def test_timeout
|
26
|
+
conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
|
27
|
+
begin
|
28
|
+
conn.get '/slow'
|
29
|
+
rescue Faraday::TimeoutError
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# test not applicable
|
34
|
+
undef test_connection_error
|
35
|
+
undef test_proxy
|
36
|
+
undef test_proxy_auth_fail
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class TestMiddleware < Faraday::TestCase
|
5
|
+
Stubs = Faraday::Adapter.lookup_middleware(:test)::Stubs
|
6
|
+
def setup
|
7
|
+
@stubs = Stubs.new do |stub|
|
8
|
+
stub.get('/hello') do
|
9
|
+
[200, {'Content-Type' => 'text/html'}, 'hello']
|
10
|
+
end
|
11
|
+
stub.get('/method-echo') do |env|
|
12
|
+
[200, {'Content-Type' => 'text/html'}, env[:method].to_s]
|
13
|
+
end
|
14
|
+
stub.get(/\A\/resources\/\d+(?:\?|\z)/) do
|
15
|
+
[200, {'Content-Type' => 'text/html'}, 'show']
|
16
|
+
end
|
17
|
+
stub.get(/\A\/resources\/(specified)\z/) do |env, meta|
|
18
|
+
[200, {'Content-Type' => 'text/html'}, "show #{meta[:match_data][1]}"]
|
19
|
+
end
|
20
|
+
stub.get('http://domain.test/hello') do
|
21
|
+
[200, {'Content-Type' => 'text/html'}, 'domain: hello']
|
22
|
+
end
|
23
|
+
stub.get('http://wrong.test/hello') do
|
24
|
+
[200, {'Content-Type' => 'text/html'}, 'wrong: hello']
|
25
|
+
end
|
26
|
+
stub.get('http://wrong.test/bait') do
|
27
|
+
[404, {'Content-Type' => 'text/html'}]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
@conn = Faraday.new do |builder|
|
31
|
+
builder.adapter :test, @stubs
|
32
|
+
end
|
33
|
+
@resp = @conn.get('/hello')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_middleware_with_simple_path_sets_status
|
37
|
+
assert_equal 200, @resp.status
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_middleware_with_simple_path_sets_headers
|
41
|
+
assert_equal 'text/html', @resp.headers['Content-Type']
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_middleware_with_simple_path_sets_body
|
45
|
+
assert_equal 'hello', @resp.body
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_middleware_with_host_points_to_the_right_stub
|
49
|
+
assert_equal 'domain: hello', @conn.get('http://domain.test/hello').body
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_middleware_can_be_called_several_times
|
53
|
+
assert_equal 'hello', @conn.get('/hello').body
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_middleware_can_handle_regular_expression_path
|
57
|
+
assert_equal 'show', @conn.get('/resources/1').body
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_middleware_can_handle_single_parameter_block
|
61
|
+
assert_equal 'get', @conn.get('/method-echo').body
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_middleware_can_handle_regular_expression_path_with_captured_result
|
65
|
+
assert_equal 'show specified', @conn.get('/resources/specified').body
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_middleware_with_get_params
|
69
|
+
@stubs.get('/param?a=1') { [200, {}, 'a'] }
|
70
|
+
assert_equal 'a', @conn.get('/param?a=1').body
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_middleware_ignores_unspecified_get_params
|
74
|
+
@stubs.get('/optional?a=1') { [200, {}, 'a'] }
|
75
|
+
assert_equal 'a', @conn.get('/optional?a=1&b=1').body
|
76
|
+
assert_equal 'a', @conn.get('/optional?a=1').body
|
77
|
+
assert_raises Faraday::Adapter::Test::Stubs::NotFound do
|
78
|
+
@conn.get('/optional')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_middleware_with_http_headers
|
83
|
+
@stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
|
84
|
+
@stubs.get('/yo') { [200, {}, 'b'] }
|
85
|
+
assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
|
86
|
+
assert_equal 'b', @conn.get('/yo').body
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_middleware_allow_different_outcomes_for_the_same_request
|
90
|
+
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
91
|
+
@stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'world'] }
|
92
|
+
assert_equal 'hello', @conn.get("/hello").body
|
93
|
+
assert_equal 'world', @conn.get("/hello").body
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_yields_env_to_stubs
|
97
|
+
@stubs.get '/hello' do |env|
|
98
|
+
assert_equal '/hello', env[:url].path
|
99
|
+
assert_equal 'foo.com', env[:url].host
|
100
|
+
assert_equal '1', env[:params]['a']
|
101
|
+
assert_equal 'text/plain', env[:request_headers]['Accept']
|
102
|
+
[200, {}, 'a']
|
103
|
+
end
|
104
|
+
|
105
|
+
@conn.headers['Accept'] = 'text/plain'
|
106
|
+
assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_parses_params_with_default_encoder
|
110
|
+
@stubs.get '/hello' do |env|
|
111
|
+
assert_equal '1', env[:params]['a']['b']
|
112
|
+
[200, {}, 'a']
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_parses_params_with_nested_encoder
|
119
|
+
@stubs.get '/hello' do |env|
|
120
|
+
assert_equal '1', env[:params]['a']['b']
|
121
|
+
[200, {}, 'a']
|
122
|
+
end
|
123
|
+
|
124
|
+
@conn.options.params_encoder = Faraday::NestedParamsEncoder
|
125
|
+
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_parses_params_with_flat_encoder
|
129
|
+
@stubs.get '/hello' do |env|
|
130
|
+
assert_equal '1', env[:params]['a[b]']
|
131
|
+
[200, {}, 'a']
|
132
|
+
end
|
133
|
+
|
134
|
+
@conn.options.params_encoder = Faraday::FlatParamsEncoder
|
135
|
+
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_raises_an_error_if_no_stub_is_found_for_request
|
139
|
+
assert_raises Stubs::NotFound do
|
140
|
+
@conn.get('/invalid'){ [200, {}, []] }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_raises_an_error_if_no_stub_is_found_for_specified_host
|
145
|
+
assert_raises Stubs::NotFound do
|
146
|
+
@conn.get('http://domain.test/bait')
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
|
151
|
+
@stubs.get('/yo', { 'X-HELLO' => 'hello' }) { [200, {}, 'a'] }
|
152
|
+
assert_raises Faraday::Adapter::Test::Stubs::NotFound do
|
153
|
+
@conn.get('/yo')
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|