def test_body_encoding(self):
        unicode_body = u("\xe9")
        byte_body = binascii.a2b_hex(b"e9")

        # unicode string in body gets converted to utf8
        response = self.fetch(
            "/echopost", method="POST", body=unicode_body, headers={"Content-Type": "application/blah"}
        )
        self.assertEqual(response.headers["Content-Length"], "2")
        self.assertEqual(response.body, utf8(unicode_body))

        # byte strings pass through directly
        response = self.fetch("/echopost", method="POST", body=byte_body, headers={"Content-Type": "application/blah"})
        self.assertEqual(response.headers["Content-Length"], "1")
        self.assertEqual(response.body, byte_body)

        # Mixing unicode in headers and byte string bodies shouldn't
        # break anything
        response = self.fetch(
            "/echopost",
            method="POST",
            body=byte_body,
            headers={"Content-Type": "application/blah"},
            user_agent=u("foo"),
        )
        self.assertEqual(response.headers["Content-Length"], "1")
        self.assertEqual(response.body, byte_body)
Exemple #2
0
    def setUp(self):

        self.formatter = LogFormatter(color=False)

        # Fake color support.  
# We can't guarantee anything about the $TERM
        # variable when the tests are run, so just patch in some values
        # for testing.  (testing with color off fails to expose some potential
        # encoding issues from the control characters)
        self.formatter._colors = {
            logging.ERROR: u("\u0001"),
        }

        self.formatter._normal = u("\u0002")

        # construct a Logger directly to bypass getLogger's caching
        # 定义一个logger,带上标识,标识唯一的logger对象
        #self.logger = logging.Logger('LogFormatterTest')
        self.logger = logging.Logger('test')

        self.logger.propagate = False

        self.tempdir = tempfile.mkdtemp()
        print(self.tempdir)

        # self.filename = os.path.join(self.tempdir, 'log.out')
        self.filename = './log.out'
        print(self.filename)

        self.handler = self.make_handler(self.filename)

        self.handler.setFormatter(self.formatter)

        # 添加日志处理器
        self.logger.addHandler(self.handler)
Exemple #3
0
	def _on_authentication_verified(self, future, response):
		if response.error or b"is_valid:true" not in response.body:
			future.set_exception(AuthError(
				"Invalid OpenID response: %s" % (response.error or
												 response.body)))
			return

		# Make sure we got back at least an email from attribute exchange
		ax_ns = None
		for name in self.request.arguments:
			if name.startswith("openid.ns.") and \
					self.get_argument(name) == u("http://openid.net/srv/ax/1.0"):
				ax_ns = name[10:]
				break

		def get_ax_arg(uri):
			if not ax_ns:
				return u("")
			prefix = "openid." + ax_ns + ".type."
			ax_name = None
			for name in self.request.arguments.keys():
				if self.get_argument(name) == uri and name.startswith(prefix):
					part = name[len(prefix):]
					ax_name = "openid." + ax_ns + ".value." + part
					break
			if not ax_name:
				return u("")
			return self.get_argument(ax_name, u(""))

		email = get_ax_arg("http://axschema.org/contact/email")
		name = get_ax_arg("http://axschema.org/namePerson")
		first_name = get_ax_arg("http://axschema.org/namePerson/first")
		last_name = get_ax_arg("http://axschema.org/namePerson/last")
		username = get_ax_arg("http://axschema.org/namePerson/friendly")
		locale = get_ax_arg("http://axschema.org/pref/language").lower()
		user = dict()
		name_parts = []
		if first_name:
			user["first_name"] = first_name
			name_parts.append(first_name)
		if last_name:
			user["last_name"] = last_name
			name_parts.append(last_name)
		if name:
			user["name"] = name
		elif name_parts:
			user["name"] = u(" ").join(name_parts)
		elif email:
			user["name"] = email.split("@")[0]
		if email:
			user["email"] = email
		if locale:
			user["locale"] = locale
		if username:
			user["username"] = username
		claimed_id = self.get_argument("openid.claimed_id", None)
		if claimed_id:
			user["claimed_id"] = claimed_id
		future.set_result(user)
