Flask test_client: checking DELETE request with data

I am trying to test a Flask application using the suggestions http://flask.pocoo.org/docs/testing/ , but I could not figure out how to test the DELETE method with the form data.

My removal method looks something like this:

from flask.ext.restful import Resource, reqparse ... def delete(self): self.reqparse.add_argument('arg1', type=str, required=True, location='form') args = self.reqparse.parse_args() ... 

I would like to test it with:

 def setUp(self): self.app = myApp.app.test_client() def test_delete(self): rv = self.app.delete('MyEndPoint', data={'arg1', 'val'}) 

But that will not work. I also looked at the source code of EnvironBuilder in werkzeug.test, but still have not figured out how to transfer the data.

+8
python flask unit-testing werkzeug
source share
1 answer

I just ran into the same problem, and this is mainly because the Werkzeug validation method does not currently support setting the content_type of DELETE requests.

The code here shows how Werkzeug gets the content type:

 def _get_content_type(self): ct = self.headers.get('Content-Type') if ct is None and not self._input_stream: if self.method in ('POST', 'PUT', 'PATCH'): if self._files: return 'multipart/form-data' return 'application/x-www-form-urlencoded' return None return ct 

If there is no content_type , the form data never displays it from environ and into the request, so your Flask server does not actually send data.

Ultimately, this is a bug with Werkzeug, because you can create a curl request that uses the DELETE method and also includes form data. I sent a transfer request to the Werkzeug repository to solve this problem. Feel free to call on github: https://github.com/mitsuhiko/werkzeug/pull/620

Refresh . To solve the problem now, you can work around this by explicitly specifying the type of content in your request, for example:

 def test_delete(self): rv = self.app.delete('MyEndPoint', data={'arg1', 'val'}, headers={'Content-Type': 'application/x-www-form-urlencoded'}) 

Update again : the transfer request that I submitted was reviewed, refined and merged and will be included in Werkzeug version 0.10, so hopefully this should not be a problem anymore :)

+5
source share

All Articles