Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#14341 closed Uncategorized (worksforme)

Session middleware raises exception when unit testing HttpResponseForbidden

Reported by: dikbrouwer Owned by: nobody
Component: Testing framework Version: 1.2
Severity: Normal Keywords:
Cc: gerdemb Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When verifying the GET response of a view in a unit test (TestCase) the following exception is raised in case a HttpResponseForbidden status is returned and the session middleware is enabled:

if response.has_header('Vary'):
TypeError: unbound method has_header() must be called with HttpResponseForbidden instance as first argument (got str instance instead)

Change History (3)

comment:1 Changed 6 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

I can't reproduce. Please reopen if you can provide a programmatic test case example that fails.

comment:2 Changed 4 years ago by gerdemb

  • Cc gerdemb added
  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

I also encountered this error and discovered the problem was in my code. Instead of returning an HttpResponseForbidden() instance from my middleware, I was returning a HttpResponseForbidden class.

class MyMiddleware:
    def process_request(self, request):
        # Correct
        return HttpResponseForbidden()
        # Incorrect
        return HttpResponseForbidden

Although the error was in my code, Django could have provided a more helpful error message perhaps by catching the incorrect response type sooner.

comment:3 Changed 4 years ago by dikbrouwer

I can't reproduce it either, it seems to work fine for me know. Perhaps I was indeed returning a class, basic mistake. Sorry for the irrelevant bug report in that case.

Note: See TracTickets for help on using tickets.
Back to Top