1# -*- coding: utf-8 -*- 2import webob 3import webob.exc 4 5import webapp2 6 7import test_base 8 9 10class TestMiscellaneous(test_base.BaseTestCase): 11 12 def test_abort(self): 13 self.assertRaises(webob.exc.HTTPOk, webapp2.abort, 200) 14 self.assertRaises(webob.exc.HTTPCreated, webapp2.abort, 201) 15 self.assertRaises(webob.exc.HTTPAccepted, webapp2.abort, 202) 16 self.assertRaises(webob.exc.HTTPNonAuthoritativeInformation, webapp2.abort, 203) 17 self.assertRaises(webob.exc.HTTPNoContent, webapp2.abort, 204) 18 self.assertRaises(webob.exc.HTTPResetContent, webapp2.abort, 205) 19 self.assertRaises(webob.exc.HTTPPartialContent, webapp2.abort, 206) 20 self.assertRaises(webob.exc.HTTPMultipleChoices, webapp2.abort, 300) 21 self.assertRaises(webob.exc.HTTPMovedPermanently, webapp2.abort, 301) 22 self.assertRaises(webob.exc.HTTPFound, webapp2.abort, 302) 23 self.assertRaises(webob.exc.HTTPSeeOther, webapp2.abort, 303) 24 self.assertRaises(webob.exc.HTTPNotModified, webapp2.abort, 304) 25 self.assertRaises(webob.exc.HTTPUseProxy, webapp2.abort, 305) 26 self.assertRaises(webob.exc.HTTPTemporaryRedirect, webapp2.abort, 307) 27 self.assertRaises(webob.exc.HTTPClientError, webapp2.abort, 400) 28 self.assertRaises(webob.exc.HTTPUnauthorized, webapp2.abort, 401) 29 self.assertRaises(webob.exc.HTTPPaymentRequired, webapp2.abort, 402) 30 self.assertRaises(webob.exc.HTTPForbidden, webapp2.abort, 403) 31 self.assertRaises(webob.exc.HTTPNotFound, webapp2.abort, 404) 32 self.assertRaises(webob.exc.HTTPMethodNotAllowed, webapp2.abort, 405) 33 self.assertRaises(webob.exc.HTTPNotAcceptable, webapp2.abort, 406) 34 self.assertRaises(webob.exc.HTTPProxyAuthenticationRequired, webapp2.abort, 407) 35 self.assertRaises(webob.exc.HTTPRequestTimeout, webapp2.abort, 408) 36 self.assertRaises(webob.exc.HTTPConflict, webapp2.abort, 409) 37 self.assertRaises(webob.exc.HTTPGone, webapp2.abort, 410) 38 self.assertRaises(webob.exc.HTTPLengthRequired, webapp2.abort, 411) 39 self.assertRaises(webob.exc.HTTPPreconditionFailed, webapp2.abort, 412) 40 self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, webapp2.abort, 413) 41 self.assertRaises(webob.exc.HTTPRequestURITooLong, webapp2.abort, 414) 42 self.assertRaises(webob.exc.HTTPUnsupportedMediaType, webapp2.abort, 415) 43 self.assertRaises(webob.exc.HTTPRequestRangeNotSatisfiable, webapp2.abort, 416) 44 self.assertRaises(webob.exc.HTTPExpectationFailed, webapp2.abort, 417) 45 self.assertRaises(webob.exc.HTTPInternalServerError, webapp2.abort, 500) 46 self.assertRaises(webob.exc.HTTPNotImplemented, webapp2.abort, 501) 47 self.assertRaises(webob.exc.HTTPBadGateway, webapp2.abort, 502) 48 self.assertRaises(webob.exc.HTTPServiceUnavailable, webapp2.abort, 503) 49 self.assertRaises(webob.exc.HTTPGatewayTimeout, webapp2.abort, 504) 50 self.assertRaises(webob.exc.HTTPVersionNotSupported, webapp2.abort, 505) 51 52 # Invalid use 500 as default. 53 self.assertRaises(KeyError, webapp2.abort, 0) 54 self.assertRaises(KeyError, webapp2.abort, 999999) 55 self.assertRaises(KeyError, webapp2.abort, 'foo') 56 57 def test_import_string(self): 58 self.assertEqual(webapp2.import_string('webob.exc'), webob.exc) 59 self.assertEqual(webapp2.import_string('webob'), webob) 60 61 self.assertEqual(webapp2.import_string('asdfg', silent=True), None) 62 self.assertEqual(webapp2.import_string('webob.asdfg', silent=True), None) 63 64 self.assertRaises(webapp2.ImportStringError, webapp2.import_string, 'asdfg') 65 self.assertRaises(webapp2.ImportStringError, webapp2.import_string, 'webob.asdfg') 66 67 def test_to_utf8(self): 68 res = webapp2._to_utf8('ábcdéf'.decode('utf-8')) 69 self.assertEqual(isinstance(res, str), True) 70 71 res = webapp2._to_utf8('abcdef') 72 self.assertEqual(isinstance(res, str), True) 73 74 ''' 75 # removed to simplify the codebase. 76 def test_to_unicode(self): 77 res = webapp2.to_unicode(unicode('foo')) 78 self.assertEqual(isinstance(res, unicode), True) 79 80 res = webapp2.to_unicode('foo') 81 self.assertEqual(isinstance(res, unicode), True) 82 ''' 83 84 def test_http_status_message(self): 85 self.assertEqual(webapp2.Response.http_status_message(404), 'Not Found') 86 self.assertEqual(webapp2.Response.http_status_message(500), 'Internal Server Error') 87 self.assertRaises(KeyError, webapp2.Response.http_status_message, 9999) 88 89 def test_cached_property(self): 90 count = [0] 91 92 class Foo(object): 93 @webapp2.cached_property 94 def bar(self): 95 count[0] += 1 96 return count[0] 97 98 self.assertTrue(isinstance(Foo.bar, webapp2.cached_property)) 99 100 foo = Foo() 101 self.assertEqual(foo.bar, 1) 102 self.assertEqual(foo.bar, 1) 103 self.assertEqual(foo.bar, 1) 104 105 def test_redirect(self): 106 app = webapp2.WSGIApplication() 107 req = webapp2.Request.blank('/') 108 req.app = app 109 app.set_globals(app=app, request=req) 110 rsp = webapp2.redirect('http://www.google.com/', code=301, body='Weee') 111 self.assertEqual(rsp.status_int, 301) 112 self.assertEqual(rsp.body, 'Weee') 113 self.assertEqual(rsp.headers.get('Location'), 'http://www.google.com/') 114 115 def test_redirect_to(self): 116 app = webapp2.WSGIApplication([ 117 webapp2.Route('/home', handler='', name='home'), 118 ]) 119 req = webapp2.Request.blank('/') 120 req.app = app 121 app.set_globals(app=app, request=req) 122 123 rsp = webapp2.redirect_to('home', _code=301, _body='Weee') 124 self.assertEqual(rsp.status_int, 301) 125 self.assertEqual(rsp.body, 'Weee') 126 self.assertEqual(rsp.headers.get('Location'), 'http://localhost/home') 127 128 129if __name__ == '__main__': 130 test_base.main() 131