Exemple #4
0
 def test_json_encode(self):
     # json deals with strings, not bytes.  On python 2 byte strings will
     # convert automatically if they are utf8; on python 3 byte strings
     # are not allowed.
     self.assertEqual(json_decode(json_encode(u("\u00e9"))), u("\u00e9"))
     if bytes is str:
         self.assertEqual(json_decode(json_encode(utf8(u("\u00e9")))), u("\u00e9"))
         self.assertRaises(UnicodeDecodeError, json_encode, b"\xe9")
Exemple #5
0
    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_unicode_message(self):
     ws = yield websocket_connect(
         'ws://localhost:%d/echo' % self.get_http_port())
     ws.write_message(u('hello \u00e9'))
     response = yield ws.read_message()
     self.assertEqual(response, u('hello \u00e9'))
     ws.close()
     yield self.close_future
Exemple #7
0
def _make_text_block(name, content, content_type=None):
    """Helper function for the builder that creates an XML text block."""
    if content_type == 'xhtml':
        return u('<%s type="xhtml"><div xmlns="%s">%s</div></%s>\n') % \
               (name, XHTML_NAMESPACE, content, name)
    if not content_type:
        return u('<%s>%s</%s>\n') % (name, escape(content), name)
    return u('<%s type="%s">%s</%s>\n') % (name, content_type,
                                         escape(content), name)
Exemple #8
0
    def test_url_escape_unicode(self):
        tests = [
            # byte strings are passed through as-is
            (u('\u00e9').encode('utf8'), '%C3%A9'),
            (u('\u00e9').encode('latin1'), '%E9'),

            # unicode strings become utf8
            (u('\u00e9'), '%C3%A9'),
        ]
        for unescaped, escaped in tests:
            self.assertEqual(url_escape(unescaped), escaped)
Exemple #9
0
 def test_utf8_logging(self):
     self.logger.error(u("\u00e9").encode("utf8"))
     if issubclass(bytes_type, basestring_type):
         # on python 2, utf8 byte strings (and by extension ascii byte
         # strings) are passed through as-is.
         self.assertEqual(self.get_output(), utf8(u("\u00e9")))
     else:
         # on python 3, byte strings always get repr'd even if
         # they're ascii-only, so this degenerates into another
         # copy of test_bytes_logging.
         self.assertEqual(self.get_output(), utf8(repr(utf8(u("\u00e9")))))
Exemple #10
0
    def test_xhtml_escape(self):
        tests = [
            ("<foo>", "&lt;foo&gt;"),
            (u("<foo>"), u("&lt;foo&gt;")),
            (b"<foo>", b"&lt;foo&gt;"),

            ("<>&\"", "&lt;&gt;&amp;&quot;"),
            ("&amp;", "&amp;amp;"),
        ]
        for unescaped, escaped in tests:
            self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped))
            self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))
Exemple #11
0
 def test_url_unescape_unicode(self):
     tests = [
         ('%C3%A9', u('\u00e9'), 'utf8'),
         ('%C3%A9', u('\u00c3\u00a9'), 'latin1'),
         ('%C3%A9', utf8(u('\u00e9')), None),
     ]
     for escaped, unescaped, encoding in tests:
         # input strings to url_unescape should only contain ascii
         # characters, but make sure the function accepts both byte
         # and unicode strings.
         self.assertEqual(url_unescape(to_unicode(escaped), encoding), unescaped)
         self.assertEqual(url_unescape(utf8(escaped), encoding), unescaped)
 def test_unicode_literal_expression(self):
     # Unicode literals should be usable in templates.  Note that this
     # test simulates unicode characters appearing directly in the
     # template file (with utf8 encoding), i.e. \u escapes would not
     # be used in the template file itself.
     if str is unicode_type:
         # python 3 needs a different version of this test since
         # 2to3 doesn't run on template internals
         template = Template(utf8(u('{{ "\u00e9" }}')))
     else:
         template = Template(utf8(u('{{ u"\u00e9" }}')))
     self.assertEqual(template.generate(), utf8(u("\u00e9")))
