3

How can I set the remote_addr property in a flask test_request_context? This question: Get IP Address when testing flask application through nosetests (and other, similar questions) explains how to do it using the get/post/whatever test client calls, but I'm not using those (in this case). Instead, I get a test_request_context and then call a function, thereby allowing me to test the functions that are called by my view functions individually.

Edit: to clarify, my testing code looks something like this:

with app.test_request_context():
    result=my_function_which_expects_to_be_called_from_a_request_context()
<check result however>

So at no point am I using a test client call.

ibrewster
  • 3,482
  • 5
  • 42
  • 54

1 Answers1

7

Pass the same arguments to test_request_context as you would to client.get. Both set up the WSGI environment the same way internally.

with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):
davidism
  • 121,510
  • 29
  • 395
  • 339