2017-05-11 13:07:33 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import flask
|
|
|
|
|
2018-03-23 16:34:33 +01:00
|
|
|
from pillar.tests import AbstractPillarTest
|
|
|
|
|
2017-05-11 13:07:33 +02:00
|
|
|
|
|
|
|
class FlaskExtraTest(unittest.TestCase):
|
|
|
|
def test_vary_xhr(self):
|
|
|
|
import pillar.flask_extra
|
|
|
|
|
|
|
|
class TestApp(flask.Flask):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.add_url_rule('/must-vary', 'must-vary', self.must_vary)
|
|
|
|
self.add_url_rule('/no-vary', 'no-vary', self.no_vary)
|
|
|
|
|
|
|
|
@pillar.flask_extra.vary_xhr()
|
|
|
|
def must_vary(self):
|
|
|
|
return 'yay'
|
|
|
|
|
|
|
|
def no_vary(self):
|
|
|
|
return 'nah', 201
|
|
|
|
|
|
|
|
app = TestApp(__name__)
|
|
|
|
client = app.test_client()
|
|
|
|
|
|
|
|
resp = client.get('/must-vary')
|
|
|
|
self.assertEqual(200, resp.status_code)
|
|
|
|
self.assertEqual('X-Requested-With', resp.headers['Vary'])
|
|
|
|
self.assertEqual('yay', resp.data.decode())
|
|
|
|
|
|
|
|
resp = client.get('/no-vary')
|
|
|
|
self.assertEqual(201, resp.status_code)
|
|
|
|
self.assertNotIn('Vary', resp.headers)
|
|
|
|
self.assertEqual('nah', resp.data.decode())
|
2018-01-12 17:21:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EnsureSchemaTest(unittest.TestCase):
|
|
|
|
def test_ensure_schema_http(self):
|
|
|
|
import pillar.flask_extra
|
|
|
|
|
|
|
|
suffix = '://user:password@hostname/some-path/%2Fpaththing?query=abc#fragment'
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.config['PREFERRED_URL_SCHEME'] = 'http'
|
|
|
|
with app.app_context():
|
|
|
|
for scheme in ('http', 'https', 'ftp', 'gopher'):
|
|
|
|
self.assertEqual(
|
|
|
|
f'http{suffix}',
|
|
|
|
pillar.flask_extra.ensure_schema(f'{scheme}{suffix}'))
|
|
|
|
|
|
|
|
def test_ensure_schema_https(self):
|
|
|
|
import pillar.flask_extra
|
|
|
|
|
|
|
|
suffix = '://user:password@hostname/some-path/%2Fpaththing?query=abc#fragment'
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
|
|
|
with app.app_context():
|
|
|
|
for scheme in ('http', 'https', 'ftp', 'gopher'):
|
|
|
|
self.assertEqual(
|
|
|
|
f'https{suffix}',
|
|
|
|
pillar.flask_extra.ensure_schema(f'{scheme}{suffix}'))
|
|
|
|
|
|
|
|
def test_no_config(self):
|
|
|
|
import pillar.flask_extra
|
|
|
|
|
|
|
|
suffix = '://user:password@hostname/some-path/%2Fpaththing?query=abc#fragment'
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.config.pop('PREFERRED_URL_SCHEME', None)
|
|
|
|
with app.app_context():
|
|
|
|
self.assertEqual(
|
|
|
|
f'https{suffix}',
|
|
|
|
pillar.flask_extra.ensure_schema(f'gopher{suffix}'))
|
|
|
|
|
|
|
|
def test_corner_cases(self):
|
|
|
|
import pillar.flask_extra
|
|
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
|
|
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
|
|
|
with app.app_context():
|
|
|
|
self.assertEqual('', pillar.flask_extra.ensure_schema(''))
|
|
|
|
self.assertEqual('/some/path/only', pillar.flask_extra.ensure_schema('/some/path/only'))
|
|
|
|
self.assertEqual('https://hostname/path',
|
|
|
|
pillar.flask_extra.ensure_schema('//hostname/path'))
|
2018-03-23 16:34:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HashedPathConverterTest(AbstractPillarTest):
|
|
|
|
def test_to_python(self):
|
|
|
|
from pillar.flask_extra import HashedPathConverter
|
|
|
|
|
|
|
|
hpc = HashedPathConverter({})
|
|
|
|
self.assertEqual('/path/to/file.min.js', hpc.to_python('/path/to/file.min.abcd1234.js'))
|
|
|
|
self.assertEqual('/path/to/file.js', hpc.to_python('/path/to/file.abcd1234.js'))
|
|
|
|
self.assertEqual('/path/to/file', hpc.to_python('/path/to/file'))
|
|
|
|
self.assertEqual('', hpc.to_python(''))
|
|
|
|
|
|
|
|
def test_to_url(self):
|
|
|
|
from pillar.flask_extra import HashedPathConverter
|
|
|
|
|
|
|
|
hpc = HashedPathConverter({})
|
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual('/path/to/file.min.abcd1234.js', hpc.to_url('/path/to/file.min.js'))
|
|
|
|
self.assertEqual('/path/to/file.abcd1234.js', hpc.to_url('/path/to/file.js'))
|
|
|
|
self.assertEqual('/path/to/file', hpc.to_url('/path/to/file'))
|
|
|
|
self.assertEqual('', hpc.to_url(''))
|