File: test_exceptions.py

package info (click to toggle)
circuits 3.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,980 kB
  • sloc: python: 17,583; javascript: 3,226; makefile: 100
file content (98 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
import json

from circuits.web import Controller
from circuits.web.exceptions import Forbidden, NotFound, Redirect

from .helpers import HTTPError, urlopen


class Root(Controller):
    def index(self):
        return 'Hello World!'

    def test_redirect(self):
        raise Redirect('/')

    def test_forbidden(self):
        raise Forbidden()

    def test_notfound(self):
        raise NotFound()

    def test_contenttype(self):
        raise Exception()

    def test_contenttype_json(self):
        self.response.headers['Content-Type'] = 'application/json'
        raise Exception()

    def test_contenttype_json_no_debug(self):
        self.response.headers['Content-Type'] = 'application/json'
        self.request.print_debug = False
        raise Exception()


def test_redirect(webapp):
    f = urlopen('%s/test_redirect' % webapp.server.http.base)
    s = f.read()
    assert s == b'Hello World!'


def test_forbidden(webapp):
    try:
        urlopen('%s/test_forbidden' % webapp.server.http.base)
    except HTTPError as e:
        assert e.code == 403
        assert e.msg == 'Forbidden'
    else:
        assert False


def test_notfound(webapp):
    try:
        urlopen('%s/test_notfound' % webapp.server.http.base)
    except HTTPError as e:
        assert e.code == 404
        assert e.msg == 'Not Found'
    else:
        assert False


def test_contenttype(webapp):
    try:
        urlopen('%s/test_contenttype' % webapp.server.http.base)
    except HTTPError as e:
        assert e.code == 500
        assert e.msg == 'Internal Server Error'
        assert 'text/html' in e.headers.get('Content-Type')
    else:
        assert False


def test_contenttype_json(webapp):
    try:
        urlopen('%s/test_contenttype_json' % webapp.server.http.base)
    except HTTPError as e:
        assert 'json' in e.headers.get('Content-Type')
        result = json.loads(e.read().decode('utf-8'))
        assert result['code'] == 500
        assert result['name'] == 'Internal Server Error'
        assert result['description'] == ''
        assert 'raise Exception' in result['traceback']
    else:
        assert False


def test_contenttype_json_no_debug(webapp):
    try:
        urlopen('%s/test_contenttype_json_no_debug' % webapp.server.http.base)
    except HTTPError as e:
        assert 'json' in e.headers.get('Content-Type')
        result = json.loads(e.read().decode('utf-8'))
        assert result['code'] == 500
        assert result['name'] == 'Internal Server Error'
        assert result['description'] == ''
        assert 'traceback' not in result
    else:
        assert False