Flask unit testing: Getting the response's redirect location Flask unit testing: Getting the response's redirect location flask flask

Flask unit testing: Getting the response's redirect location


Following on from your own answer, depending on your personal preference of unit test style so feel free to ignore, you may prefer something like the following suggestion to simplify and improve clarity and readability of the unit test:

# Python 3from urllib.parse import urlparse# Python 2from urlparse import urlparseresponse = self.test_client.post(    request_path,    data=data,    follow_redirects=False)expectedPath = '/'self.assertEqual(response.status_code, 302)self.assertEqual(urlparse(response.location).path, expectedPath)


@bwbrowning has provided the correct hint - doing a post with follow_redirects=False the return value has the location attribute set - which is the complete request path including parameters.

edit: hint - there is a slight gotcha when doing the test_client.get(..) - the path parameter needs to be a relative path, while ret.location return the full path. so, what I did was

child_path_with_parameters = rv.location.split('http://localhost')[1]child_path = child_path_with_parameters.split('?')[0]ret = self.test_client.get(child_path_with_parameters)

(the child_path is used later in order to post to the child)