def test_iri_to_uri_is_sane_for_complex_url(self): # test without the added complexity of unicode characters just # to sanity check that a complex URL is not mangled unicode_string = (u"http://*****:*****@www.host.com/with/path/and" u"?query=string&is=sane") encoded_string = url_util.iri_to_uri(unicode_string) self.assertIsInstance(encoded_string, str) self.assertEqual(unicode_string, encoded_string)
def test_iri_to_uri_encodes_to_utf8(self): original_unicode = u"/\u00fe/\u00f4?\u00ef=\u2021" # verify correct encoding encoded_string = url_util.iri_to_uri(original_unicode) self.assertIsInstance(encoded_string, str) self.assertEqual("/%C3%BE/%C3%B4?%C3%AF=%E2%80%A1", encoded_string) # verify that we can roundtrip back to the original unicode decoded_string = urllib.unquote(encoded_string).decode("utf-8") self.assertIsInstance(decoded_string, unicode) self.assertEqual(original_unicode, decoded_string)
def redirect(self, uri, *args, **kwargs): """Override to handle locations with non-ASCII unicode characters. A URI containing non-ASCII characters is known as an IRI, or Internationalized Resource Identifier. This override conveniently converts IRIs to URIs for use in the HTTP Location header. See http://www.ietf.org/rfc/rfc3987.txt webapp2.RequestHandler has redirect() and redirect_to(). Since redirect_to() calls redirect(), this override handles both cases. """ uri = url_util.iri_to_uri(uri) super(RequestHandler, self).redirect(uri, *args, **kwargs)
def test_iri_to_uri_is_idempotent(self): unicode_string = u"/\u00fe/\u00f4?\u00ef=\u2021" self.assertEqual( url_util.iri_to_uri(unicode_string), url_util.iri_to_uri(url_util.iri_to_uri(unicode_string)))