PageRenderTime 12ms CodeModel.GetById 3ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/web/test_wsgi_application.py

https://bitbucket.org/prologic/circuits/
Python | 81 lines | 57 code | 23 blank | 1 comment | 4 complexity | c7d8b08d6fccdb4c871ffde98e77e4c0 MD5 | raw file
 1#!/usr/bin/env python
 2
 3from circuits.web import Controller
 4from circuits.web.wsgi import Application
 5
 6from .helpers import urlencode, urlopen, HTTPError
 7
 8
 9class Root(Controller):
10
11    def index(self):
12        return "Hello World!"
13
14    def test_args(self, *args, **kwargs):
15        args = [arg if isinstance(arg, str) else arg.encode() for arg in args]
16        return "%s\n%s" % (repr(tuple(args)), repr(kwargs))
17
18    def test_redirect(self):
19        return self.redirect("/")
20
21    def test_forbidden(self):
22        return self.forbidden()
23
24    def test_notfound(self):
25        return self.notfound()
26
27application = Application() + Root()
28
29
30def test(webapp):
31    f = urlopen(webapp.server.http.base)
32    s = f.read()
33    assert s == b"Hello World!"
34
35
36def test_404(webapp):
37    try:
38        urlopen("%s/foo" % webapp.server.http.base)
39    except HTTPError as e:
40        assert e.code == 404
41        assert e.msg == "Not Found"
42    else:
43        assert False
44
45
46def test_args(webapp):
47    args = ("1", "2", "3")
48    kwargs = {"1": "one", "2": "two", "3": "three"}
49    url = "%s/test_args/%s" % (webapp.server.http.base, "/".join(args))
50    data = urlencode(kwargs).encode()
51
52    f = urlopen(url, data)
53    data = f.read().split(b"\n")
54    assert eval(data[0]) == args
55    assert eval(data[1]) == kwargs
56
57
58def test_redirect(webapp):
59    f = urlopen("%s/test_redirect" % webapp.server.http.base)
60    s = f.read()
61    assert s == b"Hello World!"
62
63
64def test_forbidden(webapp):
65    try:
66        urlopen("%s/test_forbidden" % webapp.server.http.base)
67    except HTTPError as e:
68        assert e.code == 403
69        assert e.msg == "Forbidden"
70    else:
71        assert False
72
73
74def test_notfound(webapp):
75    try:
76        urlopen("%s/test_notfound" % webapp.server.http.base)
77    except HTTPError as e:
78        assert e.code == 404
79        assert e.msg == "Not Found"
80    else:
81        assert False