def test_json_decode(self): # json_decode accepts both bytes and unicode, but strings it returns # are always unicode. self.assertEqual(json_decode(b('"foo"')), u"foo") self.assertEqual(json_decode(u'"foo"'), u"foo") # Non-ascii bytes are interpreted as utf8 self.assertEqual(json_decode(utf8(u'"\u00e9"')), u"\u00e9")
def test_types(self): headers = {"Cookie": "foo=bar"} response = self.fetch("/typecheck?foo=bar", headers=headers) data = json_decode(response.body) self.assertEqual(data, {}) response = self.fetch("/typecheck", method="POST", body="foo=bar", headers=headers) data = json_decode(response.body) self.assertEqual(data, {})
def test_openid_get_user(self): response = self.fetch( '/openid/client/login?openid.mode=blah&openid.ns.ax=http://openid.net/srv/ax/1.0&openid.ax.type.email=http://axschema.org/contact/email&[email protected]' ) response.rethrow() parsed = json_decode(response.body) self.assertEqual(parsed["email"], "*****@*****.**")
def test_multipart_form(self): # Encodings here are tricky: Headers are latin1, bodies can be # anything (we use utf8 by default). response = self.raw_fetch( [ b("POST /multipart HTTP/1.0"), b("Content-Type: multipart/form-data; boundary=1234567890"), b("X-Header-encoding-test: \xe9"), ], b("\r\n").join([ b("Content-Disposition: form-data; name=argument"), b(""), u"\u00e1".encode("utf-8"), b("--1234567890"), u'Content-Disposition: form-data; name="files"; filename="\u00f3"' .encode("utf8"), b(""), u"\u00fa".encode("utf-8"), b("--1234567890--"), b(""), ])) data = json_decode(response.body) self.assertEqual(u"\u00e9", data["header"]) self.assertEqual(u"\u00e1", data["argument"]) self.assertEqual(u"\u00f3", data["filename"]) self.assertEqual(u"\u00fa", data["filebody"])
def _on_friendfeed_request(self, callback, response): if response.error: logging.warning("Error response %s fetching %s", response.error, response.request.url) callback(None) return callback(escape.json_decode(response.body))
def test_oauth10a_request_parameters(self): response = self.fetch('/oauth10a/client/request_params') response.rethrow() parsed = json_decode(response.body) self.assertEqual(parsed['oauth_consumer_key'], 'asdf') self.assertEqual(parsed['oauth_token'], 'uiop') self.assertTrue('oauth_nonce' in parsed) self.assertTrue('oauth_signature' in parsed)
def test_path_encoding(self): # Path components and query arguments should be decoded the same way self.assertEqual(json_decode(self.fetch('/%C3%A9?arg=%C3%A9').body), { u"path": u"\u00e9", u"args": { u"arg": [u"\u00e9"] } })
def test_oauth10a_get_user(self): response = self.fetch( '/oauth10a/client/login?oauth_token=zxcv', headers={'Cookie':'_oauth_request_token=enhjdg==|MTIzNA=='}) response.rethrow() parsed = json_decode(response.body) self.assertEqual(parsed['email'], '*****@*****.**') self.assertEqual(parsed['access_token'], dict(key='uiop', secret='5678'))
def test_types(self): response = self.fetch("/typecheck/asdf?foo=bar", headers={"Cookie": "cook=ie"}) data = json_decode(response.body) self.assertEqual(data, {}) response = self.fetch("/typecheck/asdf?foo=bar", method="POST", headers={"Cookie": "cook=ie"}, body="foo=bar")
def test_oauth10a_get_user(self): response = self.fetch( '/oauth10a/client/login?oauth_token=zxcv', headers={'Cookie': '_oauth_request_token=enhjdg==|MTIzNA=='}) response.rethrow() parsed = json_decode(response.body) self.assertEqual(parsed['email'], '*****@*****.**') self.assertEqual(parsed['access_token'], dict(key='uiop', secret='5678'))
def test_decode_argument(self): # These urls all decode to the same thing urls = ["/decode_arg/%C3%A9?foo=%C3%A9&encoding=utf-8", "/decode_arg/%E9?foo=%E9&encoding=latin1", "/decode_arg_kw/%E9?foo=%E9&encoding=latin1", ] for url in urls: response = self.fetch(url) response.rethrow() data = json_decode(response.body) self.assertEqual(data, {u'path': [u'unicode', u'\u00e9'], u'query': [u'unicode', u'\u00e9'], }) response = self.fetch("/decode_arg/%C3%A9?foo=%C3%A9") response.rethrow() data = json_decode(response.body) self.assertEqual(data, {u'path': [u'bytes', u'c3a9'], u'query': [u'bytes', u'c3a9'], })
def test_decode_argument(self): # These urls all decode to the same thing urls = [ "/decode_arg/%C3%A9?foo=%C3%A9&encoding=utf-8", "/decode_arg/%E9?foo=%E9&encoding=latin1", "/decode_arg_kw/%E9?foo=%E9&encoding=latin1", ] for url in urls: response = self.fetch(url) response.rethrow() data = json_decode(response.body) self.assertEqual( data, { u'path': [u'unicode', u'\u00e9'], u'query': [u'unicode', u'\u00e9'], }) response = self.fetch("/decode_arg/%C3%A9?foo=%C3%A9") response.rethrow() data = json_decode(response.body) self.assertEqual(data, { u'path': [u'bytes', u'c3a9'], u'query': [u'bytes', u'c3a9'], })
def get_authenticated_user(self, callback): """Fetches the authenticated Facebook user. The authenticated user includes the special Facebook attributes 'session_key' and 'facebook_uid' in addition to the standard user attributes like 'name'. """ self.require_setting("facebook_api_key", "Facebook Connect") session = escape.json_decode(self.get_argument("session")) self.facebook_request( method="facebook.users.getInfo", callback=self.async_callback( self._on_get_user_info, callback, session), session_key=session["session_key"], uids=session["uid"], fields="uid,first_name,last_name,name,locale,pic_square," \ "profile_url,username")
def _parse_response(self, callback, response): if response.error: logging.warning("HTTP error from Facebook: %s", response.error) callback(None) return try: json = escape.json_decode(response.body) except Exception: logging.warning("Invalid JSON from Facebook: %r", response.body) callback(None) return if isinstance(json, dict) and json.get("error_code"): logging.warning("Facebook error: %d: %r", json["error_code"], json.get("error_msg")) callback(None) return callback(json)
def test_multipart_form(self): # Encodings here are tricky: Headers are latin1, bodies can be # anything (we use utf8 by default). response = self.raw_fetch([ b("POST /multipart HTTP/1.0"), b("Content-Type: multipart/form-data; boundary=1234567890"), b("X-Header-encoding-test: \xe9"), ], b("\r\n").join([ b("Content-Disposition: form-data; name=argument"), b(""), u"\u00e1".encode("utf-8"), b("--1234567890"), u'Content-Disposition: form-data; name="files"; filename="\u00f3"'.encode("utf8"), b(""), u"\u00fa".encode("utf-8"), b("--1234567890--"), b(""), ])) data = json_decode(response.body) self.assertEqual(u"\u00e9", data["header"]) self.assertEqual(u"\u00e1", data["argument"]) self.assertEqual(u"\u00f3", data["filename"]) self.assertEqual(u"\u00fa", data["filebody"])
def test_query_string_encoding(self): response = self.fetch("/echo?foo=%C3%A9") data = json_decode(response.body) self.assertEqual(data, {u"foo": [u"\u00e9"]})
def test_json_encode(self): # json deals with strings, not bytes, but our encoding function should # accept bytes as well as long as they are utf8. self.assertEqual(json_decode(json_encode(u"\u00e9")), u"\u00e9") self.assertEqual(json_decode(json_encode(utf8(u"\u00e9"))), u"\u00e9") self.assertRaises(UnicodeDecodeError, json_encode, b("\xe9"))
def fetch_json(self, *args, **kwargs): response = self.fetch(*args, **kwargs) response.rethrow() return json_decode(response.body)
def test_path_encoding(self): # Path components and query arguments should be decoded the same way self.assertEqual(json_decode(self.fetch('/%C3%A9?arg=%C3%A9').body), {u"path":u"\u00e9", u"args": {u"arg": [u"\u00e9"]}})
def test_question_mark(self): # Ensure that url-encoded question marks are handled properly self.assertEqual(json_decode(self.fetch('/%3F').body), dict(path='?', args={})) self.assertEqual(json_decode(self.fetch('/%3F?%3F=%3F').body), dict(path='?', args={'?': ['?']}))
def get_current_user(self): user_json = self.get_secure_cookie("user") if not user_json: return None return escape.json_decode(user_json)
def test_openid_get_user(self): response = self.fetch('/openid/client/login?openid.mode=blah&openid.ns.ax=http://openid.net/srv/ax/1.0&openid.ax.type.email=http://axschema.org/contact/email&[email protected]') response.rethrow() parsed = json_decode(response.body) self.assertEqual(parsed["email"], "*****@*****.**")