For example, let's say that you have some code in your app that looks like this:
@app.route('/login', methods=['POST', 'OPTIONS'])
def login():
print request.remote_addr
print request.headers['User-Agent']
And then in your testing code you do something like:
client = app.test_client()
client.post('/login',
data=json.dumps({
'username': 'sheldon@cooper.com',
'password': 'howimetyourmother'
}), content_type='application/json')
Most likely, you will then get an error that looks something like:
File "/Users/leonard/app/rest/app.py", line 515, in login
if request.headers['User-Agent']:
File "/Users/leonard/.virtualenvs/app/lib/python2.7/site-packages/werkzeug/datastructures.py", line 1229, in __getitem__
return self.environ['HTTP_' + key]
KeyError: 'HTTP_USER_AGENT'
As it turns out, there are two solutions to this problems that I found on StackOverflow (1, 2). This first option is to set the request environment variables on each call using the environ_base parameter provided by Werkzeug (more details on that here). For example, we can do something like:
client.post('/login',
data=json.dumps({
'username': 'sheldon@cooper.com',
'password': 'howimetyourmother'
}), content_type='application/json',
environ_base={
'HTTP_USER_AGENT': 'Chrome',
'REMOTE_ADDR': '127.0.0.1'
})
Although this method works perfectly well, it requires us to set the values on each call. If we have a really long unit testing script that makes many calls (as I do), then this becomes rather burdensome. The second solution is to create a proxy for the Flask app that overrides the __call__ method. So, the proxy looks something like:
class FlaskTestClientProxy(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', '127.0.0.1')
environ['HTTP_USER_AGENT'] = environ.get('HTTP_USER_AGENT', 'Chrome')
return self.app(environ, start_response)
Then when we create our test client, we simply make sure to wrap the WSGI app with our proxy, and voilĂ , it works.
app.wsgi_app = FlaskTestClientProxy(app.wsgi_app)
client = app.test_client()
client.post('/login',
data=json.dumps({
'username': 'sheldon@cooper.com',
'password': 'howimetyourmother'
}), content_type='application/json')
ReplyDeleteThis is an interesting blog that you have posted, you shares a lot of useful things about Technology.
Manual Testing Training in Chennai
manual testing course fees in chennai
Manual Testing Training in OMR
Mobile Testing Training in Chennai
Mobile Application Testing Training
Mobile App Testing Training
Drupal Training in Chennai
Photoshop Classes in Chennai
IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.
DeleteIEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.
corporate training in chennai corporate training in chennai
corporate training companies in india corporate training companies in india
corporate training companies in chennai corporate training companies in chennai
I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai
Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.
ReplyDeleteTally Course in Chennai
Tally Classes in Chennai
ui design course in chennai
CCNA Training in Chennai
ReactJS Training in Chennai
microsoft dynamics crm training in chennai
Tally Training in Chennai
Great post!! This can be one particular of the most useful blogs. Basically Wonderful. I can understand your hard work.
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
Nice article! It is really gave an valuable information and it is easy to understand.
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course