~ubuntu-branches/ubuntu/trusty/python-webob/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/test_exc.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-07 07:52:32 UTC
  • mfrom: (1.3.5)
  • Revision ID: package-import@ubuntu.com-20130107075232-w6x8r94du3t48wj4
Tags: 1.2.3-0ubuntu1
* New upstream release:
  - Dropped debian/patches/01_lp_920197.patch: no longer needed.
  - debian/watch: Update to point to pypi.
  - debian/rules: Disable docs build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from webob.request import Request
2
2
from webob.dec import wsgify
3
 
from webob.exc import sys
4
3
from webob.exc import no_escape
5
4
from webob.exc import strip_tags
6
5
from webob.exc import HTTPException
7
6
from webob.exc import WSGIHTTPException
8
 
from webob.exc import HTTPError
9
 
from webob.exc import HTTPRedirection
10
 
from webob.exc import HTTPRedirection
11
 
from webob.exc import HTTPOk
12
 
from webob.exc import HTTPCreated
13
 
from webob.exc import HTTPAccepted
14
 
from webob.exc import HTTPNonAuthoritativeInformation
15
 
from webob.exc import HTTPNoContent
16
 
from webob.exc import HTTPResetContent
17
 
from webob.exc import HTTPPartialContent
18
7
from webob.exc import _HTTPMove
19
 
from webob.exc import HTTPMultipleChoices
20
 
from webob.exc import HTTPMovedPermanently
21
 
from webob.exc import HTTPFound
22
 
from webob.exc import HTTPSeeOther
23
 
from webob.exc import HTTPNotModified
24
 
from webob.exc import HTTPUseProxy
25
 
from webob.exc import HTTPTemporaryRedirect
26
 
from webob.exc import HTTPClientError
27
 
from webob.exc import HTTPBadRequest
28
 
from webob.exc import HTTPUnauthorized
29
 
from webob.exc import HTTPPaymentRequired
30
 
from webob.exc import HTTPForbidden
31
 
from webob.exc import HTTPNotFound
32
8
from webob.exc import HTTPMethodNotAllowed
33
 
from webob.exc import HTTPNotAcceptable
34
 
from webob.exc import HTTPProxyAuthenticationRequired
35
 
from webob.exc import HTTPRequestTimeout
36
 
from webob.exc import HTTPConflict
37
 
from webob.exc import HTTPGone
38
 
from webob.exc import HTTPLengthRequired
39
 
from webob.exc import HTTPPreconditionFailed
40
 
from webob.exc import HTTPRequestEntityTooLarge
41
 
from webob.exc import HTTPRequestURITooLong
42
 
from webob.exc import HTTPUnsupportedMediaType
43
 
from webob.exc import HTTPRequestRangeNotSatisfiable
44
 
from webob.exc import HTTPExpectationFailed
45
 
from webob.exc import HTTPUnprocessableEntity
46
 
from webob.exc import HTTPLocked
47
 
from webob.exc import HTTPFailedDependency
48
 
from webob.exc import HTTPServerError
49
 
from webob.exc import HTTPInternalServerError
50
 
from webob.exc import HTTPNotImplemented
51
 
from webob.exc import HTTPBadGateway
52
 
from webob.exc import HTTPServiceUnavailable
53
 
from webob.exc import HTTPGatewayTimeout
54
 
from webob.exc import HTTPVersionNotSupported
55
 
from webob.exc import HTTPInsufficientStorage
56
9
from webob.exc import HTTPExceptionMiddleware
57
 
from webob import exc
58
10
from webob.exc import status_map
59
11
 
60
12
from nose.tools import eq_, ok_, assert_equal, assert_raises
74
26
def test_noescape_unicode():
75
27
    class DummyUnicodeObject(object):
76
28
        def __unicode__(self):
77
 
            return u'42'
 
29
            return '42'
78
30
    duo = DummyUnicodeObject()
79
 
    assert_equal(no_escape(duo), u'42')
 
31
    assert_equal(no_escape(duo), '42')
80
32
 
81
33
def test_strip_tags_empty():
82
34
    assert_equal(strip_tags(''), '')
97
49
    assert_equal(strip_tags('foo<bar>baz</bar>'), 'foobaz')
98
50
 
99
51
def test_HTTPException():
 
52
    import warnings
100
53
    _called = []
101
54
    _result = object()
102
55
    def _response(environ, start_response):
106
59
    start_response = object()
107
60
    exc = HTTPException('testing', _response)
108
61
    ok_(exc.wsgi_response is _response)
109
 
    ok_(exc.exception is exc)
 
62
    with warnings.catch_warnings(record=True) as w:
 
63
        warnings.simplefilter("always")
 
64
        assert(exc.exception is exc)
 
65
        assert(len(w) == 1)
110
66
    result = exc(environ, start_response)
111
67
    ok_(result is result)
112
68
    assert_equal(_called, [(environ, start_response)])
113
69
 
114
70
def test_exception_with_unicode_data():
115
 
    req = Request.blank('/', method=u'POST')
 
71
    req = Request.blank('/', method='POST')
116
72
    res = req.get_response(method_not_allowed_app)
117
 
    assert res.status_int == 405
 
