PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/web/test_core.py

https://bitbucket.org/prologic/circuits/
Python | 112 lines | 81 code | 30 blank | 1 comment | 0 complexity | 4bd3a0529b66c71a53313c5778bd8e44 MD5 | raw file
  1#!/usr/bin/env python
  2
  3import pytest
  4
  5
  6from circuits.six import b, u
  7from circuits.web import Controller
  8
  9
 10from .helpers import urlencode, urlopen, HTTPError
 11
 12
 13class Root(Controller):
 14
 15    def index(self):
 16        return "Hello World!"
 17
 18    def test_args(self, *args, **kwargs):
 19        return "{0}\n{1}".format(repr(args), repr(kwargs))
 20
 21    def test_default_args(self, a=None, b=None):
 22        return "a={0}\nb={1}".format(a, b)
 23
 24    def test_redirect(self):
 25        return self.redirect("/")
 26
 27    def test_forbidden(self):
 28        return self.forbidden()
 29
 30    def test_notfound(self):
 31        return self.notfound()
 32
 33    def test_failure(self):
 34        raise Exception()
 35
 36
 37def test_root(webapp):
 38    f = urlopen(webapp.server.http.base)
 39    s = f.read()
 40    assert s == b"Hello World!"
 41
 42
 43def test_404(webapp):
 44    try:
 45        urlopen("%s/foo" % webapp.server.http.base)
 46    except HTTPError as e:
 47        assert e.code == 404
 48        assert e.msg == "Not Found"
 49    else:
 50        assert False
 51
 52
 53def test_args(webapp):
 54    args = ("1", "2", "3")
 55    kwargs = {"1": "one", "2": "two", "3": "three"}
 56    url = "%s/test_args/%s" % (webapp.server.http.base, "/".join(args))
 57    data = urlencode(kwargs).encode('utf-8')
 58    f = urlopen(url, data)
 59    data = f.read().split(b"\n")
 60    assert eval(data[0]) == args
 61    assert eval(data[1]) == kwargs
 62
 63
 64@pytest.mark.parametrize("data,expected", [
 65    ((["1"], {}),           b("a=1\nb=None")),
 66    ((["1", "2"], {}),      b("a=1\nb=2")),
 67    ((["1"], {"b": "2"}),   b("a=1\nb=2")),
 68])
 69def test_default_args(webapp, data, expected):
 70    args, kwargs = data
 71    url = u("{0:s}/test_default_args/{1:s}".format(
 72        webapp.server.http.base,
 73        u("/").join(args)
 74    ))
 75    data = urlencode(kwargs).encode("utf-8")
 76    f = urlopen(url, data)
 77    assert f.read() == expected
 78
 79
 80def test_redirect(webapp):
 81    f = urlopen("%s/test_redirect" % webapp.server.http.base)
 82    s = f.read()
 83    assert s == b"Hello World!"
 84
 85
 86def test_forbidden(webapp):
 87    try:
 88        urlopen("%s/test_forbidden" % webapp.server.http.base)
 89    except HTTPError as e:
 90        assert e.code == 403
 91        assert e.msg == "Forbidden"
 92    else:
 93        assert False
 94
 95
 96def test_notfound(webapp):
 97    try:
 98        urlopen("%s/test_notfound" % webapp.server.http.base)
 99    except HTTPError as e:
100        assert e.code == 404
101        assert e.msg == "Not Found"
102    else:
103        assert False
104
105
106def test_failure(webapp):
107    try:
108        urlopen("%s/test_failure" % webapp.server.http.base)
109    except HTTPError as e:
110        assert e.code == 500
111    else:
112        assert False