Exemple #13
0
		def get_ax_arg(uri):
			if not ax_ns:
				return u("")
			prefix = "openid." + ax_ns + ".type."
			ax_name = None
			for name in self.request.arguments.keys():
				if self.get_argument(name) == uri and name.startswith(prefix):
					part = name[len(prefix):]
					ax_name = "openid." + ax_ns + ".value." + part
					break
			if not ax_name:
				return u("")
			return self.get_argument(ax_name, u(""))
Exemple #14
0
    def list(self, parts):
        """返回给定列表的一个由逗号分隔的部分.

        格式是, e.g., "A, B and C", "A and B" 或者"A"当列表长度为1.
        """
        _ = self.translate
        if len(parts) == 0:
            return ""
        if len(parts) == 1:
            return parts[0]
        comma = u(' \u0648 ') if self.code.startswith("fa") else u(", ")
        return _("%(commas)s and %(last)s") % {
            "commas": comma.join(parts[:-1]),
            "last": parts[len(parts) - 1],
        }
Exemple #15
0
    def get_handlers(self):
        class EchoHandler(RequestHandler):
            def get(self, path):
                self.write(path)

        return [("/str/(?P<path>.*)", EchoHandler),
                (u("/unicode/(?P<path>.*)"), EchoHandler)]
 def test_gettext(self):
     tornado.locale.load_gettext_translations(
         os.path.join(os.path.dirname(__file__), 'gettext_translations'),
         "tornado_test")
     locale = tornado.locale.get("fr_FR")
     self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
     self.assertEqual(locale.translate("school"), u("\u00e9cole"))
Exemple #17
0
    def list(self, parts):
        """Returns a comma-separated list for the given list of parts.

        The format is, e.g., "A, B and C", "A and B" or just "A" for lists
        of size 1.
        """
        _ = self.translate
        if len(parts) == 0:
            return ""
        if len(parts) == 1:
            return parts[0]
        comma = u(' \u0648 ') if self.code.startswith("fa") else u(", ")
        return _("%(commas)s and %(last)s") % {
            "commas": comma.join(parts[:-1]),
            "last": parts[len(parts) - 1],
        }
Exemple #18
0
 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)
     self.assertEqual(u("\u00e9"), data["header"])
     self.assertEqual(u("\u00e1"), data["argument"])
     self.assertEqual(u("\u00f3"), data["filename"])
     self.assertEqual(u("\u00fa"), data["filebody"])
Exemple #19
0
 def test_reverse_url(self):
     self.assertEqual(self.app.reverse_url('decode_arg', 'foo'),
                      '/decode_arg/foo')
     self.assertEqual(self.app.reverse_url('decode_arg', 42),
                      '/decode_arg/42')
     self.assertEqual(self.app.reverse_url('decode_arg', b'\xe9'),
                      '/decode_arg/%E9')
     self.assertEqual(self.app.reverse_url('decode_arg', u('\u00e9')),
                      '/decode_arg/%C3%A9')
Exemple #20
0
    def test_chunked_request_body(self):
        # Chunked requests are not widely supported and we don't have a way
        # to generate them in AsyncHTTPClient, but HTTPServer will read them.
        self.stream.write(b"""\
POST /echo HTTP/1.1
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded

4
foo=
3
bar
0

""".replace(b"\n", b"\r\n"))
        read_stream_body(self.stream, self.stop)
        headers, response = self.wait()
        self.assertEqual(json_decode(response), {u('foo'): [u('bar')]})
Exemple #21
0
    def make_link(m):
        url = m.group(1)
        proto = m.group(2)
        if require_protocol and not proto:
            return url  # not protocol, no linkify

        if proto and proto not in permitted_protocols:
            return url  # bad protocol, no linkify

        href = m.group(1)
        if not proto:
            href = "http://" + href   # no proto specified, use http

        if callable(extra_params):
            params = " " + extra_params(href).strip()
        else:
            params = extra_params

        # clip long urls. max_len is just an approximation
        max_len = 30
        if shorten and len(url) > max_len:
            before_clip = url
            if proto:
                proto_len = len(proto) + 1 + len(m.group(3) or "")  # +1 for :
            else:
                proto_len = 0

            parts = url[proto_len:].split("/")
            if len(parts) > 1:
                # Grab the whole host part plus the first bit of the path
                # The path is usually not that interesting once shortened
                # (no more slug, etc), so it really just provides a little
                # extra indication of shortening.
                url = url[:proto_len] + parts[0] + "/" + \
                    parts[1][:8].split('?')[0].split('.')[0]

            if len(url) > max_len * 1.5:  # still too long
                url = url[:max_len]

            if url != before_clip:
                amp = url.rfind('&')
                # avoid splitting html char entities
                if amp > max_len - 5:
                    url = url[:amp]
                url += "..."

                if len(url) >= len(before_clip):
                    url = before_clip
                else:
                    # full url is visible on mouse-over (for those who don't
                    # have a status bar, such as Safari by default)
                    params += ' title="%s"' % href

        return u('<a href="%s"%s>%s</a>') % (href, params, url)
