1

The following simplified code is defined in my flask app.

def select_url_for():
    if request.is_secure:
        return 's3'        
    return 'local'

I tried testing like this, and it works for HTTP.

def test_select_url_for(self):
    with self.app.test_request_context(): 
        self.assertTrue(select_url_for() == 'local')

How do I perform a similar test for the HTTPS with Flask?

I found this, but the question is unanswered. I need to run Flask in a HTTPS test mode.

Community
  • 1
  • 1
nu everest
  • 9,589
  • 12
  • 71
  • 90

1 Answers1

1

request.is_secure check wsgi.url_scheme environment variable. By overriding it with https make request.is_secure returns True:

def test_select_url_for(self):
    with self.app.test_request_context(environ_overrides={
            'wsgi.url_scheme': 'https'
    }):
        self.assertEqual(select_url_for(), 'local')

BTW, instead of assertTrue(a == b), use assertEqual(a, b) which give you more readable assertion failure message.

falsetru
  • 357,413
  • 63
  • 732
  • 636