Class: TestConnection

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

#env_url(url, params) {|conn| ... } ⇒ Object

Yields:

  • (conn)


396
397
398
399
400
401
# File 'test/connection_test.rb', line 396

def env_url(url, params)
  conn = Faraday::Connection.new(url, :params => params)
  yield(conn) if block_given?
  req = conn.build_request(:get)
  req.to_env(conn).url
end

#test_auto_parses_basic_auth_from_url_and_unescapesObject



83
84
85
86
87
# File 'test/connection_test.rb', line 83

def test_auto_parses_basic_auth_from_url_and_unescapes
  conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%[email protected]/fish"
  assert auth = conn.headers['Authorization']
  assert_equal Faraday::Request::BasicAuthentication.header("[email protected]", "pass word"), auth
end

#test_basic_auth_sets_headerObject



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

def test_basic_auth_sets_header
  conn = Faraday::Connection.new
  assert_nil conn.headers['Authorization']

  conn.basic_auth 'Aladdin', 'open sesame'
  assert auth = conn.headers['Authorization']
  assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
end

#test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefixObject



148
149
150
151
152
153
# File 'test/connection_test.rb', line 148

def test_build_exclusive_url_allows_absolute_uri_to_ignore_connection_path_prefix
  conn = Faraday::Connection.new
  conn.path_prefix = '/fish'
  uri = conn.build_exclusive_url("/sake.html")
  assert_equal '/sake.html', uri.path
end

#test_build_exclusive_url_doesnt_add_ending_slash_given_empty_urlObject



168
169
170
171
172
173
# File 'test/connection_test.rb', line 168

def test_build_exclusive_url_doesnt_add_ending_slash_given_empty_url
  conn = Faraday::Connection.new
  conn.url_prefix = "http://sushi.com/nigiri"
  uri = conn.build_exclusive_url('')
  assert_equal "/nigiri", uri.path
end

#test_build_exclusive_url_doesnt_add_ending_slash_given_nil_urlObject



161
162
163
164
165
166
# File 'test/connection_test.rb', line 161

def test_build_exclusive_url_doesnt_add_ending_slash_given_nil_url
  conn = Faraday::Connection.new
  conn.url_prefix = "http://sushi.com/nigiri"
  uri = conn.build_exclusive_url
  assert_equal "/nigiri", uri.path
end

#test_build_exclusive_url_doesnt_use_connection_paramsObject



175
176
177
178
179
# File 'test/connection_test.rb', line 175

def test_build_exclusive_url_doesnt_use_connection_params
  conn = Faraday::Connection.new "http://sushi.com/nigiri"
  conn.params = {:a => 1}
  assert_equal "http://sushi.com/nigiri", conn.build_exclusive_url.to_s
end

#test_build_exclusive_url_forces_connection_path_prefix_to_be_absoluteObject



134
135
136
137
138
139
# File 'test/connection_test.rb', line 134

def test_build_exclusive_url_forces_connection_path_prefix_to_be_absolute
  conn = Faraday::Connection.new
  conn.path_prefix = 'fish'
  uri = conn.build_exclusive_url("sake.html")
  assert_equal '/fish/sake.html', uri.path
end

#test_build_exclusive_url_handles_uri_instancesObject



263
264
265
266
267
# File 'test/connection_test.rb', line 263

def test_build_exclusive_url_handles_uri_instances
  conn = Faraday::Connection.new
  uri = conn.build_exclusive_url(URI('/sake.html'))
  assert_equal '/sake.html', uri.path
end

#test_build_exclusive_url_ignores_connection_path_prefix_trailing_slashObject



141
142
143
144
145
146
# File 'test/connection_test.rb', line 141

def test_build_exclusive_url_ignores_connection_path_prefix_trailing_slash
  conn = Faraday::Connection.new
  conn.path_prefix = '/fish/'
  uri = conn.build_exclusive_url("sake.html")
  assert_equal '/fish/sake.html', uri.path
end

#test_build_exclusive_url_joins_url_to_base_with_ending_slashObject



245
246
247
248
249
# File 'test/connection_test.rb', line 245