73
    assert res.status_code == 405
118
74
 
119
75
def test_WSGIHTTPException_headers():
120
76
    exc = WSGIHTTPException(headers=[('Set-Cookie', 'a=1'),
186
142
    }
187
143
    excep = WSGIHTTPException()
188
144
    assert_equal( excep(environ,start_response), [
189
 
    '<html>\n'
190
 
    ' <head>\n'
191
 
    '  <title>None None</title>\n'
192
 
    ' </head>\n'
193
 
    ' <body>\n'
194
 
    '  <h1>None None</h1>\n'
195
 
    '  <br /><br />\n'
196
 
    '\n'
197
 
    '\n\n'
198
 
    ' </body>\n'
199
 
    '</html>' ]
 
145
        b'<html>\n'
 
146
        b' <head>\n'
 
147
        b'  <title>None None</title>\n'
 
148
        b' </head>\n'
 
149
        b' <body>\n'
 
150
        b'  <h1>None None</h1>\n'
 
151
        b'  <br /><br />\n'
 
152
        b'\n'
 
153
        b'\n\n'
 
154
        b' </body>\n'
 
155
        b'</html>' ]
200
156
    )
201
157
 
202
158
def test_WSGIHTTPException_call_w_body():
209
165
       'REQUEST_METHOD': 'PUT'
210
166
    }
211
167
    excep = WSGIHTTPException()
212
 
    excep.body = 'test'
213
 
    assert_equal( excep(environ,start_response), ['test'] )
 
168
    excep.body = b'test'
 
169
    assert_equal( excep(environ,start_response), [b'test'] )
214
170
 
215
171
 
216
172
def test_WSGIHTTPException_wsgi_response():
235
191
       'REQUEST_METHOD': 'HEAD'
236
192
    }
237
193
    excep = WSGIHTTPException()
 
194
    from webob import exc
238
195
    exc.newstyle_exceptions = True
239
196
    assert_equal( excep(environ,start_response), [] )
240
197
 
248
205
       'REQUEST_METHOD': 'HEAD'
249
206
    }
250
207
    excep = WSGIHTTPException()
 
208
    from webob import exc
251
209
    exc.newstyle_exceptions = False
252
210
    assert_equal( excep(environ,start_response), [] )
253
211
 
270
228
    def verify_response(resp, description):
271
229
        assert_equal(resp.content_type, CONTENT_TYPE, description)
272
230
        assert_equal(resp.content_length, len(HELLO_WORLD), description)
273
 
        assert_equal(resp.body, '', description)
 
231
        assert_equal(resp.body, b'', description)
274
232
 
275
233
    req = Request.blank('/', method='HEAD')
276
234
    resp1 = req.get_response(head_app)
295
253
       'wsgi.url_scheme': 'HTTP',
296
254
       'SERVER_NAME': 'localhost',
297
255
       'SERVER_PORT': '80',
298
 
       'REQUEST_METHOD': 'HEAD'
 
256
       'REQUEST_METHOD': 'HEAD',
 
257
       'PATH_INFO': '/',
299
258
    }
300
259
    m = _HTTPMove()
301
260
    assert_equal( m( environ, start_response ), [] )
307
266
       'wsgi.url_scheme': 'HTTP',
308
267
       'SERVER_NAME': 'localhost',
309
268
       'SERVER_PORT': '80',
310
 
       'REQUEST_METHOD': 'HEAD'
 
269
       'REQUEST_METHOD': 'HEAD',
 
270
       'PATH_INFO': '/',
311
271
    }
312
272
    m = _HTTPMove(location='http://example.com')
313
273
    assert_equal( m( environ, start_response ), [] )
315
275
def test_HTTPMove_add_slash_and_location():
316
276
    def start_response(status, headers, exc_info=None):
317
277
        pass
318
 
    environ = {
319
 
       'wsgi.url_scheme': 'HTTP',
320
 
       'SERVER_NAME': 'localhost',
321
 
       'SERVER_PORT': '80',
322
 
       'REQUEST_METHOD': 'HEAD'
323
 
    }
324
 
    assert_raises( TypeError, _HTTPMove, location='http://example.com', add_slash=True )
 
278
    assert_raises( TypeError, _HTTPMove, location='http://example.com',
 
279
                   add_slash=True )
325
280
 
326
281
def test_HTTPMove_call_add_slash():
327
282
    def start_response(status, headers, exc_info=None):
330
285
       'wsgi.url_scheme': 'HTTP',
331
286
       'SERVER_NAME': 'localhost',
332
287
       'SERVER_PORT': '80',
333
 
       'REQUEST_METHOD': 'HEAD'
 
288
       'REQUEST_METHOD': 'HEAD',
 
289
       'PATH_INFO': '/',
334
290
    }
335
291
    m = _HTTPMove()
336
292
    m.add_slash = True
348
304
    m = _HTTPMove()
349
305
    m.add_slash = True
350
306
    environ[ 'QUERY_STRING' ] = 'querystring'
 
307
    environ['PATH_INFO'] = '/'
351
308
    assert_equal( m( environ, start_response ), [] )
352
309
 
353
310
def test_HTTPExceptionMiddleware_ok():