Ticket #43: redir_frag_test.py

File redir_frag_test.py, 6.8 KB (added by mnot@…, 13 years ago)

test script

Line 
1#!/usr/bin/env python
2
3"""
4Test for browser redirection handling WRT URI fragments, quick and dirty.
5
6Usage:
7 ./redir_frag_test.py hostname port_number
8"""
9
10import nbhttp # http://github.com/mnot/nbhttp
11
12# mapping of URIs to redirect
13redirs = {
14    'red-uri.asis': '%(base_uri)s/test',
15    'red-uri+frag.asis': '%(base_uri)s/test#anchor2',
16    'red-path.asis': '/test',
17    'red-path+frag.asis': '/test#anchor2',
18}
19
20# mapping of static pages to serve
21pages = {
22    '' : """\
23<!DOCTYPE html>
24<html>
25  <head>
26    <title>Paths and fragments in Location header</title>
27    <style type="text/css">
28.fail {
29  background-color: #ffd0d0;
30}
31.warn {
32  background-color: #ffff80;
33}
34.pass {
35  background-color: #d0ffd0;
36}
37    </style>
38
39  </head>
40  <body>
41    <h1>Paths and fragments in Location header</h1>
42    <p>
43      See <a href="http://trac.tools.ietf.org/wg/httpbis/trac/ticket/43">Ticket 43: Fragment combination / precedence during redirects</a>
44      and <a href="http://trac.tools.ietf.org/wg/httpbis/trac/ticket/185">Ticket 185: Location header payload handling</a>.
45    </p>
46 
47    <h2>Test Cases</h2>
48       
49    <table>
50      <thead>
51        <tr>
52          <th>Case</th>
53          <th>Original URI</th>
54          <th>Location header</th>
55          <th>Expected Final URI</th>
56          <th>Chrome / Firefox / IE8 / Safari</th>
57          <th>Opera</th>
58        </tr>
59      </thead>
60      <tbody>
61        <tr>
62          <td>T1</td>
63          <td><a href="%(base_uri)s/red-uri+frag.asis">%(base_uri)s/red-uri+frag.asis</a></td>
64          <td>%(base_uri)s/test#<b>anchor2</b></td>
65          <td>%(base_uri)s/test#<b>anchor2</b></td>
66
67          <td class="pass">OK</td>
68          <td class="pass">OK</td>
69        </tr>
70        <tr>
71         <td>T2</td>
72          <td><a href="%(base_uri)s/red-uri.asis">%(base_uri)s/red-uri.asis</a></td>
73          <td>%(base_uri)s/test</td>
74          <td>%(base_uri)s/test</td>
75
76          <td class="pass">OK</td>
77          <td class="pass">OK</td>
78        </tr>
79        <tr>
80          <td>T3</td>
81          <td><a href="%(base_uri)s/red-uri+frag.asis#anchor1">%(base_uri)s/red-uri+frag.asis#<b>anchor1</b></a></td>
82          <td>%(base_uri)s/test#<b>anchor2</b></td>
83          <td>%(base_uri)s/test#<b>anchor2</b></td>
84
85          <td class="pass">OK</td>
86          <td class="fail">Fail: uses orig. fragment</td>
87        </tr>
88        <tr>
89          <td>T4</td>
90          <td><a href="%(base_uri)s/red-uri.asis#anchor1">%(base_uri)s/red-uri.asis#<b>anchor1</b></a></td>
91          <td>%(base_uri)s/test</td>
92          <td>%(base_uri)s/test#<b>anchor1</b></td>
93
94          <td class="pass">OK</td>
95          <td class="pass">OK</td>
96        </tr>
97        <tr>
98          <td>T5</td>
99          <td><a href="%(base_uri)s/red-path+frag.asis">%(base_uri)s/red-path+frag.asis</a></td>
100          <td>/test#<b>anchor2</b></td>
101          <td>%(base_uri)s/test#<b>anchor2</b></td>
102
103          <td class="fail">Fail: fragment ignored</td>
104          <td class="fail">Fail: fragment ignored</td>
105        </tr>
106        <tr>
107          <td>T6</td>
108          <td><a href="%(base_uri)s/red-path.asis">%(base_uri)s/red-path.asis</a></td>
109          <td>/test</td>
110          <td>%(base_uri)s/test</td>
111
112          <td class="pass">OK</td>
113          <td class="pass">OK</td>
114        </tr>
115        <tr>
116          <td>T7</td>
117          <td><a href="%(base_uri)s/red-path+frag.asis#anchor1">%(base_uri)s/red-path+frag.asis#<b>anchor1</b></a></td>
118          <td>/test#<b>anchor2</b></td>
119          <td>%(base_uri)s/test#<b>anchor2</b></td>
120
121          <td class="fail">Fail: uses orig. fragment</td>
122          <td class="fail">Fail: uses orig. fragment</td>
123        </tr>
124        <tr>
125          <td>T8</td>
126          <td><a href="%(base_uri)s/red-path.asis#anchor1">%(base_uri)s/red-path.asis#<b>anchor1</b></a></td>
127          <td>/test</td>
128          <td>%(base_uri)s/test#<b>anchor1</b></td>
129
130          <td class="pass">OK</td>
131          <td class="pass">OK</td>
132        </tr>
133      </tbody>
134    </table>
135   
136  </body>
137</html>
138""",
139    'test': """\
140    <!DOCTYPE html>
141    <html>
142      <head>
143        <title>Paths and fragments in Location header</title>
144        <style type="text/css">
145    .body {
146      width: 400px;
147    }
148    .fail {
149      background-color: #ffd0d0;
150    }
151    .warn {
152      background-color: #ffff80;
153    }
154    .pass {
155      background-color: #d0ffd0;
156    }
157        </style>
158
159      </head>
160      <body>
161        <h1>Page Top</h1>
162        <p>%(lorum)s</p>       
163        <p>%(lorum)s</p>       
164        <p>%(lorum)s</p>       
165        <h1 id="anchor1">Target 1</h1>
166        <p>%(lorum)s</p>       
167        <p>%(lorum)s</p>       
168        <p>%(lorum)s</p>       
169        <h1 id="anchor2">Target 2</h1>
170        <p>%(lorum)s</p>       
171        <p>%(lorum)s</p>       
172        <p>%(lorum)s</p>       
173        <h1>Page Bottom</h1>
174        <p>%(lorum)s</p>       
175        <p>%(lorum)s</p>       
176        <p>%(lorum)s</p>       
177        <p>%(lorum)s</p>       
178        <p>%(lorum)s</p>       
179        <p>%(lorum)s</p>       
180        <p>%(lorum)s</p>       
181      </body>
182    </html>
183"""
184}
185
186lorum = "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."
187
188def test_handler_factory(base_uri):
189    def test_handler(method, uri, hdrs, res_start, req_pause):
190        uri_seg = uri.split('/')[-1]
191        if redirs.get(uri_seg, None):
192            redir_uri = redirs[uri_seg] % {'base_uri': base_uri}
193            print "redirect to:", redir_uri
194            code = "302"
195            phrase = "Redirect"
196            res_hdrs = [('Content-Type', 'text/plain'), ('Location', redir_uri)]
197            body = "Redirecting"
198        elif pages.get(uri_seg, None):
199            print "static:", uri_seg
200            code = "200"
201            phrase = "OK"
202            res_hdrs = [('Content-Type', 'text/html')]
203            body = pages[uri_seg] % {'base_uri': base_uri, 'lorum': lorum}
204        else:
205            print "not found:", uri_seg
206            code="404"
207            phrase="Not Found"
208            res_hdrs = [('Content-Type', 'text/plain')]
209            body = "Not Found:", uri_seg
210        res_body, res_done = res_start(code, phrase, res_hdrs, nbhttp.dummy)
211        res_body(body)
212        res_done(None)
213        return nbhttp.dummy, nbhttp.dummy
214    return test_handler
215
216
217if __name__ == "__main__":
218    import sys, os
219    h, p = sys.argv[1], int(sys.argv[2])
220    base_uri = 'http://%s:%s/' % (h,p)
221    server = nbhttp.Server("", p, test_handler_factory(base_uri))
222    sys.stderr.write("PID %s listening on %s\n" % (os.getpid(), p))
223    nbhttp.run()