def test_build_exclusive_url_joins_url_to_base_with_ending_slash
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
  uri = conn.build_exclusive_url("sake.html")
  assert_equal 'http://sushi.com/sushi/sake.html', uri.to_s
end

#test_build_exclusive_url_overrides_baseObject



257
258
259
260
261
# File 'test/connection_test.rb', line 257

def test_build_exclusive_url_overrides_base
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
  uri = conn.build_exclusive_url('/sake/')
  assert_equal 'http://sushi.com/sake/', uri.to_s
end

#test_build_exclusive_url_overrides_connection_port_for_absolute_urlsObject



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

def test_build_exclusive_url_overrides_connection_port_for_absolute_urls
  conn = Faraday::Connection.new
  conn.port = 23
  uri = conn.build_exclusive_url("http://sushi.com")
  assert_equal 80, uri.port
end

#test_build_exclusive_url_parses_urlObject



230
231
232
233
234
235
236
# File 'test/connection_test.rb', line 230

def test_build_exclusive_url_parses_url
  conn = Faraday::Connection.new
  uri = conn.build_exclusive_url("http://sushi.com/sake.html")
  assert_equal "http",       uri.scheme
  assert_equal "sushi.com",  uri.host
  assert_equal '/sake.html', uri.path
end

#test_build_exclusive_url_parses_url_and_changes_schemeObject



238
239
240
241
242
243
# File 'test/connection_test.rb', line 238

def test_build_exclusive_url_parses_url_and_changes_scheme
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi"
  conn.scheme = 'https'
  uri = conn.build_exclusive_url("sake.html")
  assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
end

#test_build_exclusive_url_parses_url_params_into_pathObject



155
156
157
158
159
# File 'test/connection_test.rb', line 155

def test_build_exclusive_url_parses_url_params_into_path
  conn = Faraday::Connection.new
  uri = conn.build_exclusive_url("http://sushi.com/sake.html")
  assert_equal '/sake.html', uri.path
end

#test_build_exclusive_url_used_default_base_with_ending_slashObject



251
252
253
254
255
# File 'test/connection_test.rb', line 251

def test_build_exclusive_url_used_default_base_with_ending_slash
  conn = Faraday::Connection.new :url => "http://sushi.com/sushi/"
  uri = conn.build_exclusive_url
  assert_equal 'http://sushi.com/sushi/', uri.to_s
end

#test_build_exclusive_url_uses_argument_paramsObject



181
182
183
184
185
186
187
188
# File 'test/connection_test.rb', line 181

def test_build_exclusive_url_uses_argument_params
  conn = Faraday::Connection.new "http://sushi.com/nigiri"
  conn.params = {:a => 1}
  params = Faraday::Utils::ParamsHash.new
  params[:a] = 2
  url = conn.build_exclusive_url(nil, params)
  assert_equal "http://sushi.com/nigiri?a=2", url.to_s
end

#test_build_exclusive_url_uses_connection_host_as_default_uri_hostObject



100
101
102
103
104
105
# File 'test/connection_test.rb', line 100

def test_build_exclusive_url_uses_connection_host_as_default_uri_host
  conn = Faraday::Connection.new
  conn.host = 'sushi.com'
  uri = conn.build_exclusive_url("/sake.html")
  assert_equal 'sushi.com', uri.host
end

#test_build_exclusive_url_uses_connection_path_prefix_to_customize_pathObject



120
121
122
123
124
125
# File 'test/connection_test.rb', line 120

def test_build_exclusive_url_uses_connection_path_prefix_to_customize_path
  conn = Faraday::Connection.new
  conn.path_prefix = '/fish'
  uri = conn.build_exclusive_url("sake.html")
  assert_equal '/fish/sake.html', uri.path
end

#test_build_exclusive_url_uses_connection_scheme_as_default_uri_schemeObject



114
115
116
117
118
# File 'test/connection_test.rb', line 114

def test_build_exclusive_url_uses_connection_scheme_as_default_uri_scheme
  conn = Faraday::Connection.new 'http://sushi.com'
  uri = conn.build_exclusive_url("/sake.html")
  assert_equal 'http', uri.scheme