Exemple #22
0
 def test_xhtml_unescape_numeric(self):
     tests = [
         ('foo&#32;bar', 'foo bar'),
         ('foo&#x20;bar', 'foo bar'),
         ('foo&#X20;bar', 'foo bar'),
         ('foo&#xabc;bar', u('foo\u0abcbar')),
         ('foo&#xyz;bar', 'foo&#xyz;bar'),  # invalid encoding
         ('foo&#;bar', 'foo&#;bar'),        # invalid encoding
         ('foo&#x;bar', 'foo&#x;bar'),      # invalid encoding
     ]
     for escaped, unescaped in tests:
         self.assertEqual(unescaped, xhtml_unescape(escaped))
Exemple #23
0
 def test_google_login(self):
     response = self.fetch('/client/login')
     self.assertDictEqual({
         u('name'): u('Foo'),
         u('email'): u('*****@*****.**'),
         u('access_token'): u('fake-access-token'),
     }, json_decode(response.body))
Exemple #24
0
 def test_header_types(self):
     # Header values may be passed as character or utf8 byte strings,
     # in a plain dictionary or an HTTPHeaders object.
     # Keys must always be the native str type.
     # All combinations should have the same results on the wire.
     for value in [u("MyUserAgent"), b"MyUserAgent"]:
         for container in [dict, HTTPHeaders]:
             headers = container()
             headers["User-Agent"] = value
             resp = self.fetch("/user_agent", headers=headers)
             self.assertEqual(
                 resp.body, b"MyUserAgent", "response=%r, value=%r, container=%r" % (resp.body, value, container)
             )
Exemple #25
0
 def test_recursive_unicode(self):
     tests = {
         'dict': {b"foo": b"bar"},
         'list': [b"foo", b"bar"],
         'tuple': (b"foo", b"bar"),
         'bytes': b"foo"
     }
     self.assertEqual(recursive_unicode(tests['dict']), {u("foo"): u("bar")})
     self.assertEqual(recursive_unicode(tests['list']), [u("foo"), u("bar")])
     self.assertEqual(recursive_unicode(tests['tuple']), (u("foo"), u("bar")))
     self.assertEqual(recursive_unicode(tests['bytes']), u("foo"))
Exemple #26
0
def escape(s, quote=None):
    """Replace special characters "&", "<", ">" and (") to HTML-safe sequences.

    There is a special handling for `None` which escapes to an empty string.

    .. versionchanged:: 0.9
       `quote` is now implicitly on.

    :param s: the string to escape.
    :param quote: ignored.
    """
    if s is None:
        return ''
    elif hasattr(s, '__html__'):
        return u(s.__html__())
    elif not isinstance(s, string_types):
        s = u(s)
    if quote is not None:
        from warnings import warn
        warn(DeprecationWarning('quote parameter is implicit now'), stacklevel=2)
    s = s.replace('&', '&amp;').replace('<', '&lt;') \
        .replace('>', '&gt;').replace('"', "&quot;")
    return s
