Skip to content

Commit

Permalink
Merge pull request #1 from Innovativity/master
Browse files Browse the repository at this point in the history
add Django 3.2 support (fixes jazzband#126)
  • Loading branch information
nrmitchi-bond authored Jun 25, 2021
2 parents db7031b + b9fa837 commit e9f5d00
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env:
- DJANGO_VERSION=1.11
- DJANGO_VERSION=2.2
- DJANGO_VERSION=3.0
- DJANGO_VERSION=3.2

jobs:
exclude:
Expand All @@ -31,6 +32,10 @@ jobs:
env: DJANGO_VERSION=3.0
- python: 3.5
env: DJANGO_VERSION=3.0
- python: 2.7
env: DJANGO_VERSION=3.2
- python: 3.5
env: DJANGO_VERSION=3.2
- python: 3.8
env: DJANGO_VERSION=1.8
- python: 3.8
Expand All @@ -40,7 +45,6 @@ jobs:
- python: 3.8
env: DJANGO_VERSION=1.11


install:
- pip install coveralls flake8 urllib3
- pip install django==${DJANGO_VERSION}
Expand Down
2 changes: 0 additions & 2 deletions revproxy/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ def get_django_response(proxy_response, strict_cookies=False):
logger.info('Normalizing response headers')
set_response_headers(response, headers)

logger.debug('Response headers: %s', getattr(response, '_headers'))

cookies = proxy_response.headers.getlist('set-cookie')
logger.info('Checking for invalid cookies')
for cookie_string in cookies:
Expand Down
14 changes: 12 additions & 2 deletions revproxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,24 @@ def required_header(header):


def set_response_headers(response, response_headers):
# check for Django 3.2 headers interface
# https://code.djangoproject.com/ticket/31789
# check and set pointer before loop to improve efficiency
if hasattr(response, 'headers'):
headers = response.headers
else:
headers = response

for header, value in response_headers.items():
if is_hop_by_hop(header) or header.lower() == 'set-cookie':
continue

response[header.title()] = value
headers[header] = value

logger.debug('Response headers: %s', getattr(response, '_headers'))
if hasattr(response, 'headers'):
logger.debug('Response headers: %s', response.headers)
else:
logger.debug('Response headers: %s', getattr(response, '_headers'))


def normalize_request_headers(request):
Expand Down
18 changes: 15 additions & 3 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ def test_response_headers_are_not_in_hop_by_hop_headers(self):
with patch(URLOPEN, urlopen_mock):
response = CustomProxyView.as_view()(request, path)

response_headers = response._headers
# Django 3.2+
if hasattr(response, 'headers'):
response_headers = response.headers
else:
response_headers = response._headers

for header in response_headers:
self.assertFalse(is_hop_by_hop(header))
Expand Down Expand Up @@ -122,7 +126,11 @@ def test_cookie_is_not_in_response_headers(self):
with patch(URLOPEN, urlopen_mock):
response = CustomProxyView.as_view()(request, path)

response_headers = response._headers
# Django 3.2+
if hasattr(response, 'headers'):
response_headers = response.headers
else:
response_headers = response._headers
self.assertNotIn('set-cookie', response_headers)

def test_set_cookie_is_used_by_httpproxy_response(self):
Expand Down Expand Up @@ -157,5 +165,9 @@ def test_invalid_cookie(self):
with patch(URLOPEN, urlopen_mock):
response = CustomProxyView.as_view()(request, path)

response_headers = response._headers
# Django 3.2+
if hasattr(response, 'headers'):
response_headers = response.headers
else:
response_headers = response._headers
self.assertFalse(response.cookies)

0 comments on commit e9f5d00

Please sign in to comment.