~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/web/test/test_error.py

  • Committer: Marc Tardif
  • Date: 2010-05-20 19:56:06 UTC
  • Revision ID: marc.tardif@canonical.com-20100520195606-xdrf0ztlxhvwmmzb
Added twisted-web.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
"""
 
5
HTTP errors.
 
6
"""
 
7
 
 
8
from twisted.trial import unittest
 
9
from twisted.web import error
 
10
 
 
11
class ErrorTestCase(unittest.TestCase):
 
12
    """
 
13
    Tests for how L{Error} attributes are initialized.
 
14
    """
 
15
    def test_noMessageValidStatus(self):
 
16
        """
 
17
        If no C{message} argument is passed to the L{Error} constructor and the
 
18
        C{code} argument is a valid HTTP status code, C{code} is mapped to a
 
19
        descriptive string to which C{message} is assigned.
 
20
        """
 
21
        e = error.Error("200")
 
22
        self.assertEquals(e.message, "OK")
 
23
 
 
24
 
 
25
    def test_noMessageInvalidStatus(self):
 
26
        """
 
27
        If no C{message} argument is passed to the L{Error} constructor and
 
28
        C{code} isn't a valid HTTP status code, C{message} stays C{None}.
 
29
        """
 
30
        e = error.Error("InvalidCode")
 
31
        self.assertEquals(e.message, None)
 
32
 
 
33
 
 
34
    def test_messageExists(self):
 
35
        """
 
36
        If a C{message} argument is passed to the L{Error} constructor, the
 
37
        C{message} isn't affected by the value of C{status}.
 
38
        """
 
39
        e = error.Error("200", "My own message")
 
40
        self.assertEquals(e.message, "My own message")
 
41
 
 
42
 
 
43
 
 
44
class PageRedirectTestCase(unittest.TestCase):
 
45
    """
 
46
    Tests for how L{PageRedirect} attributes are initialized.
 
47
    """
 
48
    def test_noMessageValidStatus(self):
 
49
        """
 
50
        If no C{message} argument is passed to the L{PageRedirect} constructor
 
51
        and the C{code} argument is a valid HTTP status code, C{code} is mapped
 
52
        to a descriptive string to which C{message} is assigned.
 
53
        """
 
54
        e = error.PageRedirect("200", location="/foo")
 
55
        self.assertEquals(e.message, "OK to /foo")
 
56
 
 
57
 
 
58
    def test_noMessageValidStatusNoLocation(self):
 
59
        """
 
60
        If no C{message} argument is passed to the L{PageRedirect} constructor
 
61
        and C{location} is also empty and the C{code} argument is a valid HTTP
 
62
        status code, C{code} is mapped to a descriptive string to which
 
63
        C{message} is assigned without trying to include an empty location.
 
64
        """
 
65
        e = error.PageRedirect("200")
 
66
        self.assertEquals(e.message, "OK")
 
67
 
 
68
 
 
69
    def test_noMessageInvalidStatusLocationExists(self):
 
70
        """
 
71
        If no C{message} argument is passed to the L{PageRedirect} constructor
 
72
        and C{code} isn't a valid HTTP status code, C{message} stays C{None}.
 
73
        """
 
74
        e = error.PageRedirect("InvalidCode", location="/foo")
 
75
        self.assertEquals(e.message, None)
 
76
 
 
77
 
 
78
    def test_messageExistsLocationExists(self):
 
79
        """
 
80
        If a C{message} argument is passed to the L{PageRedirect} constructor,
 
81
        the C{message} isn't affected by the value of C{status}.
 
82
        """
 
83
        e = error.PageRedirect("200", "My own message", location="/foo")
 
84
        self.assertEquals(e.message, "My own message to /foo")
 
85
 
 
86
 
 
87
    def test_messageExistsNoLocation(self):
 
88
        """
 
89
        If a C{message} argument is passed to the L{PageRedirect} constructor
 
90
        and no location is provided, C{message} doesn't try to include the empty
 
91
        location.
 
92
        """
 
93
        e = error.PageRedirect("200", "My own message")
 
94
        self.assertEquals(e.message, "My own message")
 
95
 
 
96
 
 
97
 
 
98
class InfiniteRedirectionTestCase(unittest.TestCase):
 
99
    """
 
100
    Tests for how L{InfiniteRedirection} attributes are initialized.
 
101
    """
 
102
    def test_noMessageValidStatus(self):
 
103
        """
 
104
        If no C{message} argument is passed to the L{InfiniteRedirection}
 
105
        constructor and the C{code} argument is a valid HTTP status code,
 
106
        C{code} is mapped to a descriptive string to which C{message} is
 
107
        assigned.
 
108
        """
 
109
        e = error.InfiniteRedirection("200", location="/foo")
 
110
        self.assertEquals(e.message, "OK to /foo")
 
111
 
 
112
 
 
113
    def test_noMessageValidStatusNoLocation(self):
 
114
        """
 
115
        If no C{message} argument is passed to the L{InfiniteRedirection}
 
116
        constructor and C{location} is also empty and the C{code} argument is a
 
117
        valid HTTP status code, C{code} is mapped to a descriptive string to
 
118
        which C{message} is assigned without trying to include an empty
 
119
        location.
 
120
        """
 
121
        e = error.InfiniteRedirection("200")
 
122
        self.assertEquals(e.message, "OK")
 
123
 
 
124
 
 
125
    def test_noMessageInvalidStatusLocationExists(self):
 
126
        """
 
127
        If no C{message} argument is passed to the L{InfiniteRedirection}
 
128
        constructor and C{code} isn't a valid HTTP status code, C{message} stays
 
129
        C{None}.
 
130
        """
 
131
        e = error.InfiniteRedirection("InvalidCode", location="/foo")
 
132
        self.assertEquals(e.message, None)
 
133
 
 
134
 
 
135
    def test_messageExistsLocationExists(self):
 
136
        """
 
137
        If a C{message} argument is passed to the L{InfiniteRedirection}
 
138
        constructor, the C{message} isn't affected by the value of C{status}.
 
139
        """
 
140
        e = error.InfiniteRedirection("200", "My own message", location="/foo")
 
141
        self.assertEquals(e.message, "My own message to /foo")
 
142
 
 
143
 
 
144
    def test_messageExistsNoLocation(self):
 
145
        """
 
146
        If a C{message} argument is passed to the L{InfiniteRedirection}
 
147
        constructor and no location is provided, C{message} doesn't try to
 
148
        include the empty location.
 
149
        """
 
150
        e = error.InfiniteRedirection("200", "My own message")
 
151
        self.assertEquals(e.message, "My own message")