Exemple #27
0
 def test_gettext(self):
     tornado.locale.load_gettext_translations(
         os.path.join(os.path.dirname(__file__), 'gettext_translations'),
         "tornado_test")
     locale = tornado.locale.get("fr_FR")
     self.assertTrue(isinstance(locale, tornado.locale.GettextLocale))
     self.assertEqual(locale.translate("school"), u("\u00e9cole"))
     self.assertEqual(locale.pgettext("law", "right"), u("le droit"))
     self.assertEqual(locale.pgettext("good", "right"), u("le bien"))
     self.assertEqual(locale.pgettext("organization", "club", "clubs", 1), u("le club"))
     self.assertEqual(locale.pgettext("organization", "club", "clubs", 2), u("les clubs"))
     self.assertEqual(locale.pgettext("stick", "club", "clubs", 1), u("le b\xe2ton"))
     self.assertEqual(locale.pgettext("stick", "club", "clubs", 2), u("les b\xe2tons"))
Exemple #28
0
    def get_authenticated_user(self, callback, http_client=None):
        """Fetches the authenticated user data upon redirect.

        This method should be called by the handler that receives the
        redirect from the authenticate_redirect() or authorize_redirect()
        methods.
        """
        # Verify the OpenID response via direct request to the OP
        args = dict((k, v[-1]) for k, v in self.request.arguments.items())
        args["openid.mode"] = u("check_authentication")
        url = self._OPENID_ENDPOINT
        if http_client is None:
            http_client = self.get_auth_http_client()
        http_client.fetch(url, self.async_callback(
            self._on_authentication_verified, callback),
            method="POST", body=urllib_parse.urlencode(args))
Exemple #29
0
 def test_decode_argument_plus(self):
     # These urls are all equivalent.
     urls = ["/decode_arg/1%20%2B%201?foo=1%20%2B%201&encoding=utf-8",
             "/decode_arg/1%20+%201?foo=1+%2B+1&encoding=utf-8"]
     for url in urls:
         response = self.fetch(url)
         response.rethrow()
         data = json_decode(response.body)
         self.assertEqual(data, {u('path'): [u('unicode'), u('1 + 1')],
                                 u('query'): [u('unicode'), u('1 + 1')],
                                 })
Exemple #30
0
 def get_authenticated_user(self, callback):
     """Fetches the authenticated user data upon redirect."""
     # Look to see if we are doing combined OpenID/OAuth
     oauth_ns = ""
     for name, values in self.request.arguments.iteritems():
         if name.startswith("openid.ns.") and \
                 values[-1] == u("http://specs.openid.net/extensions/oauth/1.0"):
             oauth_ns = name[10:]
             break
     token = self.get_argument("openid." + oauth_ns + ".request_token", "")
     if token:
         http = self.get_auth_http_client()
         token = dict(key=token, secret="")
         http.fetch(self._oauth_access_token_url(token),
                    self.async_callback(self._on_access_token, callback))
     else:
         OpenIdMixin.get_authenticated_user(self, callback)
Exemple #31
0
 def test_unicode_message(self):
     ws = yield self.ws_connect('/echo')
     ws.write_message(u('hello \u00e9'))
     response = yield ws.read_message()
     self.assertEqual(response, u('hello \u00e9'))
     yield self.close(ws)
Exemple #32
0
 def test_group_encoding(self):
     # Path components and query arguments should be decoded the same way
     self.assertEqual(self.fetch_json('/group/%C3%A9?arg=%C3%A9'),
                      {u("path"): u("/group/%C3%A9"),
                       u("path_args"): [u("\u00e9")],
                       u("args"): {u("arg"): [u("\u00e9")]}})
Exemple #33
0
 def test_squeeze(self):
     self.assertEqual(squeeze(u('sequences     of    whitespace   chars')),
                      u('sequences of whitespace chars'))
Exemple #34
0
 def test_import_member_unicode(self):
     self.assertIs(import_object(u('tornado.escape.utf8')), utf8)
 def test_path_quoting(self):
     response = self.fetch("/path/foo%20bar%C3%A9")
     self.assertEqual(response.body, u("foo bar\u00e9").encode("utf-8"))