end

#test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_pathObject



127
128
129
130
131
132
# File 'test/connection_test.rb', line 127

def test_build_exclusive_url_uses_root_connection_path_prefix_to_customize_path
  conn = Faraday::Connection.new
  conn.path_prefix = '/'
  uri = conn.build_exclusive_url("sake.html")
  assert_equal '/sake.html', uri.path
end

#test_build_url_merges_paramsObject



196
197
198
199
200
201
# File 'test/connection_test.rb', line 196

def test_build_url_merges_params
  conn = Faraday::Connection.new "http://sushi.com/nigiri"
  conn.params = {:a => 1, :b => 1}
  url = conn.build_url(nil, :b => 2, :c => 3)
  assert_equal "http://sushi.com/nigiri?a=1&b=2&c=3", url.to_s
end

#test_build_url_uses_paramsObject



190
191
192
193
194
# File 'test/connection_test.rb', line 190

def test_build_url_uses_params
  conn = Faraday::Connection.new "http://sushi.com/nigiri"
  conn.params = {:a => 1, :b => 1}
  assert_equal "http://sushi.com/nigiri?a=1&b=1", conn.build_url.to_s
end

#test_dups_connection_objectObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'test/connection_test.rb', line 354

def test_dups_connection_object
  conn = Faraday::Connection.new 'http://sushi.com/foo',
    :ssl => { :verify => :none },
    :headers => {'content-type' => 'text/plain'},
    :params => {'a'=>'1'}

  other = conn.dup

  assert_equal conn.build_exclusive_url, other.build_exclusive_url
  assert_equal 'text/plain', other.headers['content-type']
  assert_equal '1', other.params['a']

  other.basic_auth('', '')
  other.headers['content-length'] = 12
  other.params['b'] = '2'

  assert_equal 2, other.builder.handlers.size
  assert_equal 2, conn.builder.handlers.size
  assert !conn.headers.key?('content-length')
  assert !conn.params.key?('b')
end

#test_env_url_bracketizes_nested_params_in_queryObject



213
214
215
216
# File 'test/connection_test.rb', line 213

def test_env_url_bracketizes_nested_params_in_query
  url = env_url nil, 'a' => {'b' => 'c'}
  assert_equal "a%5Bb%5D=c", url.query
end

#test_env_url_bracketizes_repeated_params_in_queryObject



218
219
220
221
# File 'test/connection_test.rb', line 218

def test_env_url_bracketizes_repeated_params_in_query
  uri = env_url("http://sushi.com/sake.html", 'a' => [1, 2])
  assert_equal "a%5B%5D=1&a%5B%5D=2", uri.query
end

#test_env_url_escapes_per_specObject



208
209
210
211
# File 'test/connection_test.rb', line 208

def test_env_url_escapes_per_spec
  uri = env_url('http:/', 'a' => '1+2 foo~bar.-baz')
  assert_equal "a=1%2B2+foo~bar.-baz", uri.query
end

#test_env_url_parses_url_params_into_queryObject



203
204
205
206
# File 'test/connection_test.rb', line 203

def test_env_url_parses_url_params_into_query
  uri = env_url("http://sushi.com/sake.html", 'a[b]' => '1 + 2')
  assert_equal "a%5Bb%5D=1+%2B+2", uri.query
end

#test_env_url_without_braketizing_repeated_params_in_queryObject



223
224
225
226
227
228
# File 'test/connection_test.rb', line 223

def test_env_url_without_braketizing_repeated_params_in_query
  uri = env_url 'http://sushi.com', 'a' => [1, 2] do |conn|
    conn.options.params_encoder = Faraday::FlatParamsEncoder
  end
  assert_equal "a=1&a=2", uri.query
end

#test_init_with_blockObject



381
382
383
384
# File 'test/connection_test.rb', line 381

def test_init_with_block
  conn = Faraday::Connection.new { }
  assert_equal 0, conn.builder.handlers.size
end

#test_init_with_block_yields_connectionObject



386
387
388
389
390
391
392
393
394
# File 'test/connection_test.rb', line 386

