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