/r/ handler: include Accept header in flask cache key

requires pending bug fix https://github.com/pallets-eco/flask-caching/pull/431
pull/353/head
Ryan Barrett 2022-12-26 09:52:08 -08:00
rodzic 2150693893
commit 5f082349d5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 30 dodań i 1 usunięć

Wyświetl plik

@ -90,6 +90,18 @@ def redir(to):
logger.info(f'redirecting to {to}')
return redirect(to, code=301)
# offically-supported-monkey-patch flask_caching to include Accept header in
# cache key:
# https://flask-caching.readthedocs.io/en/latest/api.html#flask_caching.Cache.cached
# requires this pending bug fix:
# https://github.com/pallets-eco/flask-caching/pull/431
orig_cache_key = redir.make_cache_key
def accept_header_cache_key(*args, **kwargs):
return f'{orig_cache_key(*args, **kwargs)} {request.headers.get("Accept")}'
redir.make_cache_key = accept_header_cache_key
def convert_to_as2(url, domain):
"""Fetch a URL as HTML, convert it to AS2, and return it.

Wyświetl plik

@ -4,6 +4,7 @@ git+https://github.com/snarfed/oauth-dropins.git#egg=oauth_dropins
git+https://github.com/snarfed/granary.git#egg=granary
git+https://github.com/snarfed/negotiator.git@py3#egg=negotiator
git+https://github.com/dvska/gdata-python3.git#egg=gdata
git+https://github.com/snarfed/flask-caching.git@fix-97#egg=Flask-Caching
beautifulsoup4==4.11.1
brevity==0.2.17
@ -20,7 +21,6 @@ feedgen==0.9.0
feedparser==6.0.10
fixtures==4.0.1
Flask==2.2.2
Flask-Caching==2.0.1
flask-gae-static==1.0
google-api-core==2.11.0
google-auth==2.15.0

Wyświetl plik

@ -6,6 +6,7 @@ from unittest.mock import patch
from granary import as2
from oauth_dropins.webutil.testutil import requests_response
from app import app, cache
import common
from models import User
from .test_webmention import REPOST_HTML, REPOST_AS2
@ -50,6 +51,22 @@ class RedirectTest(testutil.TestCase):
def test_as2_ld(self):
self._test_as2(common.CONTENT_TYPE_AS2_LD)
def test_accept_header_cache_key(self):
app.config['CACHE_TYPE'] = 'SimpleCache'
cache.init_app(app)
self.client = app.test_client()
got = self.client.get('/r/https://foo.com/bar')
self.assertEqual(301, got.status_code)
self.assertEqual('https://foo.com/bar', got.headers['Location'])
self._test_as2(common.CONTENT_TYPE_AS2)
got = self.client.get('/r/https://foo.com/bar',
headers={'Accept': 'text/html'})
self.assertEqual(301, got.status_code)
self.assertEqual('https://foo.com/bar', got.headers['Location'])
@patch('requests.get')
def _test_as2(self, accept, mock_get):
"""Currently mainly for Pixelfed.