def test_init_with_block_yields_connection
  conn = Faraday::Connection.new(:params => {'a'=>'1'}) { |faraday|
    faraday.adapter :net_http
    faraday.url_prefix = 'http://sushi.com/omnom'
    assert_equal '1', faraday.params['a']
  }
  assert_equal 1, conn.builder.handlers.size
  assert_equal '/omnom', conn.path_prefix
end

#test_initialize_inherits_default_port_out_of_given_urlObject



24
25
26
27
# File 'test/connection_test.rb', line 24

def test_initialize_inherits_default_port_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com"
  assert_equal 80, conn.port
end

#test_initialize_parses_host_out_of_given_urlObject



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

def test_initialize_parses_host_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com"
  assert_equal 'sushi.com', conn.host
end

#test_initialize_parses_nil_path_prefix_out_of_given_urlObject



39
40
41
42
# File 'test/connection_test.rb', line 39

def test_initialize_parses_nil_path_prefix_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com"
  assert_equal '/', conn.path_prefix
end

#test_initialize_parses_path_prefix_out_of_given_urlObject



44
45
46
47
# File 'test/connection_test.rb', line 44

def test_initialize_parses_path_prefix_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com/fish"
  assert_equal '/fish', conn.path_prefix
end

#test_initialize_parses_path_prefix_out_of_given_url_optionObject



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

def test_initialize_parses_path_prefix_out_of_given_url_option
  conn = Faraday::Connection.new :url => "http://sushi.com/fish"
  assert_equal '/fish', conn.path_prefix
end

#test_initialize_parses_port_out_of_given_urlObject



34
35
36
37
# File 'test/connection_test.rb', line 34

def test_initialize_parses_port_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com:815"
  assert_equal 815, conn.port
end

#test_initialize_parses_scheme_out_of_given_urlObject



29
30
31
32
# File 'test/connection_test.rb', line 29

def test_initialize_parses_scheme_out_of_given_url
  conn = Faraday::Connection.new "http://sushi.com"
  assert_equal 'http', conn.scheme
end

#test_initialize_stores_default_headers_from_optionsObject



69
70
71
72
# File 'test/connection_test.rb', line 69

def test_initialize_stores_default_headers_from_options
  conn = Faraday::Connection.new :headers => {:user_agent => 'Faraday'}
  assert_equal 'Faraday', conn.headers['User-agent']
end

#test_initialize_stores_default_params_from_optionsObject



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

def test_initialize_stores_default_params_from_options
  conn = Faraday::Connection.new :params => {:a => 1}
  assert_equal({'a' => 1}, conn.params)
end

#test_initialize_stores_default_params_from_uriObject



59
60
61
62
# File 'test/connection_test.rb', line 59

def test_initialize_stores_default_params_from_uri
  conn = Faraday::Connection.new "http://sushi.com/fish?a=1"
  assert_equal({'a' => '1'}, conn.params)
end

#test_initialize_stores_default_params_from_uri_and_optionsObject



64
65
66
67
# File 'test/connection_test.rb', line 64

def test_initialize_stores_default_params_from_uri_and_options
  conn = Faraday::Connection.new "http://sushi.com/fish?a=1&b=2", :params => {'a' => 3}
  assert_equal({'a' => 3, 'b' => '2'}, conn.params)
end

#test_initialize_with_false_optionObject



376
377
378
379
# File 'test/connection_test.rb', line 376

def test_initialize_with_false_option
  conn = Faraday::Connection.new :ssl => {:verify => false}
  assert !conn.ssl.verify?
end

#test_no_proxy_from_blank_envObject



333
334
335
336
337
338
# File 'test/connection_test.rb', line 333

def test_no_proxy_from_blank_env
  with_env 'http_proxy', '' do
    conn = Faraday::Connection.new
    assert_equal nil, conn.proxy
  end
end

#test_no_proxy_from_envObject



326
327
328
329
330
331
# File 'test/connection_test.rb', line 326

def test_no_proxy_from_env
  with_env 'http_proxy', nil do
    conn = Faraday::Connection.new
    assert_equal nil, conn.proxy
  end
