Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always return a Response object from the test client #1894

davidism opened this issue Jul 16, 2020 · 0 comments · Fixed by #1879

always return a Response object from the test client #1894

davidism opened this issue Jul 16, 2020 · 0 comments · Fixed by #1879


Copy link

davidism commented Jul 16, 2020

The test client can currently return a number of different values. The default is to return a tuple of (app_iter, status_code, headers) representing the data passed to the WSGI start_response function. If you pass as_tuple=True, you get (environ, response), where response is that tuple, so (environ, (app_iter, status_code, header), representing the WSGI request and response. But if you pass response_wrapper, the response tuple in both those cases becomes a Response class.

For most tests it's easier to assert properties of a Response object, so most code ends up importing and passing Response. Even then, it misses out on useful test information, such as redirect history (#763), and the request is still only accessible as a separate environ dict. should always return a Response object, with the following behavior. The Request and Response wrappers on their own add essentially no overhead, they don't do anything with the data passed to them until properties are accessed.

  • To support test-related data and methods, create a new TestResponse subclass. If no response_wrapper is passed, or Response is passed as the wrapper, use TestResponse instead.
  • To continue to support passing a custom response_wrapper, if the passed class is not a subclass of TestResponse, make a new class with both the custom class and TestResponse as bases.
  • To continue to support tests that expected a tuple (app_iter, status_code, headers = client.get(...), or status_code = client.get(...)[1]), the class can implement __iter__ and __getitem__, along with deprecation warnings.
  • Store the environ that produced the response by wrapping it in Request and storing it in a TestResponse.request attribute. Deprecate the as_tuple parameter.
@davidism davidism added this to the 2.0.0 milestone Jul 16, 2020
@davidism davidism self-assigned this Jul 16, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

Successfully merging a pull request may close this issue.

1 participant