Exemple #36
0
    def format_date(self, date, gmt_offset=0, relative=True, shorter=False,
                    full_format=False):
        """Formats the given date (which should be GMT).

        By default, we return a relative time (e.g., "2 minutes ago"). You
        can return an absolute date string with ``relative=False``.

        You can force a full format date ("July 10, 1980") with
        ``full_format=True``.

        This method is primarily intended for dates in the past.
        For dates in the future, we fall back to full format.
        """
        if isinstance(date, numbers.Real):
            date = datetime.datetime.utcfromtimestamp(date)
        now = datetime.datetime.utcnow()
        if date > now:
            if relative and (date - now).seconds < 60:
                # Due to click skew, things are some things slightly
                # in the future. Round timestamps in the immediate
                # future down to now in relative mode.
                date = now
            else:
                # Otherwise, future dates always use the full format.
                full_format = True
        local_date = date - datetime.timedelta(minutes=gmt_offset)
        local_now = now - datetime.timedelta(minutes=gmt_offset)
        local_yesterday = local_now - datetime.timedelta(hours=24)
        difference = now - date
        seconds = difference.seconds
        days = difference.days

        _ = self.translate
        format = None
        if not full_format:
            if relative and days == 0:
                if seconds < 50:
                    return _("1 second ago", "%(seconds)d seconds ago",
                             seconds) % {"seconds": seconds}

                if seconds < 50 * 60:
                    minutes = round(seconds / 60.0)
                    return _("1 minute ago", "%(minutes)d minutes ago",
                             minutes) % {"minutes": minutes}

                hours = round(seconds / (60.0 * 60))
                return _("1 hour ago", "%(hours)d hours ago",
                         hours) % {"hours": hours}

            if days == 0:
                format = _("%(time)s")
            elif days == 1 and local_date.day == local_yesterday.day and \
                    relative:
                format = _("yesterday") if shorter else \
                    _("yesterday at %(time)s")
            elif days < 5:
                format = _("%(weekday)s") if shorter else \
                    _("%(weekday)s at %(time)s")
            elif days < 334:  # 11mo, since confusing for same month last year
                format = _("%(month_name)s %(day)s") if shorter else \
                    _("%(month_name)s %(day)s at %(time)s")

        if format is None:
            format = _("%(month_name)s %(day)s, %(year)s") if shorter else \
                _("%(month_name)s %(day)s, %(year)s at %(time)s")

        tfhour_clock = self.code not in ("en", "en_US", "zh_CN")
        if tfhour_clock:
            str_time = "%d:%02d" % (local_date.hour, local_date.minute)
        elif self.code == "zh_CN":
            str_time = "%s%d:%02d" % (
                (u('\u4e0a\u5348'), u('\u4e0b\u5348'))[local_date.hour >= 12],
                local_date.hour % 12 or 12, local_date.minute)
        else:
            str_time = "%d:%02d %s" % (
                local_date.hour % 12 or 12, local_date.minute,
                ("am", "pm")[local_date.hour >= 12])

        return format % {
            "month_name": self._months[local_date.month - 1],
            "weekday": self._weekdays[local_date.weekday()],
            "day": str(local_date.day),
            "year": str(local_date.year),
            "time": str_time
        }
Exemple #37
0
            self.gettext = translations.gettext
        # self.gettext must exist before __init__ is called, since it
        # calls into self.translate
        super(GettextLocale, self).__init__(code, translations)

    def translate(self, message, plural_message=None, count=None):
        if plural_message is not None:
            assert count is not None
            return self.ngettext(message, plural_message, count)
        else:
            return self.gettext(message)


LOCALE_NAMES = {
    "af_ZA": {
        "name_en": u("Afrikaans"),
        "name": u("Afrikaans")
    },
    "am_ET": {
        "name_en": u("Amharic"),
        "name": u('\u12a0\u121b\u122d\u129b')
    },
    "ar_AR": {
        "name_en": u("Arabic"),
        "name": u("\u0627\u0644\u0639\u0631\u0628\u064a\u0629")
    },
    "bg_BG": {
        "name_en": u("Bulgarian"),
        "name": u("\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438")
    },
    "bn_IN": {
Exemple #38
0
 def test_unicode_logging(self):
     self.logger.error(u("\u00e9"))
     self.assertEqual(self.get_output(), utf8(u("\u00e9")))
Exemple #39
0
 def test_csv(self):
     tornado.locale.load_translations(
         os.path.join(os.path.dirname(__file__), 'csv_translations'))
     locale = tornado.locale.get("fr_FR")
     self.assertTrue(isinstance(locale, tornado.locale.CSVLocale))
     self.assertEqual(locale.translate("school"), u("\u00e9cole"))
Exemple #40
0
    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')],
            })
