public
Last active

Test for browser redirection handling WRT URI fragments, quick and dirty.

  • Download Gist
redir_frag_test.py
Python
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
#!/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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.