1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#!/usr/bin/env python
"""
%prog port
e.g. %prog 8000
Runs a local server to point the mechanize functional tests at. Example:
python test-tools/twisted-localserver.py 8042
python functional_tests.py --uri=http://localhost:8042/
You need twisted.web to run it.
"""
import optparse
import os
import re
import sys
import time
from twisted.cred import checkers, portal
from twisted.internet import reactor
from twisted.python import log
from twisted.web import http, resource, server
from twisted.web.guard import (BasicCredentialFactory, DigestCredentialFactory,
HTTPAuthSessionWrapper)
from twisted.web.resource import IResource, EncodingResourceWrapper
from twisted.web.server import GzipEncoderFactory
from twisted.web.util import Redirect
from zope.interface import implementer
def gzip_wrapper(page):
return EncodingResourceWrapper(page, [GzipEncoderFactory()])
def html(title=None, extra_content=""):
html = """\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>mechanize</title>
</head>
<body><a href="http://sourceforge.net/">
%s
</body>
</html>
""" % extra_content
if title is not None:
html = re.sub("<title>(.*)</title>", "<title>%s</title>" % title, html)
return html
MECHANIZE_HTML = html()
ROOT_HTML = html("mechanize")
RELOAD_TEST_HTML = """\
<html>
<head><title>Title</title></head>
<body>
<a href="/mechanize">near the start</a>
<p>Now some data to prevent HEAD parsing from reading the link near
the end.
<pre>
%s</pre>
<a href="/mechanize">near the end</a>
</body>
</html>""" % (("0123456789ABCDEF" * 4 + "\n") * 61)
REFERER_TEST_HTML = """\
<html>
<head>
<title>mechanize Referer (sic) test page</title>
</head>
<body>
<p>This page exists to test the Referer functionality of \
<a href="/mechanize">mechanize</a>.
<p><a href="/dynamic">Here</a>\
is a link to a page that displays the Referer header.
</body>
</html>"""
BASIC_AUTH_PAGE = """
<html>
<head>
<title>Basic Auth Protected Area</title>
</head>
<body>
<p>Hello, basic auth world.
<p>
</body>
</html>
"""
DIGEST_AUTH_PAGE = """
<html>
<head>
<title>Digest Auth Protected Area</title>
</head>
<body>
<p>Hello, digest auth world.
<p>
</body>
</html>
"""
class TestHTTPUser(object):
"""
Test avatar implementation for http auth with cred
"""
isLeaf = True
def render(self, request):
return self.template.encode('utf-8')
def __init__(self, template, username):
self.template = template
self.username = username
@implementer(portal.IRealm)
class TestAuthRealm(object):
"""
Test realm that supports the IHTTPUser interface
"""
def __init__(self, template=BASIC_AUTH_PAGE):
self.template = template
def requestAvatar(self, avatarId, mind, *interfaces):
if IResource in interfaces:
if avatarId == checkers.ANONYMOUS:
return (IResource, TestHTTPUser(self.template, 'anonymous'),
lambda: None)
return (IResource, TestHTTPUser(self.template, avatarId),
lambda: None)
raise NotImplementedError("Only IResource interface is supported")
class Page(resource.Resource):
def __init__(self, text='', leaf=False, content_type='text/html'):
self.isLeaf = leaf
self.content_type = content_type
self.text = text
resource.Resource.__init__(self)
def getChild(self, path, request):
if not path:
return self
return resource.Resource.getChild(self, path, request)
def render(self, request):
request.setResponseCode(http.OK)
request.setHeader('content-type', self.content_type)
return self.text.encode('utf-8')
class DynamicPage(resource.Resource):
isLeaf = True
def getChild(self, path, request):
if not path:
return self
return resource.Resource.getChild(self, path, request)
def render(self, request):
request.setResponseCode(http.OK)
request.setHeader('content-type', 'text/html')
year_plus_one = time.localtime(time.time())[0] + 1
expires = "09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
request.addCookie('foo', 'bar', expires=expires)
request.addCookie('sessioncookie', 'spam\n')
html = (
"<html><head><title>Cookies/form submission parameters</title>")
if request.args.get(b'refresh'):
html += '<meta http-equiv="refresh" content=\'%s\'>' % (
request.args.get(b'refresh')[0].decode('ascii'))
elif not request.getCookie(b'foo'):
html += '<meta http-equiv="refresh" content="1">'
html += "</head><body>"
if request.getHeader(b'referer'):
html += "<p>Referer:</p><pre>{}</pre>".format(
request.getHeader(b'referer'))
html += "<p>Received cookies:</p>"
html += "<pre>"
html += request.getHeader('cookie') or ''
html += "</pre>"
if request.getCookie(b'foo'):
html += "<p>Your browser supports cookies!"
if request.getCookie(b'sessioncookie'):
html += "<p>Received session cookie"
html += '</body></html>'
return html.encode('utf-8')
def _make_page(parent, name, text, content_type, wrapper,
leaf=False):
page = Page(text=text, leaf=leaf, content_type=content_type)
name = name.encode('utf-8')
parent.putChild(name, wrapper(page))
return page
def make_page(parent, name, text,
content_type="text/html", wrapper=lambda page: page):
return _make_page(parent, name, text, content_type, wrapper, leaf=False)
def make_leaf_page(parent, name, text,
content_type="text/html", wrapper=lambda page: page):
return _make_page(parent, name, text, content_type, wrapper, leaf=True)
def make_redirect(parent, name, location_relative_ref):
redirect = Redirect(location_relative_ref)
name = name.encode('utf-8')
parent.putChild(name, redirect)
return redirect
def require_basic_auth(resource):
p = portal.Portal(TestAuthRealm())
c = checkers.InMemoryUsernamePasswordDatabaseDontUse(john=b'john')
p.registerChecker(c)
cred_factory = BasicCredentialFactory("Basic Auth protected area")
return HTTPAuthSessionWrapper(p, [cred_factory])
def require_digest_auth(resource):
p = portal.Portal(TestAuthRealm(DIGEST_AUTH_PAGE))
c = checkers.InMemoryUsernamePasswordDatabaseDontUse(
digestuser=b'digestuser')
p.registerChecker(c)
cred_factory = DigestCredentialFactory(
"md5", b"Digest Auth protected area")
return HTTPAuthSessionWrapper(p, [cred_factory])
def parse_options(args):
parser = optparse.OptionParser()
parser.add_option("--log", action="store_true")
options, remaining_args = parser.parse_args(args)
options.port = int(remaining_args[0]) if remaining_args else 8000
return options
def main(argv):
options = parse_options(argv[1:])
if options.log:
log.startLogging(sys.stdout)
# This is supposed to match the SF site so it's easy to run a functional
# test over the internet and against Apache.
# TODO: Remove bizarre structure and strings expected by functional tests.
root = Page(text=ROOT_HTML)
mechanize = make_page(root, "mechanize", MECHANIZE_HTML)
make_leaf_page(root, "robots.txt",
"User-Agent: *\nDisallow: /norobots",
"text/plain")
make_leaf_page(root, "robots", "Hello, robots.", "text/plain")
make_leaf_page(root, "norobots", "Hello, non-robots.", "text/plain")
root.putChild(b'dynamic', DynamicPage())
test_fixtures = make_page(root, "test_fixtures",
# satisfy stupid assertions in functional tests
html("Python bits",
extra_content="GeneralFAQ.html"))
make_leaf_page(test_fixtures, "cctest2.txt",
"Hello ClientCookie functional test suite.",
"text/plain")
make_leaf_page(test_fixtures, "referertest.html", REFERER_TEST_HTML)
make_leaf_page(test_fixtures, "mechanize_reload_test.html",
RELOAD_TEST_HTML)
make_redirect(root, "redirected", b"/doesnotexist")
make_redirect(root, "redirected_good", b"/test_fixtures")
example_html = open(os.path.join(
"examples", "forms", "example.html")).read()
make_leaf_page(mechanize, "example.html", example_html)
make_page(root, "basic_auth", BASIC_AUTH_PAGE, wrapper=require_basic_auth)
make_page(root, "digest_auth", DIGEST_AUTH_PAGE,
wrapper=require_digest_auth)
make_leaf_page(
root, "gzip", open(__file__).read(),
"text/plain", wrapper=gzip_wrapper)
site = server.Site(root)
reactor.listenTCP(options.port, site)
reactor.run()
if __name__ == "__main__":
main(sys.argv)
|