end

#test_proxy_accepts_env_without_schemeObject



318
319
320
321
322
323
324
# File 'test/connection_test.rb', line 318

def test_proxy_accepts_env_without_scheme
  with_env 'http_proxy', "localhost:8888" do
    uri = Faraday::Connection.new.proxy[:uri]
    assert_equal 'localhost', uri.host
    assert_equal 8888, uri.port
  end
end

#test_proxy_accepts_hashObject



294
295
296
297
298
299
300
301
# File 'test/connection_test.rb', line 294

def test_proxy_accepts_hash
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
    conn = Faraday::Connection.new
    conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick'
    assert_equal 'proxy.com', conn.proxy.host
    assert_equal 'rick',      conn.proxy.user
  end
end

#test_proxy_accepts_hash_with_string_uriObject



285
286
287
288
289
290
291
292
# File 'test/connection_test.rb', line 285

def test_proxy_accepts_hash_with_string_uri
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
    conn = Faraday::Connection.new
    conn.proxy :uri => 'http://proxy.com', :user => 'rick'
    assert_equal 'proxy.com', conn.proxy.host
    assert_equal 'rick',      conn.proxy.user
  end
end

#test_proxy_accepts_http_envObject



303
304
305
306
307
308
# File 'test/connection_test.rb', line 303

def test_proxy_accepts_http_env
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
    conn = Faraday::Connection.new
    assert_equal 'duncan.proxy.com', conn.proxy.host
  end
end

#test_proxy_accepts_http_env_with_authObject



310
311
312
313
314
315
316
# File 'test/connection_test.rb', line 310

def test_proxy_accepts_http_env_with_auth
  with_env 'http_proxy', "http://a%40b:my%[email protected]:80" do
    conn = Faraday::Connection.new
    assert_equal 'a@b',     conn.proxy.user
    assert_equal 'my pass', conn.proxy.password
  end
end

#test_proxy_accepts_stringObject



269
270
271
272
273
274
275
# File 'test/connection_test.rb', line 269

def test_proxy_accepts_string
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
    conn = Faraday::Connection.new
    conn.proxy 'http://proxy.com'
    assert_equal 'proxy.com', conn.proxy.host
  end
end

#test_proxy_accepts_uriObject



277
278
279
280
281
282
283
# File 'test/connection_test.rb', line 277

def test_proxy_accepts_uri
  with_env 'http_proxy', "http://duncan.proxy.com:80" do
    conn = Faraday::Connection.new
    conn.proxy URI.parse('http://proxy.com')
    assert_equal 'proxy.com', conn.proxy.host
  end
end

#test_proxy_doesnt_accept_uppercase_envObject



340
341
342
343
344
345
# File 'test/connection_test.rb', line 340

def test_proxy_doesnt_accept_uppercase_env
  with_env 'HTTP_PROXY', "http://localhost:8888/" do
    conn = Faraday::Connection.new
    assert_nil conn.proxy
  end
end

#test_proxy_requires_uriObject



347
348
349
350
351
352
# File 'test/connection_test.rb', line 347

def test_proxy_requires_uri
  conn = Faraday::Connection.new
  assert_raises ArgumentError do
    conn.proxy :uri => :bad_uri, :user => 'rick'
  end
end

#test_token_auth_sets_headerObject



89
90
91
92
93
94
95
96
97
98
# File 'test/connection_test.rb', line 89

def test_token_auth_sets_header
  conn = Faraday::Connection.new
  assert_nil conn.headers['Authorization']

  conn.token_auth 'abcdef', :nonce => 'abc'
  assert auth = conn.headers['Authorization']
  assert_match(/^Token /, auth)
  assert_match(/token="abcdef"/, auth)
  assert_match(/nonce="abc"/, auth)
end

#with_env(key, proxy) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'test/connection_test.rb', line 5

def with_env(key, proxy)
  old_value = ENV.fetch(key, false)
  ENV[key] = proxy
  begin
    yield
  ensure
    if old_value == false
      ENV.delete key
    else
      ENV[key] = old_value
    end
  end
end