Question

I have a problem making the simple (non-json) arguments work with POST. Just taking the simple example from their tutorials, I can't make a unit test where the task is passing as an argument. However task is never passed in. Its none.

class TodoList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('task', type = str)
        super(TodoList, self).__init__()

    def post(self):
        args = self.reqparse.parse_args()
        #args['task'] is None, but why?
        return TODOS[args['task']], 201

Unit test:

def test_task(self):
        rv = self.app.post('todos', data='task=test')
        self.check_content_type(rv.headers)
        resp = json.loads(rv.data)
        eq_(rv.status_code, 201)

What am I missing please?

Was it helpful?

Solution

When you use 'task=test' test_client do not set application/x-www-form-urlencoded content type, because you put string to input stream. So flask can't detect form and read data from form and reqparse will return None for any values for this case.

To fix it you must set up content type or use dict {'task': 'test'} or tuple.

Also for request testing better to use client = self.app.test_client() instead app = self.app.test_client(), if you use FlaskTesting.TestCase class, then just call self.client.post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top