nu everest
nu everest

Reputation: 10249

How to test HTTPS in Flask

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.

Upvotes: 1

Views: 362

Answers (1)

falsetru
falsetru

Reputation: 368894

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.

Upvotes: 1

Related Questions