Exemple #41
0
 def test_optional_path(self):
     self.assertEqual(self.fetch_json("/optional_path/foo"),
                      {u("path"): u("foo")})
     self.assertEqual(self.fetch_json("/optional_path/"), {u("path"): None})
Exemple #42
0
 def get(self):
     # Try setting cookies with different argument types
     # to ensure that everything gets encoded correctly
     self.set_cookie("str", "asdf")
     self.set_cookie("unicode", u("qwer"))
     self.set_cookie("bytes", b"zxcv")
Exemple #43
0
 def test_unicode_template(self):
     template = Template(utf8(u("\u00e9")))
     self.assertEqual(template.generate(), utf8(u("\u00e9")))
Exemple #44
0
 def test_utf8_in_file(self):
     tmpl = self.loader.load("utf8.html")
     result = tmpl.generate()
     self.assertEqual(to_unicode(result).strip(), u("H\u00e9llo"))
Exemple #45
0
 def test_uncompressed(self):
     response = self.fetch('/', method='POST', body='foo=bar')
     self.assertEquals(json_decode(response.body), {u('foo'): [u('bar')]})
Exemple #46
0
 def test_non_ascii_name(self):
     name = tornado.locale.LOCALE_NAMES['es_LA']['name']
     self.assertTrue(isinstance(name, unicode_type))
     self.assertEqual(name, u('Espa\u00f1ol'))
     self.assertEqual(utf8(name), b'Espa\xc3\xb1ol')
Exemple #47
0
elif ssl:
    # Python 2.6-2.7.8
    _client_ssl_defaults = dict(cert_reqs=ssl.CERT_REQUIRED,
                                ca_certs=certifi.where())
    _server_ssl_defaults = {}
else:
    # Google App Engine
    _client_ssl_defaults = dict(cert_reqs=None, ca_certs=None)
    _server_ssl_defaults = {}

# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,
# getaddrinfo attempts to import encodings.idna. If this is done at
# module-import time, the import lock is already held by the main thread,
# leading to deadlock. Avoid it by caching the idna encoder on the main
# thread now.
u('foo').encode('idna')

# These errnos indicate that a non-blocking operation must be retried
# at a later time.  On most platforms they're the same value, but on
# some they differ.
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)

if hasattr(errno, "WSAEWOULDBLOCK"):
    _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK, )

# Default backlog used when calling sock.listen()
_DEFAULT_BACKLOG = 128


