Implemented pillar.flask_extra.ensure_schema(url)
This function ensures that the URL has the correct schema, given the app configuration. This is required because the Flask instance can sit behind an SSL-terminating proxy like HAProxy and not know that it is reachable via HTTPS.
This commit is contained in:
@@ -33,3 +33,54 @@ class FlaskExtraTest(unittest.TestCase):
|
||||
self.assertEqual(201, resp.status_code)
|
||||
self.assertNotIn('Vary', resp.headers)
|
||||
self.assertEqual('nah', resp.data.decode())
|
||||
|
||||
|
||||
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'))
|
||||
|
Reference in New Issue
Block a user