Robert Brewer avatar Robert Brewer committed b07e12b

New CPWebCase base() function.

Comments (0)

Files changed (4)

cherrypy/test/helper.py

     def prefix(self):
         return self.script_name.rstrip("/")
     
+    def base(self):
+        if ((self.scheme == "http" and self.PORT == 80) or
+            (self.scheme == "https" and self.PORT == 443)):
+            port = ""
+        else:
+            port = ":%s" % self.PORT
+        return "%s://%s%s%s" % (self.scheme, self.HOST, port,
+                                self.script_name.rstrip("/"))
+    
     def exit(self):
         sys.exit()
     

cherrypy/test/test_core.py

         # Make sure GET params are preserved.
         self.getPage("/redirect?id=3")
         self.assertStatus(('302 Found', '303 See Other'))
-        self.assertInBody("<a href='%s://127.0.0.1:%s%s/redirect/?id=3'>"
-                          "%s://127.0.0.1:%s%s/redirect/?id=3</a>" %
-                          (self.scheme, self.PORT, self.prefix(),
-                           self.scheme, self.PORT, self.prefix()))
+        self.assertInBody("<a href='%s/redirect/?id=3'>"
+                          "%s/redirect/?id=3</a>" % (self.base(), self.base()))
         
         if self.prefix():
             # Corner case: the "trailing slash" redirect could be tricky if
             # we're using a virtual root and the URI is "/vroot" (no slash).
             self.getPage("")
             self.assertStatus(('302 Found', '303 See Other'))
-            self.assertInBody("<a href='%s://127.0.0.1:%s%s/'>"
-                              "%s://127.0.0.1:%s%s/</a>" %
-                              (self.scheme, self.PORT, self.prefix(),
-                               self.scheme, self.PORT, self.prefix()))
+            self.assertInBody("<a href='%s/'>%s/</a>" %
+                              (self.base(), self.base()))
         
         self.getPage("/redirect/by_code?code=300")
         self.assertMatchesBody(r"<a href='(.*)somewhere else'>\1somewhere else</a>")
         # Make sure str(HTTPRedirect()) works.
         self.getPage("/redirect/stringify", protocol="HTTP/1.0")
         self.assertStatus(200)
-        self.assertBody("(['%s://127.0.0.1:%s/'], 302)" % (self.scheme, self.PORT))
+        self.assertBody("(['%s/'], 302)" % self.base())
         if cherrypy.server.protocol_version == "HTTP/1.1":
             self.getPage("/redirect/stringify", protocol="HTTP/1.1")
             self.assertStatus(200)
-            self.assertBody("(['%s://127.0.0.1:%s/'], 303)" % (self.scheme, self.PORT))
+            self.assertBody("(['%s/'], 303)" % self.base())
     
     def testFlatten(self):
         for url in ["/flatten/as_string", "/flatten/as_list",

cherrypy/test/test_objectmapping.py

     def testObjectMapping(self):
         for url in script_names:
             prefix = self.script_name = url
-            if prefix == "/":
-                prefix = ""
             
             self.getPage('/')
             self.assertBody('world')
             # Test omitted trailing slash (should be redirected by default).
             self.getPage("/dir1/dir2")
             self.assertStatus((302, 303))
-            self.assertHeader('Location', '%s://%s:%s%s/dir1/dir2/'
-                              % (self.scheme, self.HOST, self.PORT, prefix))
+            self.assertHeader('Location', '%s/dir1/dir2/' % self.base())
             
             # Test extra trailing slash (should be redirected if configured).
             self.getPage("/dir1/myMethod/")
             self.assertStatus((302, 303))
-            self.assertHeader('Location', '%s://%s:%s%s/dir1/myMethod'
-                              % (self.scheme, self.HOST, self.PORT, prefix))
+            self.assertHeader('Location', '%s/dir1/myMethod' % self.base())
             
             # Test that default method must be exposed in order to match.
             self.getPage("/dir1/dir2/dir3/dir4/index")
             
             self.getPage("/redirect")
             self.assertStatus('302 Found')
-            self.assertHeader('Location', '%s://%s:%s%s/dir1/'
-                              % (self.scheme, self.HOST, self.PORT, prefix))
+            self.assertHeader('Location', '%s/dir1/' % self.base())
             
             # Test that we can use URL's which aren't all valid Python identifiers
             # This should also test the %XX-unquoting of URL's.
             self.getPage("/dir1/dir2/script_name")
             self.assertBody(url)
             self.getPage("/dir1/dir2/cherrypy_url")
-            self.assertBody("%s://%s:%s%s/extra" %
-                            (self.scheme, self.HOST, self.PORT, prefix))
+            self.assertBody("%s/extra" % self.base())
             
             # Test that configs don't overwrite each other from diferent apps
             self.getPage("/confvalue")

cherrypy/test/test_proxy.py

             elif self.scheme == "https" and self.PORT != 443:
                 port = ":%s" % self.PORT
             self.assertEqual(cherrypy.url("/this/new/page", script_name=sn),
-                             "%s://127.0.0.1%s%s/this/new/page"
-                             % (self.scheme, port, sn))
+                             "%s://%s%s%s/this/new/page"
+                             % (self.scheme, self.HOST, port, sn))
 
 
 if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.