1# -*- coding: utf-8 -*- 2from google.appengine.ext import webapp 3 4import webapp2 5 6import test_base 7 8 9# Old WSGIApplication, new RequestHandler. 10class NewStyleHandler(webapp2.RequestHandler): 11 def get(self, text): 12 self.response.out.write(text) 13 14app = webapp.WSGIApplication([ 15 (r'/test/(.*)', NewStyleHandler), 16]) 17 18# New WSGIApplication, old RequestHandler. 19class OldStyleHandler(webapp.RequestHandler): 20 def get(self, text): 21 self.response.out.write(text) 22 23class OldStyleHandler2(webapp.RequestHandler): 24 def get(self, text=None): 25 self.response.out.write(text) 26 27class OldStyleHandlerWithError(webapp.RequestHandler): 28 def get(self, text): 29 raise ValueError() 30 31 def handle_exception(self, e, debug): 32 self.response.set_status(500) 33 self.response.out.write('ValueError!') 34 35app2 = webapp2.WSGIApplication([ 36 (r'/test/error', OldStyleHandlerWithError), 37 (r'/test/(.*)', OldStyleHandler), 38 webapp2.Route(r'/test2/<text>', OldStyleHandler2), 39]) 40 41 42class TestWebapp1(test_base.BaseTestCase): 43 def test_old_app_new_handler(self): 44 req = webapp2.Request.blank('/test/foo') 45 rsp = req.get_response(app) 46 self.assertEqual(rsp.status_int, 200) 47 self.assertEqual(rsp.body, 'foo') 48 49 req = webapp2.Request.blank('/test/bar') 50 rsp = req.get_response(app) 51 self.assertEqual(rsp.status_int, 200) 52 self.assertEqual(rsp.body, 'bar') 53 54 self.assertTrue(issubclass(OldStyleHandler, webapp.RequestHandler)) 55 56 def test_new_app_old_handler(self): 57 req = webapp2.Request.blank('/test/foo') 58 rsp = req.get_response(app2) 59 self.assertEqual(rsp.status_int, 200) 60 self.assertEqual(rsp.body, 'foo') 61 62 req = webapp2.Request.blank('/test/bar') 63 rsp = req.get_response(app2) 64 self.assertEqual(rsp.status_int, 200) 65 self.assertEqual(rsp.body, 'bar') 66 67 def test_new_app_old_handler_405(self): 68 req = webapp2.Request.blank('/test/foo') 69 req.method = 'POST' 70 rsp = req.get_response(app2) 71 self.assertEqual(rsp.status_int, 405) 72 self.assertEqual(rsp.headers.get('Allow'), None) 73 74 def test_new_app_old_handler_501(self): 75 app2.allowed_methods = list(app2.allowed_methods) + ['NEW_METHOD'] 76 req = webapp2.Request.blank('/test/foo') 77 req.method = 'NEW_METHOD' 78 rsp = req.get_response(app2) 79 self.assertEqual(rsp.status_int, 501) 80 81 def test_new_app_old_handler_501_2(self): 82 req = webapp2.Request.blank('/test/foo') 83 req.method = 'WHATMETHODISTHIS' 84 rsp = req.get_response(app2) 85 self.assertEqual(rsp.status_int, 501) 86 87 def test_new_app_old_handler_with_error(self): 88 req = webapp2.Request.blank('/test/error') 89 rsp = req.get_response(app2) 90 self.assertEqual(rsp.status_int, 500) 91 self.assertEqual(rsp.body, 'ValueError!') 92 93 def test_new_app_old_kwargs(self): 94 req = webapp2.Request.blank('/test2/foo') 95 rsp = req.get_response(app2) 96 self.assertEqual(rsp.status_int, 200) 97 self.assertEqual(rsp.body, 'foo') 98 99 def test_unicode_cookie(self): 100 # see http://stackoverflow.com/questions/6839922/unicodedecodeerror-is-raised-when-getting-a-cookie-in-google-app-engine 101 import urllib 102 103 # This is the value we want to set. 104 initial_value = u'äëïöü' 105 # WebOb version that comes with SDK doesn't quote cookie values. 106 # So we have to do it. 107 quoted_value = urllib.quote(initial_value.encode('utf-8')) 108 109 rsp = webapp.Response() 110 rsp.headers['Set-Cookie'] = 'app=%s; Path=/' % quoted_value 111 112 cookie = rsp.headers.get('Set-Cookie') 113 req = webapp.Request.blank('/', headers=[('Cookie', cookie)]) 114 115 # The stored value is the same quoted value from before. 116 # Notice that here we use .str_cookies, not .cookies. 117 stored_value = req.str_cookies.get('app') 118 self.assertEqual(stored_value, quoted_value) 119 120 # And we can get the initial value unquoting and decoding. 121 final_value = urllib.unquote(stored_value).decode('utf-8') 122 self.assertEqual(final_value, initial_value) 123 124 125if __name__ == '__main__': 126 test_base.main() 127