How do I unit-test HTTPS requests in Flask? How do I unit-test HTTPS requests in Flask? flask flask

How do I unit-test HTTPS requests in Flask?


You can force the Flask test's get() call to use HTTPS like this:

response = self.app.get('/login', base_url='https://localhost')assert(response.status_code == 200)

The addition of the base_url is used by the underlying Werkzeug to set the url scheme (HTTP/HTTPS). For local test calls like these the host name is not used, and can be left out. You can see basic in code documentation for base_url here.


Have you looked at unit testing for flask?

After your setup code, you'll have something like this

response = self.client.get(url)self.assertEquals(response.status_code, 301)self.assertEquals(resonse.text.find('https'), 0)

Update

It seems the best way forward is to create a werkzeug environment. Flask uses the werkzeug test client. You can have a look at the api here. The quickstart (which is useful) is over here.

You'll see that werkzeug has an EnvironBuilder with a base_url. Perhaps its possible to play around with that to mimic an https environment in your test suite.


There's a even easier Flask way to do that, just use the PREFERRED_URL_SCHEME configuration parameter equals to 'https'.

You can find it's definition here. You can find the rules how it's used internally by Flask here. You can also find how its used here.