Python tornado.escape.to_unicode() Examples

The following are 30 code examples of tornado.escape.to_unicode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.escape , or try the search function .
Example #1
Source File: simple_httpclient_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []
        chunks = []
        self.fetch("/redirect?url=/hello",
                   header_callback=headers.append,
                   streaming_callback=chunks.append)
        chunks = list(map(to_unicode, chunks))
        self.assertEqual(chunks, ['Hello world!'])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #2
Source File: httpclient_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_method_after_redirect(self):
        # Legacy redirect codes (301, 302) convert POST requests to GET.
        for status in [301, 302, 303]:
            url = "/redirect?url=/all_methods&status=%d" % status
            resp = self.fetch(url, method="POST", body=b"")
            self.assertEqual(b"GET", resp.body)

            # Other methods are left alone.
            for method in ["GET", "OPTIONS", "PUT", "DELETE"]:
                resp = self.fetch(url, method=method, allow_nonstandard_methods=True)
                self.assertEqual(utf8(method), resp.body)
            # HEAD is different so check it separately.
            resp = self.fetch(url, method="HEAD")
            self.assertEqual(200, resp.code)
            self.assertEqual(b"", resp.body)

        # Newer redirects always preserve the original method.
        for status in [307, 308]:
            url = "/redirect?url=/all_methods&status=307"
            for method in ["GET", "OPTIONS", "POST", "PUT", "DELETE"]:
                resp = self.fetch(url, method=method, allow_nonstandard_methods=True)
                self.assertEqual(method, to_unicode(resp.body))
            resp = self.fetch(url, method="HEAD")
            self.assertEqual(200, resp.code)
            self.assertEqual(b"", resp.body) 
Example #3
Source File: locale_test.py    From pySINDy with MIT License 6 votes vote down vote up
def test_csv_bom(self):
        with open(os.path.join(os.path.dirname(__file__), 'csv_translations',
                               'fr_FR.csv'), 'rb') as f:
            char_data = to_unicode(f.read())
        # Re-encode our input data (which is utf-8 without BOM) in
        # encodings that use the BOM and ensure that we can still load
        # it. Note that utf-16-le and utf-16-be do not write a BOM,
        # so we only test whichver variant is native to our platform.
        for encoding in ['utf-8-sig', 'utf-16']:
            tmpdir = tempfile.mkdtemp()
            try:
                with open(os.path.join(tmpdir, 'fr_FR.csv'), 'wb') as f:
                    f.write(char_data.encode(encoding))
                tornado.locale.load_translations(tmpdir)
                locale = tornado.locale.get('fr_FR')
                self.assertIsInstance(locale, tornado.locale.CSVLocale)
                self.assertEqual(locale.translate("school"), u"\u00e9cole")
            finally:
                shutil.rmtree(tmpdir) 
Example #4
Source File: simple_httpclient_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []  # type: typing.List[str]
        chunk_bytes = []  # type: typing.List[bytes]
        self.fetch(
            "/redirect?url=/hello",
            header_callback=headers.append,
            streaming_callback=chunk_bytes.append,
        )
        chunks = list(map(to_unicode, chunk_bytes))
        self.assertEqual(chunks, ["Hello world!"])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #5
Source File: locale_test.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def test_csv_bom(self):
        with open(
            os.path.join(os.path.dirname(__file__), "csv_translations", "fr_FR.csv"),
            "rb",
        ) as f:
            char_data = to_unicode(f.read())
        # Re-encode our input data (which is utf-8 without BOM) in
        # encodings that use the BOM and ensure that we can still load
        # it. Note that utf-16-le and utf-16-be do not write a BOM,
        # so we only test whichver variant is native to our platform.
        for encoding in ["utf-8-sig", "utf-16"]:
            tmpdir = tempfile.mkdtemp()
            try:
                with open(os.path.join(tmpdir, "fr_FR.csv"), "wb") as f:
                    f.write(char_data.encode(encoding))
                tornado.locale.load_translations(tmpdir)
                locale = tornado.locale.get("fr_FR")
                self.assertIsInstance(locale, tornado.locale.CSVLocale)
                self.assertEqual(locale.translate("school"), u"\u00e9cole")
            finally:
                shutil.rmtree(tmpdir) 
Example #6
Source File: simple_httpclient_test.py    From pySINDy with MIT License 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []
        chunks = []
        self.fetch("/redirect?url=/hello",
                   header_callback=headers.append,
                   streaming_callback=chunks.append)
        chunks = list(map(to_unicode, chunks))
        self.assertEqual(chunks, ['Hello world!'])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #7
