Passing custom HTTP headers using Flask’s test_client
Passing custon HTTP headers using Flask’s test_client object should have been as easy as doing this:
1 2 |
headers = { 'API-KEY': 'myKey' } response = self.client.get('/users', follow_redirects=True, headers=headers) |
For some reason this didn’t work in my Flask project. In case others have the same issue as I did, maybe the solution I found to work may work for others too:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import unittest from server import create_app class UsersTests(unittest.TestCase): def setUp(self): app = create_app() self.client = app.test_client() def test_custom_headers(self): self.client.environ_base['HTTP_API_KEY'] = "mySecretKey" self.client.environ_base['HTTP_ACCEPT'] = 'application/xml' response = self.client.get("/users", follow_redirects=True) |