Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.1/cherrypy/test/test_objectmapping.py

Revision 719 (checked in by fumanchu, 3 years ago)

Removed a couple of test suite dependencies on httptools.

Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 import sys
30
31 import cherrypy
32
33
34 class Root:
35     def index(self, name="world"):
36         return name
37     index.exposed = True
38    
39     def default(self, *params):
40         return "default:"+repr(params)
41     default.exposed = True
42    
43     def other(self):
44         return "other"
45     other.exposed = True
46    
47     def extra(self, *p):
48         return repr(p)
49     extra.exposed = True
50    
51     def redirect(self):
52         raise cherrypy.HTTPRedirect('dir1/', 302)
53     redirect.exposed = True
54    
55     def notExposed(self):
56         return "not exposed"
57
58 def mapped_func(self, ID=None):
59     return "ID is %s" % ID
60 mapped_func.exposed = True
61 setattr(Root, "Von B\xfclow", mapped_func)
62
63
64 class Exposing:
65     def base(self):
66         return "expose works!"
67     cherrypy.expose(base)
68     cherrypy.expose(base, "1")
69     cherrypy.expose(base, "2")
70
71 class ExposingNewStyle(object):
72     def base(self):
73         return "expose works!"
74     cherrypy.expose(base)
75     cherrypy.expose(base, "1")
76     cherrypy.expose(base, "2")
77
78
79
80 class Dir1:
81     def index(self):
82         return "index for dir1"
83     index.exposed = True
84    
85     def myMethod(self):
86         return "myMethod from dir1, object Path is:" + repr(cherrypy.request.objectPath)
87     myMethod.exposed = True
88    
89     def default(self, *params):
90         return "default for dir1, param is:" + repr(params)
91     default.exposed = True
92
93
94 class Dir2:
95     def index(self):
96         return "index for dir2, path is:" + cherrypy.request.path
97     index.exposed = True
98    
99     def method(self):
100         return "method for dir2"
101     method.exposed = True
102
103
104 class Dir3:
105     def default(self):
106         return "default for dir3, not exposed"
107
108
109 class Dir4:
110     def index(self):
111         return "index for dir4, not exposed"
112
113 cherrypy.root = Root()
114 cherrypy.root.exposing = Exposing()
115 cherrypy.root.exposingnew = ExposingNewStyle()
116 cherrypy.root.dir1 = Dir1()
117 cherrypy.root.dir1.dir2 = Dir2()
118 cherrypy.root.dir1.dir2.dir3 = Dir3()
119 cherrypy.root.dir1.dir2.dir3.dir4 = Dir4()
120 cherrypy.config.update({
121         'server.logToScreen': False,
122         'server.environment': "production",
123 })
124
125
126 import helper
127
128 class ObjectMappingTest(helper.CPWebCase):
129    
130     def testObjectMapping(self):
131         self.getPage('/')
132         self.assertBody('world')
133        
134         self.getPage("/dir1/myMethod")
135         self.assertBody("myMethod from dir1, object Path is:'/dir1/myMethod'")
136        
137         self.getPage("/this/method/does/not/exist")
138         self.assertBody("default:('this', 'method', 'does', 'not', 'exist')")
139        
140         self.getPage("/extra/too/much")
141         self.assertBody("default:('extra', 'too', 'much')")
142        
143         self.getPage("/other")
144         self.assertBody('other')
145        
146         self.getPage("/notExposed")
147         self.assertBody("default:('notExposed',)")
148        
149         self.getPage("/dir1/dir2/")
150         self.assertBody('index for dir2, path is:/dir1/dir2/')
151        
152         self.getPage("/dir1/dir2")
153         self.assert_(self.status in ('302 Found', '303 See Other'))
154         self.assertHeader('Location', 'http://%s:%s/dir1/dir2/'
155                           % (self.HOST, self.PORT))
156        
157         self.getPage("/dir1/dir2/dir3/dir4/index")
158         self.assertBody("default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')")
159        
160         self.getPage("/redirect")
161         self.assertStatus('302 Found')
162         self.assertHeader('Location', 'http://%s:%s/dir1/'
163                           % (self.HOST, self.PORT))
164        
165         # Test that we can use URL's which aren't all valid Python identifiers
166         # This should also test the %XX-unquoting of URL's.
167         self.getPage("/Von%20B%fclow?ID=14")
168         self.assertBody("ID is 14")
169    
170     def testExpose(self):
171         # Test the cherrypy.expose function/decorator
172         self.getPage("/exposing/base")
173         self.assertBody("expose works!")
174        
175         self.getPage("/exposing/1")
176         self.assertBody("expose works!")
177        
178         self.getPage("/exposing/2")
179         self.assertBody("expose works!")
180        
181         self.getPage("/exposingnew/base")
182         self.assertBody("expose works!")
183        
184         self.getPage("/exposingnew/1")
185         self.assertBody("expose works!")
186        
187         self.getPage("/exposingnew/2")
188         self.assertBody("expose works!")
189
190
191
192 if __name__ == "__main__":
193     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets