summaryrefslogtreecommitdiffstats
path: root/tests/regex/proxy.py
blob: 767330d398c6bc4266859f7067932cefae7cef20 (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
import unittest
import re
import traceback


class ProxyRegexTestCase(unittest.TestCase):
    def testProxy(self):
        """
        Run a list of possible proxy= values through the regex and check for
        correct results.

        tests are in the form of: (proxy string, match.groups() tuple)
        """
        tests = [ ( "proxy.host",
                      (None, None, None, None, 'proxy.host', None, None) ),

                  ( "proxy.host:3128",
                      (None, None, None, None, 'proxy.host', ':3128', None) ),

                  ( "user:password@proxy.host",
                      (None, 'user:password@', 'user', ':password', 'proxy.host', None, None) ),

                  ( "user@proxy.host",
                      (None, 'user@', 'user', None, 'proxy.host', None, None) ),

                  ( "user:password@proxy.host:3128",
                      (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', None) ),

                  ( "user@proxy.host:3128",
                      (None, 'user@', 'user', None, 'proxy.host', ':3128', None) ),

                  ( "proxy.host/blah/blah",
                      (None, None, None, None, 'proxy.host', None, '/blah/blah') ),

                  ( "proxy.host:3128/blah/blah",
                      (None, None, None, None, 'proxy.host', ':3128', '/blah/blah') ),

                  ( "user:password@proxy.host/blah/blah",
                      (None, 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah') ),

                  ( "user@proxy.host/blah/blah",
                      (None, 'user@', 'user', None, 'proxy.host', None, '/blah/blah') ),

                  ( "user:password@proxy.host:3128/blah/blah",
                      (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', "/blah/blah") ),

                  ( "user@proxy.host:3128/blah/blah",
                      (None, 'user@', 'user', None, 'proxy.host', ':3128', "/blah/blah") ),



                  ( "http://proxy.host",
                      ('http://', None, None, None, 'proxy.host', None, None) ),

                  ( "http://proxy.host:3128",
                      ('http://', None, None, None, 'proxy.host', ':3128', None) ),

                  ( "http://user:password@proxy.host",
                      ('http://', 'user:password@', 'user', ':password', 'proxy.host', None, None) ),

                  ( "http://user@proxy.host",
                      ('http://', 'user@', 'user', None, 'proxy.host', None, None) ),

                  ( "http://user:password@proxy.host:3128",
                      ('http://', 'user:password@', 'user', ':password', 'proxy.host', ':3128', None) ),

                  ( "http://user@proxy.host:3128",
                      ('http://', 'user@', 'user', None, 'proxy.host', ':3128', None) ),

                  ( "http://proxy.host/blah/blah",
                      ('http://', None, None, None, 'proxy.host', None, '/blah/blah') ),

                  ( "http://proxy.host:3128/blah/blah",
                      ('http://', None, None, None, 'proxy.host', ':3128', '/blah/blah') ),

                  ( "http://user:password@proxy.host/blah/blah",
                      ("http://", 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah') ),

                  ( "http://user@proxy.host/blah/blah",
                      ("http://", 'user@', 'user', None, 'proxy.host', None, '/blah/blah') ),

                  ( "http://user:password@proxy.host:3128/blah/blah",
                      ("http://", 'user:password@', 'user', ':password', 'proxy.host', ':3128', '/blah/blah') ),

                  ( "http://user@proxy.host:3128/blah/blah",
                      ("http://", 'user@', 'user', None, 'proxy.host', ':3128', '/blah/blah') ),

                ]


        # This is from yumupdate.py and needs to be updated when it changes
        pattern = re.compile("([A-Za-z]+://)?(([A-Za-z0-9]+)(:[^:@]+)?@)?([^:/]+)(:[0-9]+)?(/.*)?")

        got_error = False
        for proxy, result in tests:
            try:
                self.assertEqual(pattern.match(proxy).groups(), result)
            except AssertionError as error:
                got_error = True
                print error

        if got_error:
            self.fail()

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(ProxyRegexTestCase)


if __name__ == "__main__":
    unittest.main()