Created
March 13, 2010 00:11
-
-
Save mnot/330963 to your computer and use it in GitHub Desktop.
Test for browser redirection handling WRT URI fragments, quick and dirty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Test for browser redirection handling WRT URI fragments, quick and dirty. | |
Usage: | |
./redir_frag_test.py hostname port_number | |
""" | |
import nbhttp # http://github.com/mnot/nbhttp | |
# mapping of URIs to redirect | |
redirs = { | |
'red-uri.asis': '%(base_uri)s/test', | |
'red-uri+frag.asis': '%(base_uri)s/test#anchor2', | |
'red-path.asis': '/test', | |
'red-path+frag.asis': '/test#anchor2', | |
} | |
# mapping of static pages to serve | |
pages = { | |
'' : """\ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Paths and fragments in Location header</title> | |
<style type="text/css"> | |
.fail { | |
background-color: #ffd0d0; | |
} | |
.warn { | |
background-color: #ffff80; | |
} | |
.pass { | |
background-color: #d0ffd0; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Paths and fragments in Location header</h1> | |
<p> | |
See <a href="http://trac.tools.ietf.org/wg/httpbis/trac/ticket/43">Ticket 43: Fragment combination / precedence during redirects</a> | |
and <a href="http://trac.tools.ietf.org/wg/httpbis/trac/ticket/185">Ticket 185: Location header payload handling</a>. | |
</p> | |
<h2>Test Cases</h2> | |
<table> | |
<thead> | |
<tr> | |
<th>Case</th> | |
<th>Original URI</th> | |
<th>Location header</th> | |
<th>Expected Final URI</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td>T1</td> | |
<td><a href="%(base_uri)s/red-uri+frag.asis">%(base_uri)s/red-uri+frag.asis</a></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
</tr> | |
<tr> | |
<td>T2</td> | |
<td><a href="%(base_uri)s/red-uri.asis">%(base_uri)s/red-uri.asis</a></td> | |
<td>%(base_uri)s/test</td> | |
<td>%(base_uri)s/test</td> | |
</tr> | |
<tr> | |
<td>T3</td> | |
<td><a href="%(base_uri)s/red-uri+frag.asis#anchor1">%(base_uri)s/red-uri+frag.asis#<b>anchor1</b></a></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
</tr> | |
<tr> | |
<td>T4</td> | |
<td><a href="%(base_uri)s/red-uri.asis#anchor1">%(base_uri)s/red-uri.asis#<b>anchor1</b></a></td> | |
<td>%(base_uri)s/test</td> | |
<td>%(base_uri)s/test#<b>anchor1</b></td> | |
</tr> | |
<tr> | |
<td>T5</td> | |
<td><a href="%(base_uri)s/red-path+frag.asis">%(base_uri)s/red-path+frag.asis</a></td> | |
<td>/test#<b>anchor2</b></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
</tr> | |
<tr> | |
<td>T6</td> | |
<td><a href="%(base_uri)s/red-path.asis">%(base_uri)s/red-path.asis</a></td> | |
<td>/test</td> | |
<td>%(base_uri)s/test</td> | |
</tr> | |
<tr> | |
<td>T7</td> | |
<td><a href="%(base_uri)s/red-path+frag.asis#anchor1">%(base_uri)s/red-path+frag.asis#<b>anchor1</b></a></td> | |
<td>/test#<b>anchor2</b></td> | |
<td>%(base_uri)s/test#<b>anchor2</b></td> | |
</tr> | |
<tr> | |
<td>T8</td> | |
<td><a href="%(base_uri)s/red-path.asis#anchor1">%(base_uri)s/red-path.asis#<b>anchor1</b></a></td> | |
<td>/test</td> | |
<td>%(base_uri)s/test#<b>anchor1</b></td> | |
</tr> | |
</tbody> | |
</table> | |
</body> | |
</html> | |
""", | |
'test': """\ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Paths and fragments in Location header</title> | |
<style type="text/css"> | |
.body { | |
width: 400px; | |
} | |
.fail { | |
background-color: #ffd0d0; | |
} | |
.warn { | |
background-color: #ffff80; | |
} | |
.pass { | |
background-color: #d0ffd0; | |
} | |
</style> | |
</head> | |
<body onLoad="alert(location.hash);"> | |
<h1>Page Top</h1> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<h1 id="anchor1">Target 1</h1> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<h1 id="anchor2">Target 2</h1> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<h1>Page Bottom</h1> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
<p>%(lorum)s</p> | |
</body> | |
</html> | |
""" | |
} | |
lorum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
def test_handler_factory(base_uri): | |
def test_handler(method, uri, hdrs, res_start, req_pause): | |
uri_seg = uri.split('/')[-1] | |
if redirs.get(uri_seg, None): | |
redir_uri = redirs[uri_seg] % {'base_uri': base_uri} | |
print "redirect to:", redir_uri | |
code = "302" | |
phrase = "Redirect" | |
res_hdrs = [('Content-Type', 'text/plain'), ('Location', redir_uri)] | |
body = "Redirecting" | |
elif pages.get(uri_seg, None): | |
print "static:", uri_seg | |
code = "200" | |
phrase = "OK" | |
res_hdrs = [('Content-Type', 'text/html')] | |
body = pages[uri_seg] % {'base_uri': base_uri, 'lorum': lorum} | |
else: | |
print "not found:", uri_seg | |
code="404" | |
phrase="Not Found" | |
res_hdrs = [('Content-Type', 'text/plain')] | |
body = "Not Found:", uri_seg | |
res_body, res_done = res_start(code, phrase, res_hdrs, nbhttp.dummy) | |
res_body(body) | |
res_done(None) | |
return nbhttp.dummy, nbhttp.dummy | |
return test_handler | |
if __name__ == "__main__": | |
import sys, os | |
h, p = sys.argv[1], int(sys.argv[2]) | |
base_uri = 'http://%s:%s/' % (h,p) | |
server = nbhttp.Server("", p, test_handler_factory(base_uri)) | |
sys.stderr.write("PID %s listening on %s\n" % (os.getpid(), p)) | |
nbhttp.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment