Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 348 lines (279 sloc) 10.801 kb
ca35d12 Anand Chitipothu only uppercase HTTP methods are allowed. (fix for bug@#176415)
anandology authored
1 import webtest
2 import time
bce568c Anand Chitipothu Added app.stop() by making server a global variable. #122
anandology authored
3 import threading
ca35d12 Anand Chitipothu only uppercase HTTP methods are allowed. (fix for bug@#176415)
anandology authored
4
5 import web
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
6 import urllib
ca35d12 Anand Chitipothu only uppercase HTTP methods are allowed. (fix for bug@#176415)
anandology authored
7
8 data = """
9 import web
10
11 urls = ("/", "%(classname)s")
12 app = web.application(urls, globals(), autoreload=True)
13
14 class %(classname)s:
15 def GET(self):
16 return "%(output)s"
17
18 """
19
f75d4b7 Anand Chitipothu added failing test: testIter
anandology authored
20 urls = (
21 "/iter", "do_iter",
22 )
23 app = web.application(urls, globals())
24
25 class do_iter:
26 def GET(self):
27 yield 'hello, '
28 yield web.input(name='world').name
29
30 POST = GET
31
ca35d12 Anand Chitipothu only uppercase HTTP methods are allowed. (fix for bug@#176415)
anandology authored
32 def write(filename, data):
33 f = open(filename, 'w')
34 f.write(data)
35 f.close()
36
37 class ApplicationTest(webtest.TestCase):
38 def test_reloader(self):
39 write('foo.py', data % dict(classname='a', output='a'))
40 import foo
41 app = foo.app
42
43 self.assertEquals(app.request('/').data, 'a')
44
45 # test class change
46 time.sleep(1)
47 write('foo.py', data % dict(classname='a', output='b'))
48 self.assertEquals(app.request('/').data, 'b')
49
50 # test urls change
51 time.sleep(1)
52 write('foo.py', data % dict(classname='c', output='c'))
53 self.assertEquals(app.request('/').data, 'c')
54
55 def testUppercaseMethods(self):
56 urls = ("/", "hello")
57 app = web.application(urls, locals())
58 class hello:
59 def GET(self): return "hello"
60 def internal(self): return "secret"
61
62 response = app.request('/', method='internal')
63 self.assertEquals(response.status, '405 Method Not Allowed')
2eeb1f4 support for redirect in urls (Bug#216097)
Anand authored
64
65 def testRedirect(self):
66 urls = (
67 "/a", "redirect /hello/",
68 "/b/(.*)", r"redirect /hello/\1",
69 "/hello/(.*)", "hello"
70 )
71 app = web.application(urls, locals())
72 class hello:
73 def GET(self, name):
74 name = name or 'world'
75 return "hello " + name
76
77 response = app.request('/a')
78 self.assertEquals(response.status, '301 Moved Permanently')
79 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/hello/')
80
81 response = app.request('/a?x=2')
82 self.assertEquals(response.status, '301 Moved Permanently')
83 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/hello/?x=2')
84
85 response = app.request('/b/foo?x=2')
86 self.assertEquals(response.status, '301 Moved Permanently')
87 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/hello/foo?x=2')
70ae7c7 truncate path when using a subapplication (Bug#216102)
Anand authored
88
89 def test_subdirs(self):
90 urls = (
91 "/(.*)", "blog"
92 )
93 class blog:
94 def GET(self, path):
95 return "blog " + path
96 app_blog = web.application(urls, locals())
97
98 urls = (
99 "/blog", app_blog,
100 "/(.*)", "index"
101 )
102 class index:
103 def GET(self, path):
104 return "hello " + path
105 app = web.application(urls, locals())
106
107 self.assertEquals(app.request('/blog/foo').data, 'blog foo')
108 self.assertEquals(app.request('/foo').data, 'hello foo')
109
110 def processor(handler):
111 return web.ctx.path + ":" + handler()
112 app.add_processor(processor)
113 self.assertEquals(app.request('/blog/foo').data, '/blog/foo:blog foo')
dfa1aea Anand Chitipothu better notfound customization
anandology authored
114
115 def test_subdomains(self):
116 def create_app(name):
117 urls = ("/", "index")
118 class index:
119 def GET(self):
120 return name
121 return web.application(urls, locals())
122
123 urls = (
124 "a.example.com", create_app('a'),
125 "b.example.com", create_app('b'),
126 ".*.example.com", create_app('*')
127 )
128 app = web.subdomain_application(urls, locals())
129
130 def test(host, expected_result):
131 result = app.request('/', host=host)
132 self.assertEquals(result.data, expected_result)
133
134 test('a.example.com', 'a')
135 test('b.example.com', 'b')
136 test('c.example.com', '*')
137 test('d.example.com', '*')
85bbdc1 fix absolute redirect problem. (Bug#261521)
Anand authored
138
139 def test_redirect(self):
140 urls = (
141 "/(.*)", "blog"
142 )
143 class blog:
144 def GET(self, path):
145 if path == 'foo':
146 raise web.seeother('/login', absolute=True)
147 else:
148 raise web.seeother('/bar')
149 app_blog = web.application(urls, locals())
150
151 urls = (
152 "/blog", app_blog,
153 "/(.*)", "index"
154 )
155 class index:
156 def GET(self, path):
157 return "hello " + path
158 app = web.application(urls, locals())
159
160 response = app.request('/blog/foo')
161 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/login')
162
163 response = app.request('/blog/foo', env={'SCRIPT_NAME': '/x'})
164 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/x/login')
165
166 response = app.request('/blog/foo2')
167 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/blog/bar')
168
169 response = app.request('/blog/foo2', env={'SCRIPT_NAME': '/x'})
170 self.assertEquals(response.headers['Location'], 'http://0.0.0.0:8080/x/blog/bar')
171
ae5e8a1 Anand Chitipothu fixed bug in executing processors of sub-application
anandology authored
172 def test_processors(self):
173 urls = (
174 "/(.*)", "blog"
175 )
176 class blog:
177 def GET(self, path):
178 return 'blog ' + path
179
180 state = web.storage(x=0, y=0)
181 def f():
182 state.x += 1
183
184 app_blog = web.application(urls, locals())
185 app_blog.add_processor(web.loadhook(f))
186
187 urls = (
188 "/blog", app_blog,
189 "/(.*)", "index"
190 )
191 class index:
192 def GET(self, path):
193 return "hello " + path
194 app = web.application(urls, locals())
195 def g():
196 state.y += 1
197 app.add_processor(web.loadhook(g))
198
199 app.request('/blog/foo')
200 assert state.x == 1 and state.y == 1, repr(state)
201 app.request('/foo')
202 assert state.x == 1 and state.y == 2, repr(state)
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
203
204 def testUnicodeInput(self):
205 urls = (
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
206 "(/.*)", "foo"
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
207 )
208 class foo:
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
209 def GET(self, path):
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
210 i = web.input(name='')
211 return repr(i.name)
212
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
213 def POST(self, path):
214 if path == '/multipart':
215 i = web.input(file={})
216 return i.file.value
217 else:
218 i = web.input()
219 return repr(dict(i))
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
220
221 app = web.application(urls, locals())
222
223 def f(name):
9b27481 Anand Chitipothu fix testUnicodeInput
anandology authored
224 path = '/?' + urllib.urlencode({"name": name.encode('utf-8')})
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
225 self.assertEquals(app.request(path).data, repr(name))
226
227 f(u'\u1234')
228 f(u'foo')
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
229
230 response = app.request('/', method='POST', data=dict(name='foo'))
231 self.assertEquals(response.data, "{'name': u'foo'}")
3ba0911 Anand Chitipothu unicodify web.input
anandology authored
232
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
233 data = '--boundary\r\nContent-Disposition: form-data; name="x"\r\nfoo\r\n--boundary\r\nContent-Disposition: form-data; name="file"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\na\r\n--boundary--\r\n'
234 headers = {'Content-Type': 'multipart/form-data; boundary=boundary'}
235 response = app.request('/multipart', method="POST", data=data, headers=headers)
236 self.assertEquals(response.data, 'a')
dfa1aea Anand Chitipothu better notfound customization
anandology authored
237
238 def testCustomNotFound(self):
239 urls_a = ("/", "a")
240 urls_b = ("/", "b")
241
242 app_a = web.application(urls_a, locals())
243 app_b = web.application(urls_b, locals())
244
245 app_a.notfound = lambda: web.HTTPError("404 Not Found", {}, "not found 1")
246
247 urls = (
248 "/a", app_a,
249 "/b", app_b
250 )
251 app = web.application(urls, locals())
252
253 def assert_notfound(path, message):
254 response = app.request(path)
255 self.assertEquals(response.status.split()[0], "404")
256 self.assertEquals(response.data, message)
257
258 assert_notfound("/a/foo", "not found 1")
259 assert_notfound("/b/foo", "not found")
260
261 app.notfound = lambda: web.HTTPError("404 Not Found", {}, "not found 2")
262 assert_notfound("/a/foo", "not found 1")
263 assert_notfound("/b/foo", "not found 2")
4c1d7ca Anand Chitipothu bug fix in application.request
anandology authored
264
f75d4b7 Anand Chitipothu added failing test: testIter
anandology authored
265 def testIter(self):
266 self.assertEquals(app.request('/iter').data, 'hello, world')
267 self.assertEquals(app.request('/iter?name=web').data, 'hello, web')
268
269 self.assertEquals(app.request('/iter', method='POST').data, 'hello, world')
270 self.assertEquals(app.request('/iter', method='POST', data='name=web').data, 'hello, web')
271
06a9549 Anand Chitipothu Run unload hook even when handling request raises exception (Bug#372837)
anandology authored
272 def testUnload(self):
273 x = web.storage(a=0)
274
275 urls = (
276 "/foo", "foo",
277 "/bar", "bar"
278 )
279 class foo:
280 def GET(self):
281 return "foo"
282 class bar:
283 def GET(self):
284 raise web.notfound()
285
286 app = web.application(urls, locals())
287 def unload():
288 x.a += 1
289 app.add_processor(web.unloadhook(unload))
290
291 app.request('/foo')
292 self.assertEquals(x.a, 1)
293
294 app.request('/bar')
295 self.assertEquals(x.a, 2)
0cae750 Anand Chitipothu fix changequery to make it work correctly even when the input has multi-...
anandology authored
296
297 def test_changequery(self):
298 urls = (
299 '/', 'index',
300 )
301 class index:
302 def GET(self):
303 return web.changequery(x=1)
304 app = web.application(urls, locals())
305
306 def f(path):
307 return app.request(path).data
308
309 self.assertEquals(f('/?x=2'), '/?x=1')
310 self.assertEquals(f('/?y=1&y=2&x=2'), '/?y=1&y=2&x=1')
8c1ef7b Anand Chitipothu Improvements to setcookie (tx lovelylain) #65 (closes #39)
anandology authored
311
312 def test_setcookie(self):
313 urls = (
314 '/', 'index',
315 )
316 class index:
317 def GET(self):
318 web.setcookie("foo", "bar")
319 return "hello"
320 app = web.application(urls, locals())
321 def f(script_name=""):
322 response = app.request("/", env={"SCRIPT_NAME": script_name})
323 return response.headers['Set-Cookie']
324
325 self.assertEquals(f(''), 'foo=bar; Path=/')
326 self.assertEquals(f('/admin'), 'foo=bar; Path=/admin/')
06a9549 Anand Chitipothu Run unload hook even when handling request raises exception (Bug#372837)
anandology authored
327
bce568c Anand Chitipothu Added app.stop() by making server a global variable. #122
anandology authored
328 def test_stopsimpleserver(self):
329 urls = (
330 '/', 'index',
331 )
332 class index:
333 def GET(self):
334 pass
335 app = web.application(urls, locals())
336 thread = threading.Thread(target=app.run)
337
338 thread.start()
339 time.sleep(1)
257a2ac Anand Chitipothu Fixed Travis-CI failures.
anandology authored
340 self.assertTrue(thread.isAlive())
bce568c Anand Chitipothu Added app.stop() by making server a global variable. #122
anandology authored
341
342 app.stop()
343 thread.join(timeout=1)
257a2ac Anand Chitipothu Fixed Travis-CI failures.
anandology authored
344 self.assertFalse(thread.isAlive())
bce568c Anand Chitipothu Added app.stop() by making server a global variable. #122
anandology authored
345
ca35d12 Anand Chitipothu only uppercase HTTP methods are allowed. (fix for bug@#176415)
anandology authored
346 if __name__ == '__main__':
347 webtest.main()
Something went wrong with that request. Please try again.