For certain pages in a Flask app I'm creating, I have an HTTPS redirection system as follows.
def requires_https(f, code=302):
"""defaults to temp. redirect (301 is permanent)"""
@wraps(f)
def decorated(*args, **kwargs):
passthrough_conditions = [
request.is_secure,
request.headers.get('X-Forwarded-Proto', 'http') == 'https',
'localhost' in request.url
]
if not any(passthrough_conditions):
if request.url.startswith('http://'):
url = request.url.replace('http://', 'https://')
r = redirect(url, code=code)
return r
return decorated
If you're not requesting the HTTPS version of the page, it redirects you to it. I want to write unit tests for this service. I have written one that makes sure that you're redirected to the HTTPS version (check for a 301 or a 301, basically). I want to test that if you are requesting the https version of the page and are already on https, it does not redirect you (basically, for a 200). How do I get Flask to send an https request in the unit test?