Class: AuthenticationMiddlewareTest

Inherits:
Faraday::TestCase show all
Defined in:
test/authentication_middleware_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

#connObject



4
5
6
7
8
9
10
11
12
13
# File 'test/authentication_middleware_test.rb', line 4

def conn
  Faraday::Connection.new('http://example.net/') do |builder|
    yield(builder)
    builder.adapter :test do |stub|
      stub.get('/auth-echo') do |env|
        [200, {}, env[:request_headers]['Authorization']]
      end
    end
  end
end

#test_authorization_middleware_with_hashObject



58
59
60
61
62
63
64
# File 'test/authentication_middleware_test.rb', line 58

def test_authorization_middleware_with_hash
  response = conn { |b|
    b.request :authorization, 'baz', :foo => 42
  }.get('/auth-echo')
  assert_match(/^baz /, response.body)
  assert_match(/foo="42"/, response.body)
end

#test_authorization_middleware_with_stringObject



51
52
53
54
55
56
# File 'test/authentication_middleware_test.rb', line 51

def test_authorization_middleware_with_string
  response = conn { |b|
    b.request :authorization, 'custom', 'abc def'
  }.get('/auth-echo')
  assert_match(/^custom abc def$/, response.body)
end

#test_basic_middleware_adds_basic_headerObject



15
16
17
18
# File 'test/authentication_middleware_test.rb', line 15

def test_basic_middleware_adds_basic_header
  response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.get('/auth-echo')
  assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', response.body
end

#test_basic_middleware_adds_basic_header_correctly_with_long_valuesObject



20
21
22
23
# File 'test/authentication_middleware_test.rb', line 20

def test_basic_middleware_adds_basic_header_correctly_with_long_values
  response = conn { |b| b.request :basic_auth, 'A' * 255, '' }.get('/auth-echo')
  assert_equal "Basic #{'QUFB' * 85}Og==", response.body
end

#test_basic_middleware_does_not_interfere_with_existing_authorizationObject



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

def test_basic_middleware_does_not_interfere_with_existing_authorization
  response = conn { |b| b.request :basic_auth, 'aladdin', 'opensesame' }.
    get('/auth-echo', nil, :authorization => 'Token token="bar"')
  assert_equal 'Token token="bar"', response.body
end

#test_token_middleware_adds_token_headerObject



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

def test_token_middleware_adds_token_header
  response = conn { |b| b.request :token_auth, 'quux' }.get('/auth-echo')
  assert_equal 'Token token="quux"', response.body
end

#test_token_middleware_does_not_interfere_with_existing_authorizationObject



45
46
47
48
49
# File 'test/authentication_middleware_test.rb', line 45

def test_token_middleware_does_not_interfere_with_existing_authorization
  response = conn { |b| b.request :token_auth, 'quux' }.
    get('/auth-echo', nil, :authorization => 'Token token="bar"')
  assert_equal 'Token token="bar"', response.body
end

#test_token_middleware_includes_other_values_if_providedObject



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

def test_token_middleware_includes_other_values_if_provided
  response = conn { |b|
    b.request :token_auth, 'baz', :foo => 42
  }.get('/auth-echo')
  assert_match(/^Token /, response.body)
  assert_match(/token="baz"/, response.body)
  assert_match(/foo="42"/, response.body)
end