Source File: locale_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_csv_bom(self):
        with open(os.path.join(os.path.dirname(__file__), 'csv_translations',
                               'fr_FR.csv'), 'rb') as f:
            char_data = to_unicode(f.read())
        # Re-encode our input data (which is utf-8 without BOM) in
        # encodings that use the BOM and ensure that we can still load
        # it. Note that utf-16-le and utf-16-be do not write a BOM,
        # so we only test whichver variant is native to our platform.
        for encoding in ['utf-8-sig', 'utf-16']:
            tmpdir = tempfile.mkdtemp()
            try:
                with open(os.path.join(tmpdir, 'fr_FR.csv'), 'wb') as f:
                    f.write(char_data.encode(encoding))
                tornado.locale.load_translations(tmpdir)
                locale = tornado.locale.get('fr_FR')
                self.assertIsInstance(locale, tornado.locale.CSVLocale)
                self.assertEqual(locale.translate("school"), u("\u00e9cole"))
            finally:
                shutil.rmtree(tmpdir) 
Example #8
Source File: locale_test.py    From teleport with Apache License 2.0 6 votes vote down vote up
def test_csv_bom(self):
        with open(os.path.join(os.path.dirname(__file__), 'csv_translations',
                               'fr_FR.csv'), 'rb') as f:
            char_data = to_unicode(f.read())
        # Re-encode our input data (which is utf-8 without BOM) in
        # encodings that use the BOM and ensure that we can still load
        # it. Note that utf-16-le and utf-16-be do not write a BOM,
        # so we only test whichver variant is native to our platform.
        for encoding in ['utf-8-sig', 'utf-16']:
            tmpdir = tempfile.mkdtemp()
            try:
                with open(os.path.join(tmpdir, 'fr_FR.csv'), 'wb') as f:
                    f.write(char_data.encode(encoding))
                tornado.locale.load_translations(tmpdir)
                locale = tornado.locale.get('fr_FR')
                self.assertIsInstance(locale, tornado.locale.CSVLocale)
                self.assertEqual(locale.translate("school"), u"\u00e9cole")
            finally:
                shutil.rmtree(tmpdir) 
Example #9
Source File: simple_httpclient_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []
        chunks = []
        self.fetch("/redirect?url=/hello",
                   header_callback=headers.append,
                   streaming_callback=chunks.append)
        chunks = list(map(to_unicode, chunks))
        self.assertEqual(chunks, ['Hello world!'])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #10
Source File: simple_httpclient_test.py    From teleport with Apache License 2.0 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []
        chunks = []
        self.fetch("/redirect?url=/hello",
                   header_callback=headers.append,
                   streaming_callback=chunks.append)
        chunks = list(map(to_unicode, chunks))
        self.assertEqual(chunks, ['Hello world!'])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #11
Source File: locale_test.py    From tornado-zh with MIT License 6 votes vote down vote up
def test_csv_bom(self):
        with open(os.path.join(os.path.dirname(__file__), 'csv_translations',
                               'fr_FR.csv'), 'rb') as f:
            char_data = to_unicode(f.read())
        # Re-encode our input data (which is utf-8 without BOM) in
        # encodings that use the BOM and ensure that we can still load
        # it. Note that utf-16-le and utf-16-be do not write a BOM,
        # so we only test whichver variant is native to our platform.
        for encoding in ['utf-8-sig', 'utf-16']:
            tmpdir = tempfile.mkdtemp()
            try:
                with open(os.path.join(tmpdir, 'fr_FR.csv'), 'wb') as f:
                    f.write(char_data.encode(encoding))
                tornado.locale.load_translations(tmpdir)
                locale = tornado.locale.get('fr_FR')
                self.assertIsInstance(locale, tornado.locale.CSVLocale)
                self.assertEqual(locale.translate("school"), u("\u00e9cole"))
            finally:
                shutil.rmtree(tmpdir) 
Example #12
Source File: simple_httpclient_test.py    From EventGhost with GNU General Public License v2.0 6 votes vote down vote up
def test_streaming_follow_redirects(self):
        # When following redirects, header and streaming callbacks
        # should only be called for the final result.
        # TODO(bdarnell): this test belongs in httpclient_test instead of
        # simple_httpclient_test, but it fails with the version of libcurl
        # available on travis-ci. Move it when that has been upgraded
        # or we have a better framework to skip tests based on curl version.
        headers = []
        chunks = []
        self.fetch("/redirect?url=/hello",
                   header_callback=headers.append,
                   streaming_callback=chunks.append)
        chunks = list(map(to_unicode, chunks))
        self.assertEqual(chunks, ['Hello world!'])
        # Make sure we only got one set of headers.
        num_start_lines = len([h for h in headers if h.startswith("HTTP/")])
        self.assertEqual(num_start_lines, 1) 
