Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion googleapiclient/discovery_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def get_static_doc(serviceName, version):
doc_name = "{}.{}.json".format(serviceName, version)

try:
with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), "r") as f:
with open(
os.path.join(DISCOVERY_DOC_DIR, doc_name), "r", encoding="utf-8"
) as f:
content = f.read()
except FileNotFoundError:
# File does not exist. Nothing to do here.
Expand Down
20 changes: 19 additions & 1 deletion tests/test_discovery_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
"""Discovery document cache tests."""

import datetime
import os
import tempfile
import unittest
from unittest import mock

from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE, get_static_doc

try:
from googleapiclient.discovery_cache.file_cache import Cache as FileCache
Expand Down Expand Up @@ -59,3 +61,19 @@ def future_now():

# Make sure the content is expired
self.assertEqual(None, cache.get(first_url))


class GetStaticDocTest(unittest.TestCase):
def test_reads_discovery_document_as_utf8(self):
# The packaged discovery documents are UTF-8 and must be decoded as
# such regardless of the host's locale encoding.
content = '{"description": "\u201cquoted\u201d"}'
with tempfile.TemporaryDirectory() as doc_dir:
with open(
os.path.join(doc_dir, "fake.v1.json"), "w", encoding="utf-8"
) as f:
f.write(content)
with mock.patch(
"googleapiclient.discovery_cache.DISCOVERY_DOC_DIR", new=doc_dir
):
self.assertEqual(content, get_static_doc("fake", "v1"))
Loading