title: How to add headers to flask test_request_context?
body: <p>I have a testing fixture to make a test client in my flask app:</p>
  
  <pre class="lang-py prettyprint-override"><code>@pytest.fixture(scope='session') 
  def test_client():
      """ Create the application and the test client. 
      """
      print('----------Setup test client----------')
      app = create_app(config_class=config.TestConfig)
      testing_client = app.test_client()
  
      ctx = app.test_request_context() 
      ctx.push()
      yield testing_client # this is where the testing happens
      print('-------Teardown test client--------')
      ctx.pop()
  </code></pre>
  
  <p>I would like to add a header for "User-Agent" to this test request context because in my application I check the browser of the user. I am retrieving the browser name in my app via</p>
  
  <pre class="lang-py prettyprint-override"><code>user_agent = flask.request.user_agent.browser
  </code></pre>
  
  <p>This line returns None if I use the test client above. While I can successfully set the 'User-Agent' header in individual requests within a single test, e.g.:</p>
  
  <pre class="lang-py prettyprint-override"><code>user_agent_str = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
  
  response = test_client.get('/some_flask_route',
          environ_base={'HTTP_USER_AGENT': user_agent_str},
          follow_redirects=True)
  </code></pre>
  
  <p>After doing this:
  <code>flask.request.user_agent.browser</code> 
  returns 'chrome' as expected.</p>
  
  <p>However, this is very redundant because it requires me to insert the environ_base line of code into every single one of my many tests. </p>
  
  <h3>Expected Behavior</h3>
  
  <p>
  I would expect that I could set the header when I make the test request context in my test fixture, e.g.:</p>
  
  <pre class="lang-py prettyprint-override"><code>user_agent_str = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
  
  @pytest.fixture(scope='session') 
  def test_client():
      """ Create the application and the test client. 
      """
      print('----------Setup test client----------')
      app = create_app(config_class=config.TestConfig)
      testing_client = app.test_client()
      ctx = app.test_request_context(environ_base={'HTTP_USER_AGENT': user_agent_str}) 
      ctx.push()
      yield testing_client # this is where the testing happens
      print('-------Teardown test client--------')
      ctx.pop()
  </code></pre>
  
  <p>and this would set the environment for all requests, eliminating the need to set environ_base in every one of my requests.</p>
  
  <h3>Actual Behavior</h3>
  
  <p>While adding environ_base into test_request_context() does not break the fixture, it does not set the 'User-Agent' header and </p>
  
  <pre class="lang-py prettyprint-override"><code>flask.request.user_agent.browser
  </code></pre>
  
  <p>returns None.</p>
  
  <h3>Environment</h3>
  
  <ul>
  <li>Python version: 3.7.3</li>
  <li>Flask version: 1.1.1</li>
  <li>Werkzeug version: 0.15.4</li>
  </ul>
  
  <p>I have tried the solution suggested in the question here: <a href="https://stackoverflow.com/questions/15278285/setting-mocking-request-headers-for-flask-app-unit-test">Setting (mocking) request headers for Flask app unit test</a></p>
  
  <p>but it does not seem to change anything. I am simply running the flask server on localhost with no deployment yet, i.e. essentially just:</p>
  
  <pre class="lang-py prettyprint-override"><code>app = Flask(__name__)
  app.run(host='127.0.0.1',port=5000,debug=True)
  </code></pre>