Example #13
Source File: template_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
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"))) 
Example #14
Source File: template_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
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")) 
Example #15
Source File: test_graphql.py    From graphene-tornado with MIT License 5 votes vote down vote up
def test_supports_pretty_printing_by_request(http_helper):
    response = yield http_helper.get(
        url_string(query="{test}", pretty="1"), headers=GRAPHQL_HEADER
    )
    assert (
        to_unicode(response.body)
        == """{
  "data": {
    "test": "Hello World"
  }
}"""
    ) 
Example #16
Source File: test_graphiql.py    From graphene-tornado with MIT License 5 votes vote down vote up
def has_graphiql(response):
    assert "ReactDOM" in to_unicode(response.body)
    assert response.code == 200 
Example #17
Source File: test_graphiql.py    From graphene-tornado with MIT License 5 votes vote down vote up
def test_graphiql_renders_pretty(http_helper):
    response = yield http_helper.get(
        "/graphql?query={test}", headers={"Accept": "text/html"}
    )
    pretty_response = (
        ("{\n" '  "data": {\n' '    "test": "Hello World"\n' "  }\n" "}")
        .replace('"', '\\"')
        .replace("\n", "\\n")
    )
    assert pretty_response in to_unicode(response.body) 
Example #18
Source File: test_graphiql.py    From graphene-tornado with MIT License 5 votes vote down vote up
def test_graphiql_default_title(http_helper):
    res = yield http_helper.get("/graphql", headers={"Accept": "text/html"})
    assert "<title>GraphiQL</title>" in to_unicode(res.body) 
Example #19
Source File: test_graphql.py    From graphene-tornado with MIT License 5 votes vote down vote up
def response_json(response):
    return json.loads(to_unicode(response.body)) 
Example #20
Source File: web_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #21
Source File: template_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_bytes_apply(self):
        def upper(s):
            return utf8(to_unicode(s).upper())
        template = Template(utf8(u("{% apply upper %}foo \u00e9{% end %}")))
        self.assertEqual(template.generate(upper=upper), utf8(u("FOO \u00c9"))) 
Example #22
Source File: escape_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
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) 
Example #23
Source File: template_test.py    From pySINDy with MIT License 5 votes vote down vote up
def test_bytes_apply(self):
        def upper(s):
            return utf8(to_unicode(s).upper())
        template = Template(utf8(u"{% apply upper %}foo \u00e9{% end %}"))
        self.assertEqual(template.generate(upper=upper), utf8(u"FOO \u00c9")) 
Example #24
Source File: template_test.py    From pySINDy with MIT License 5 votes vote down vote up
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")) 
Example #25
Source File: concurrent_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def handle_read(self, data):
        logging.info("handle_read")
        data = to_unicode(data)
        if data == data.upper():
            self.stream.write(b"error\talready capitalized\n")
        else:
            # data already has \n
            self.stream.write(utf8("ok\t%s" % data.upper()))
        self.stream.close() 
Example #26
Source File: tcpserver_test.py    From pySINDy with MIT License 5 votes vote down vote up
def run_subproc(self, code):
        proc = subprocess.Popen(sys.executable,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
        proc.stdin.write(utf8(code))
        proc.stdin.close()
        proc.wait()
        stdout = proc.stdout.read()
        proc.stdout.close()
        if proc.returncode != 0:
            raise RuntimeError("Process returned %d. stdout=%r" % (
                proc.returncode, stdout))
        return to_unicode(stdout) 
Example #27
Source File: concurrent_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def process_response(self, data):
        status, message = re.match('(.*)\t(.*)\n', to_unicode(data)).groups()
        if status == 'ok':
            return message
        else:
            raise CapError(message) 
Example #28
Source File: concurrent_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def handle_stream(self, stream, address):
        data = yield stream.read_until(b"\n")
        data = to_unicode(data)
        if data == data.upper():
            stream.write(b"error\talready capitalized\n")
        else:
            # data already has \n
            stream.write(utf8("ok\t%s" % data.upper()))
        stream.close() 
Example #29
Source File: tcpserver_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def run_subproc(self, code):
        proc = subprocess.Popen(sys.executable,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
        proc.stdin.write(utf8(code))
        proc.stdin.close()
        proc.wait()
        stdout = proc.stdout.read()
        proc.stdout.close()
        if proc.returncode != 0:
            raise RuntimeError("Process returned %d. stdout=%r" % (
                proc.returncode, stdout))
        return to_unicode(stdout) 
Example #30
Source File: template_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def test_bytes_apply(self):
        def upper(s):
            return utf8(to_unicode(s).upper())
        template = Template(utf8(u"{% apply upper %}foo \u00e9{% end %}"))
        self.assertEqual(template.generate(upper=upper), utf8(u"FOO \u00c9"))