~ubuntu-branches/ubuntu/maverick/rapache/maverick

« back to all changes in this revision

Viewing changes to tests/LineTest.py

  • Committer: Bazaar Package Importer
  • Author(s): Emanuele Gentili
  • Date: 2008-10-15 15:44:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081015154427-sc86e7urpacfwppr
Tags: 0.7-0ubuntu1
* rapache version bump (LP: #283770)
* debian/patches:
 + removed 01_surf_this_button.patch, fixed in bzr branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Rapache - Apache Configuration Tool
 
2
# Copyright (C) 2008 Stefano Forenza,  Jason Taylor, Emanuele Gentili
 
3
 
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.
 
8
 
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.
 
13
 
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/>.
 
16
import unittest
 
17
import sys
 
18
sys.path.append('../RapacheCore')
 
19
from LineElement import *
 
20
import re
 
21
 
 
22
class LineTest( unittest.TestCase ):
 
23
    def test_init (self):
 
24
        l = Line()
 
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))
 
29
        pass
 
30
    def test_element__init (self):
 
31
        c = etree.Element("line")
 
32
        c.set('directive','DocumentRoot')
 
33
        c.set('value', '/var/www')
 
34
        l = Line( c )
 
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' )
 
39
        
 
40
        
 
41
    def test_value_get_set(self):
 
42
        l = Line()
 
43
        self.assertEqual( l.value, None )
 
44
        l.value = "/var/www"
 
45
        self.assertEqual( l.value, "/var/www" )                
 
46
    def test_opts_set(self):
 
47
        #testing .opts attrib. can't be overriden
 
48
        l = Line()
 
49
        l.opts = ['a','b']
 
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
 
54
        l = Line()
 
55
        l.opts = ('a','b')
 
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'])
 
59
    def test_parse(self):
 
60
        l = Line()
 
61
        expected_values =[
 
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' ],
 
69
            #should return None
 
70
            [ 'ServerAlias', None]
 
71
          ]
 
72
        for case in expected_values:
 
73
            string, expected = case  
 
74
            l.parse(string)        
 
75
            self.assertEqual(l.value, expected)
 
76
        
 
77
        expected_keys =[
 
78
         [ 'DocumentRoot /var/www/', 'DocumentRoot' ],
 
79
         [ '\t\t  DocumentRoot /var/www/', 'DocumentRoot' ]
 
80
        ]
 
81
        for case in expected_keys:
 
82
            string, expected = case  
 
83
            l.parse(string)        
 
84
            self.assertEqual(l.key, expected)
 
85
        l.parse(string) 
 
86
        
 
87
        l.parse('ServerAlias www.example.com beta.example.com ')
 
88
        self.assertEqual(list(l.opts),['www.example.com','beta.example.com'])
 
89
        
 
90
        l.parse('AuthName "Enter your password"')
 
91
        self.assertEqual( l.value,  "Enter your password")
 
92
        
 
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')
 
99
        l = Line( c )
 
100
        self.assertTrue ( l.changed() )
 
101
        l = Line(c)
 
102
        l.parse('DocumentRoot /var/www')
 
103
        self.assertFalse ( l.changed() )
 
104
        l.value = '/srv/www'
 
105
        self.assertTrue ( l.changed() )
 
106
        l = Line(c)
 
107
        l.parse('DocumentRoot /var/www')
 
108
        self.assertFalse ( l.changed() )
 
109
        l.key = 'ServerAlias'
 
110
        self.assertTrue ( l.changed() )
 
111
        
 
112
    def test_get_set_value(self):        
 
113
        l = Line()
 
114
        l.key = 'DocumentRoot'
 
115
        l.value = '/var/www' 
 
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' ])
 
119
        # let's test quoting
 
120
        l = Line()
 
121
        l.key = 'AuthName'
 
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' ])
 
126
        #multiple options
 
127
        l = Line()
 
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' ] )
 
133
        
 
134
        #multiple with quoted string
 
135
        l = Line()
 
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__":
 
142
    unittest.main()