| 1 |
from cherrypy.test import test |
|---|
| 2 |
test.prefer_parent_path() |
|---|
| 3 |
|
|---|
| 4 |
import cherrypy |
|---|
| 5 |
|
|---|
| 6 |
script_names = ["", "/path/to/myapp"] |
|---|
| 7 |
|
|---|
| 8 |
def setup_server(): |
|---|
| 9 |
class Root: |
|---|
| 10 |
def index(self): |
|---|
| 11 |
raise cherrypy.HTTPRedirect('dummy') |
|---|
| 12 |
index.exposed = True |
|---|
| 13 |
|
|---|
| 14 |
def remoteip(self): |
|---|
| 15 |
return cherrypy.request.remote.ip |
|---|
| 16 |
remoteip.exposed = True |
|---|
| 17 |
|
|---|
| 18 |
def xhost(self): |
|---|
| 19 |
raise cherrypy.HTTPRedirect('blah') |
|---|
| 20 |
xhost.exposed = True |
|---|
| 21 |
xhost._cp_config = {'tools.proxy.local': 'X-Host', |
|---|
| 22 |
'tools.trailing_slash.extra': True, |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
def base(self): |
|---|
| 26 |
return cherrypy.request.base |
|---|
| 27 |
base.exposed = True |
|---|
| 28 |
|
|---|
| 29 |
def newurl(self): |
|---|
| 30 |
return ("Browse to <a href='%s'>this page</a>." |
|---|
| 31 |
% cherrypy.url("/this/new/page")) |
|---|
| 32 |
newurl.exposed = True |
|---|
| 33 |
|
|---|
| 34 |
for sn in script_names: |
|---|
| 35 |
cherrypy.tree.mount(Root(), sn) |
|---|
| 36 |
|
|---|
| 37 |
cherrypy.config.update({ |
|---|
| 38 |
'environment': 'test_suite', |
|---|
| 39 |
'tools.proxy.on': True, |
|---|
| 40 |
'tools.proxy.base': 'www.mydomain.com', |
|---|
| 41 |
}) |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
from cherrypy.test import helper |
|---|
| 45 |
|
|---|
| 46 |
class ProxyTest(helper.CPWebCase): |
|---|
| 47 |
|
|---|
| 48 |
def testProxy(self): |
|---|
| 49 |
self.getPage("/") |
|---|
| 50 |
self.assertHeader('Location', |
|---|
| 51 |
"%s://www.mydomain.com%s/dummy" % |
|---|
| 52 |
(self.scheme, self.prefix())) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
self.getPage("/", headers=[('X-Forwarded-Host', 'http://www.yetanother.com')]) |
|---|
| 56 |
self.assertHeader('Location', "http://www.yetanother.com/dummy") |
|---|
| 57 |
self.getPage("/", headers=[('X-Forwarded-Host', 'www.yetanother.com')]) |
|---|
| 58 |
self.assertHeader('Location', "%s://www.yetanother.com/dummy" % self.scheme) |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
self.getPage("/remoteip", |
|---|
| 62 |
headers=[('X-Forwarded-For', '192.168.0.20')]) |
|---|
| 63 |
self.assertBody("192.168.0.20") |
|---|
| 64 |
self.getPage("/remoteip", |
|---|
| 65 |
headers=[('X-Forwarded-For', '67.15.36.43, 192.168.0.20')]) |
|---|
| 66 |
self.assertBody("192.168.0.20") |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
self.getPage("/xhost", headers=[('X-Host', 'www.yetanother.com')]) |
|---|
| 70 |
self.assertHeader('Location', "%s://www.yetanother.com/blah" % self.scheme) |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
self.getPage("/base", headers=[('X-Forwarded-Proto', 'https')]) |
|---|
| 74 |
self.assertBody("https://www.mydomain.com") |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
for sn in script_names: |
|---|
| 78 |
|
|---|
| 79 |
self.getPage(sn + "/newurl") |
|---|
| 80 |
self.assertBody("Browse to <a href='%s://www.mydomain.com" % self.scheme |
|---|
| 81 |
+ sn + "/this/new/page'>this page</a>.") |
|---|
| 82 |
self.getPage(sn + "/newurl", headers=[('X-Forwarded-Host', |
|---|
| 83 |
'http://www.yetanother.com')]) |
|---|
| 84 |
self.assertBody("Browse to <a href='http://www.yetanother.com" |
|---|
| 85 |
+ sn + "/this/new/page'>this page</a>.") |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
port = "" |
|---|
| 89 |
if self.scheme == "http" and self.PORT != 80: |
|---|
| 90 |
port = ":%s" % self.PORT |
|---|
| 91 |
elif self.scheme == "https" and self.PORT != 443: |
|---|
| 92 |
port = ":%s" % self.PORT |
|---|
| 93 |
host = self.HOST |
|---|
| 94 |
if host == '': |
|---|
| 95 |
import socket |
|---|
| 96 |
host = socket.gethostname() |
|---|
| 97 |
self.assertEqual(cherrypy.url("/this/new/page", script_name=sn), |
|---|
| 98 |
"%s://%s%s%s/this/new/page" |
|---|
| 99 |
% (self.scheme, host, port, sn)) |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
self.getPage("/xhost/", headers=[('X-Host', 'www.yetanother.com')]) |
|---|
| 103 |
self.assertHeader('Location', "%s://www.yetanother.com/xhost" |
|---|
| 104 |
% self.scheme) |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
if __name__ == '__main__': |
|---|
| 108 |
setup_server() |
|---|
| 109 |
helper.testmain() |
|---|