File: authentication_middleware_test.rb

package info (click to toggle)
ruby-faraday 0.13.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: ruby: 5,821; sh: 136; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require File.expand_path('../helper', __FILE__)

class AuthenticationMiddlewareTest < Faraday::TestCase
  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

  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

  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

  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

  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

  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

  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

  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

  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
end