def bind_sockets(port,
                 address=None,
Exemple #48
0
 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")]})
Exemple #49
0
            # python 3
            self.ngettext = translations.ngettext
            self.gettext = translations.gettext
        # self.gettext must exist before __init__ is called, since it
        # calls into self.translate
        super(GettextLocale, self).__init__(code, translations)

    def translate(self, message, plural_message=None, count=None):
        if plural_message is not None:
            assert count is not None
            return self.ngettext(message, plural_message, count)
        else:
            return self.gettext(message)

LOCALE_NAMES = {
    "af_ZA": {"name_en": u("Afrikaans"), "name": u("Afrikaans")},
    "am_ET": {"name_en": u("Amharic"), "name": u('\u12a0\u121b\u122d\u129b')},
    "ar_AR":
    {"name_en": u("Arabic"), "name":
     u("\u0627\u0644\u0639\u0631\u0628\u064a\u0629")},
    "bg_BG":
    {"name_en": u("Bulgarian"), "name": u(
     "\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438")},
    "bn_IN":
    {"name_en": u("Bengali"), "name": u("\u09ac\u09be\u0982\u09b2\u09be")},
    "bs_BA": {"name_en": u("Bosnian"), "name": u("Bosanski")},
    "ca_ES": {"name_en": u("Catalan"), "name": u("Catal\xe0")},
    "cs_CZ": {"name_en": u("Czech"), "name": u("\u010ce\u0161tina")},
    "cy_GB": {"name_en": u("Welsh"), "name": u("Cymraeg")},
    "da_DK": {"name_en": u("Danish"), "name": u("Dansk")},
    "de_DE": {"name_en": u("German"), "name": u("Deutsch")},
Exemple #50
0
 def test_empty_query_string(self):
     response = self.fetch("/echo?foo=&foo=")
     data = json_decode(response.body)
     self.assertEqual(data, {u("foo"): [u(""), u("")]})
Exemple #51
0
 def test_import_module_unicode(self):
     # The internal implementation of __import__ differs depending on
     # whether the thing being imported is a module or not.
     # This variant requires a byte string in python 2.
     self.assertIs(import_object(u('tornado.escape')), tornado.escape)
Exemple #52
0
 def test_empty_post_parameters(self):
     response = self.fetch("/echo", method="POST", body="foo=&bar=")
     data = json_decode(response.body)
     self.assertEqual(data, {u("foo"): [u("")], u("bar"): [u("")]})
Exemple #53
0
 def test_unicode_escapes(self):
     self.assertEqual(utf8(u('\u00e9')), b'\xc3\xa9')
Exemple #54
0
#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, with_statement
import tornado.escape

from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import u, unicode_type, bytes_type
from tornado.test.util import unittest

linkify_tests = [
    # (input, linkify_kwargs, expected_output)
    ("hello http://world.com/!", {},
     u('hello <a href="http://world.com/">http://world.com/</a>!')),
    ("hello http://world.com/with?param=true&stuff=yes", {},
     u('hello <a href="http://world.com/with?param=true&amp;stuff=yes">http://world.com/with?param=true&amp;stuff=yes</a>'
       )),

    # an opened paren followed by many chars killed Gruber's regex
    ("http://url.com/w(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", {},
     u('<a href="http://url.com/w">http://url.com/w</a>(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
       )),

    # as did too many dots at the end
    ("http://url.com/withmany.......................................", {},
     u('<a href="http://url.com/withmany">http://url.com/withmany</a>.......................................'
       )),
    ("http://url.com/withmany((((((((((((((((((((((((((((((((((a)", {},
     u('<a href="http://url.com/withmany">http://url.com/withmany</a>((((((((((((((((((((((((((((((((((a)'
       )),

    # some examples from http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
from __future__ import absolute_import, division, print_function, with_statement
from tornado.ioloop import IOLoop
from tornado.netutil import ThreadedResolver
from tornado.util import u

# When this module is imported, it runs getaddrinfo on a thread. Since
# the hostname is unicode, getaddrinfo attempts to import encodings.idna
# but blocks on the import lock. Verify that ThreadedResolver avoids
# this deadlock.

resolver = ThreadedResolver()
IOLoop.current().run_sync(lambda: resolver.resolve(u('localhost'), 80))
Exemple #56
0
 def test_escape_return_types(self):
     # On python2 the escape methods should generally return the same
     # type as their argument
     self.assertEqual(type(xhtml_escape("foo")), str)
     self.assertEqual(type(xhtml_escape(u("foo"))), unicode_type)
Exemple #57
0
 def get(self):
     # unicode domain and path arguments shouldn't break things
     # either (see bug #285)
     self.set_cookie("unicode_args", "blah", domain=u("foo.com"),
                     path=u("/foo"))
Exemple #58
0
    def test_unicode_apply(self):
        def upper(s):
            return to_unicode(s).upper()

        template = Template(utf8(u("{% apply upper %}foo \u00e9{% end %}")))
        self.assertEqual(template.generate(upper=upper), utf8(u("FOO \u00c9")))
Exemple #59
0
 def test_twitter_get_user(self):
     response = self.fetch(
         '/twitter/client/login?oauth_token=zxcv',
         headers={'Cookie': '_oauth_request_token=enhjdg==|MTIzNA=='})
     response.rethrow()
     parsed = json_decode(response.body)
     self.assertEqual(
         parsed, {
             u('access_token'): {
                 u('key'): u('hjkl'),
                 u('screen_name'): u('foo'),
                 u('secret'): u('vbnm')
             },
             u('name'): u('Foo'),
             u('screen_name'): u('foo'),
             u('username'): u('foo')
         })
Exemple #60
0
 def test_gzip(self):
     response = self.post_gzip('foo=bar')
     self.assertEquals(json_decode(response.body), {u('foo'): [u('bar')]})