utils: make sure bytes are used for URL cache key (#22203)

This commit is contained in:
Frédéric Péters 2018-03-01 14:43:28 +01:00
parent 690bda4315
commit 53146ac618
2 changed files with 7 additions and 1 deletions

View File

@ -23,6 +23,7 @@ from requests import Response, Session as RequestsSession
from django.conf import settings
from django.core.cache import cache
from django.utils.encoding import smart_bytes
from django.utils.http import urlencode
from .signature import sign_url
@ -101,7 +102,7 @@ class Requests(RequestsSession):
if method == 'GET' and cache_duration:
# handle cache
cache_key = hashlib.md5(url).hexdigest()
cache_key = hashlib.md5(smart_bytes(url)).hexdigest()
cache_content = cache.get(cache_key)
if cache_content and not invalidate_cache:
response = Response()

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import mock
import pytest
import urlparse
@ -143,3 +145,6 @@ def test_requests_cache():
# check raise_if_not_cached
with pytest.raises(NothingInCacheException):
requests.get('http://cache.example.org/other', raise_if_not_cached=True)
# check with unicode url
assert requests.get(u'http://cache.example.org/éléphant').content == 'hello second world'