summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_printable_uri.rb
blob: 59394547b9312e25d4a76d05f5c80356980864db (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require_relative 'helper'
require 'uri'
require 'rubygems/printable_uri'

class TestPrintableUri < Gem::TestCase
  def test_parsed_uri
    assert_equal true, Gem::PrintableUri.parse_uri("https://www.example.com").parsed_uri?
  end

  def test_parsed_uri_with_empty_uri_object
    assert_equal true, Gem::PrintableUri.parse_uri(URI("")).parsed_uri?
  end

  def test_parsed_uri_with_valid_uri_object
    assert_equal true, Gem::PrintableUri.parse_uri(URI("https://www.example.com")).parsed_uri?
  end

  def test_parsed_uri_with_other_objects
    assert_equal false, Gem::PrintableUri.parse_uri(Object.new).parsed_uri?
  end

  def test_parsed_uri_with_invalid_uri
    assert_equal false, Gem::PrintableUri.parse_uri("https://www.example.com:80index").parsed_uri?
  end

  def test_credential_redacted_with_user_pass
    assert_equal true, Gem::PrintableUri.parse_uri("https://user:pass@example.com").credential_redacted?
  end

  def test_credential_redacted_with_token
    assert_equal true, Gem::PrintableUri.parse_uri("https://token@example.com").credential_redacted?
  end

  def test_credential_redacted_with_user_x_oauth_basic
    assert_equal true, Gem::PrintableUri.parse_uri("https://token:x-oauth-basic@example.com").credential_redacted?
  end

  def test_credential_redacted_without_credential
    assert_equal false, Gem::PrintableUri.parse_uri("https://www.example.com").credential_redacted?
  end

  def test_credential_redacted_with_empty_uri_object
    assert_equal false, Gem::PrintableUri.parse_uri(URI("")).credential_redacted?
  end

  def test_credential_redacted_with_valid_uri_object
    assert_equal true, Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).credential_redacted?
  end

  def test_credential_redacted_with_other_objects
    assert_equal false, Gem::PrintableUri.parse_uri(Object.new).credential_redacted?
  end

  def test_original_password_user_pass
    assert_equal "pass", Gem::PrintableUri.parse_uri("https://user:pass@example.com").original_password
  end

  def test_original_password_with_token
    assert_equal nil, Gem::PrintableUri.parse_uri("https://token@example.com").original_password
  end

  def test_original_password_without_credential
    assert_equal nil, Gem::PrintableUri.parse_uri("https://www.example.com").original_password
  end

  def test_original_password_with_invalid_uri
    assert_equal nil, Gem::PrintableUri.parse_uri("https://www.example.com:80index").original_password
  end

  def test_original_password_with_empty_uri_object
    assert_equal nil, Gem::PrintableUri.parse_uri(URI("")).original_password
  end

  def test_original_password_with_valid_uri_object
    assert_equal "pass", Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).original_password
  end

  def test_original_password_with_other_objects
    assert_equal nil, Gem::PrintableUri.parse_uri(Object.new).original_password
  end

  def test_to_s_with_user_pass
    assert_equal "https://user:REDACTED@example.com", Gem::PrintableUri.parse_uri("https://user:pass@example.com").to_s
  end

  def test_to_s_with_token
    assert_equal "https://REDACTED@example.com", Gem::PrintableUri.parse_uri("https://token@example.com").to_s
  end

  def test_to_s_with_user_x_oauth_basic
    assert_equal "https://REDACTED:x-oauth-basic@example.com", Gem::PrintableUri.parse_uri("https://token:x-oauth-basic@example.com").to_s
  end

  def test_to_s_without_credential
    assert_equal "https://www.example.com", Gem::PrintableUri.parse_uri("https://www.example.com").to_s
  end

  def test_to_s_with_invalid_uri
    assert_equal "https://www.example.com:80index", Gem::PrintableUri.parse_uri("https://www.example.com:80index").to_s
  end

  def test_to_s_with_empty_uri_object
    assert_equal "", Gem::PrintableUri.parse_uri(URI("")).to_s
  end

  def test_to_s_with_valid_uri_object
    assert_equal "https://user:REDACTED@example.com", Gem::PrintableUri.parse_uri(URI("https://user:pass@example.com")).to_s
  end

  def test_to_s_with_other_objects
    obj = Object.new
    obj.stub(:to_s, "my-to-s") do
      assert_equal "my-to-s", Gem::PrintableUri.parse_uri(obj).to_s
    end
  end
end