1
# Rapache - Apache Configuration Tool
2
# Copyright (C) 2008 Stefano Forenza, Jason Taylor, Emanuele Gentili
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
sys.path.append('../RapacheCore')
19
from LineElement import *
22
class LineTest( unittest.TestCase ):
25
self.assertEqual( l.key, None )
26
self.assertEqual( l.value, None )
27
self.assertEqual( len( l.opts ), 0 )
28
self.assertTrue( isinstance(l.opts, Options))
30
def test_element__init (self):
31
c = etree.Element("line")
32
c.set('directive','DocumentRoot')
33
c.set('value', '/var/www')
35
self.assertEqual( l.element.attrib['directive'], 'DocumentRoot' )
36
self.assertEqual( l.element.attrib['value'], '/var/www' )
37
self.assertEqual( l.key, 'DocumentRoot' )
38
self.assertEqual( l.value, '/var/www' )
41
def test_value_get_set(self):
43
self.assertEqual( l.value, None )
45
self.assertEqual( l.value, "/var/www" )
46
def test_opts_set(self):
47
#testing .opts attrib. can't be overriden
50
#l.opts should still be an Options instance, not a list
51
self.assertTrue( isinstance(l.opts, Options))
52
self.assertEqual( list(l.opts), ['a','b'])
53
#again, but using a tuple
56
#l.opts should still be an Options instance, not a list
57
self.assertTrue( isinstance(l.opts, Options))
58
self.assertEqual( list(l.opts), ['a','b'])
62
[ 'DocumentRoot /var/www/', '/var/www/' ],
63
#should strip trailing spaces
64
[ 'DocumentRoot /var/www/ ', '/var/www/' ],
65
#should strip preceding spaces
66
[ 'DocumentRoot /var/www/ ', '/var/www/' ],
67
#should return all the options as one single string
68
[ 'ServerAlias www.example.com beta.example.com ', 'www.example.com beta.example.com' ],
70
[ 'ServerAlias', None]
72
for case in expected_values:
73
string, expected = case
75
self.assertEqual(l.value, expected)
78
[ 'DocumentRoot /var/www/', 'DocumentRoot' ],
79
[ '\t\t DocumentRoot /var/www/', 'DocumentRoot' ]
81
for case in expected_keys:
82
string, expected = case
84
self.assertEqual(l.key, expected)
87
l.parse('ServerAlias www.example.com beta.example.com ')
88
self.assertEqual(list(l.opts),['www.example.com','beta.example.com'])
90
l.parse('AuthName "Enter your password"')
91
self.assertEqual( l.value, "Enter your password")
93
l.parse('AuthName "Enter your \"password\""')
94
self.assertEqual( l.value, 'Enter your "password"')
95
def test_changed(self):
96
c = etree.Element("line")
97
c.set('directive','DocumentRoot')
98
c.set('value', '/var/www')
100
self.assertTrue ( l.changed() )
102
l.parse('DocumentRoot /var/www')
103
self.assertFalse ( l.changed() )
105
self.assertTrue ( l.changed() )
107
l.parse('DocumentRoot /var/www')
108
self.assertFalse ( l.changed() )
109
l.key = 'ServerAlias'
110
self.assertTrue ( l.changed() )
112
def test_get_set_value(self):
114
l.key = 'DocumentRoot'
116
self.assertEqual( l.get_as_str(), 'DocumentRoot /var/www\n')
117
self.assertEqual( l.value, '/var/www' )
118
self.assertEqual( list(l.opts), [ '/var/www' ])
122
l.value = 'Enter your password'
123
self.assertEqual( l.get_as_str(), 'AuthName "Enter your password"\n')
124
self.assertEqual( l.value, 'Enter your password' )
125
self.assertEqual( list(l.opts), [ 'Enter your password' ])
128
l.key = 'ServerAlias'
129
l.opts = 'example.org', 'example.net'
130
self.assertEqual( l.get_as_str(), 'ServerAlias example.org example.net\n')
131
self.assertEqual( l.value, 'example.org example.net' )
132
self.assertEqual( list(l.opts), [ 'example.org', 'example.net' ] )
134
#multiple with quoted string
136
l.key = 'ErrorDocument'
137
l.opts = '404', 'Not a good idea'
138
self.assertEqual( l.get_as_str(), 'ErrorDocument 404 "Not a good idea"\n')
139
self.assertEqual( l.value, '404 "Not a good idea"' )
140
self.assertEqual( list(l.opts), ['404', 'Not a good idea' ] )
141
if __name__ == "__main__":