3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
11
from mozprofile.permissions import ServerLocations, \
12
MissingPrimaryLocationError, MultiplePrimaryLocationsError, \
13
DuplicateLocationError, BadPortLocationError, LocationsSyntaxError
15
class ServerLocationsTest(unittest.TestCase):
16
"""test server locations"""
18
locations = """# This is the primary location from which tests run.
20
http://mochi.test:8888 primary,privileged
22
# a few test locations
23
http://127.0.0.1:80 privileged
24
http://127.0.0.1:8888 privileged
25
https://test:80 privileged
26
http://example.org:80 privileged
27
http://test1.example.org privileged
31
locations_no_primary = """http://secondary.test:80 privileged
32
http://tertiary.test:8888 privileged
35
locations_bad_port = """http://mochi.test:8888 primary,privileged
36
http://127.0.0.1:80 privileged
37
http://127.0.0.1:8888 privileged
38
http://test:badport privileged
39
http://example.org:80 privileged
42
def compare_location(self, location, scheme, host, port, options):
43
self.assertEqual(location.scheme, scheme)
44
self.assertEqual(location.host, host)
45
self.assertEqual(location.port, port)
46
self.assertEqual(location.options, options)
48
def create_temp_file(self, contents):
49
f = tempfile.NamedTemporaryFile()
54
def test_server_locations(self):
55
# write a permissions file
56
f = self.create_temp_file(self.locations)
59
locations = ServerLocations(f.name)
61
# ensure that they're what we expect
62
self.assertEqual(len(locations), 6)
64
self.compare_location(i.next(), 'http', 'mochi.test', '8888',
65
['primary', 'privileged'])
66
self.compare_location(i.next(), 'http', '127.0.0.1', '80',
68
self.compare_location(i.next(), 'http', '127.0.0.1', '8888',
70
self.compare_location(i.next(), 'https', 'test', '80', ['privileged'])
71
self.compare_location(i.next(), 'http', 'example.org', '80',
73
self.compare_location(i.next(), 'http', 'test1.example.org', '80',
76
locations.add_host('mozilla.org')
77
self.assertEqual(len(locations), 7)
78
self.compare_location(i.next(), 'http', 'mozilla.org', '80',
82
self.assertRaises(MultiplePrimaryLocationsError, locations.add_host,
83
'primary.test', options='primary')
85
# We no longer throw these DuplicateLocation Error
87
locations.add_host('127.0.0.1')
88
except DuplicateLocationError:
89
self.assertTrue(False, "Should no longer throw DuplicateLocationError")
91
self.assertRaises(BadPortLocationError, locations.add_host, '127.0.0.1',
94
# test some errors in locations file
95
f = self.create_temp_file(self.locations_no_primary)
99
ServerLocations(f.name)
100
except LocationsSyntaxError, e:
102
self.assertNotEqual(exc, None)
103
self.assertEqual(exc.err.__class__, MissingPrimaryLocationError)
104
self.assertEqual(exc.lineno, 3)
106
# test bad port in a locations file to ensure lineno calculated
108
f = self.create_temp_file(self.locations_bad_port)
112
ServerLocations(f.name)
113
except LocationsSyntaxError, e:
115
self.assertNotEqual(exc, None)
116
self.assertEqual(exc.err.__class__, BadPortLocationError)
117
self.assertEqual(exc.lineno, 4)
119
def test_server_locations_callback(self):
120
class CallbackTest(object):
121
last_locations = None
123
def callback(self, locations):
124
self.last_locations = locations
127
f = self.create_temp_file(self.locations)
128
locations = ServerLocations(f.name, c.callback)
130
# callback should be for all locations in file
131
self.assertEqual(len(c.last_locations), 6)
133
# validate arbitrary one
134
self.compare_location(c.last_locations[2], 'http', '127.0.0.1', '8888',
137
locations.add_host('a.b.c')
139
# callback should be just for one location
140
self.assertEqual(len(c.last_locations), 1)
141
self.compare_location(c.last_locations[0], 'http', 'a.b.c', '80',
144
# read a second file, which should generate a callback with both
146
f = self.create_temp_file(self.locations_no_primary)
147
locations.read(f.name)
148
self.assertEqual(len(c.last_locations), 2)
151
if __name__ == '__main__':