Opened 8 years ago

Last modified 5 years ago

#27760 new Cleanup/optimization

Hard to diagnose reason for 400 response when making test request that sets SERVER_NAME

Reported by: Peter Inglesby Owned by: nobody
Component: Testing framework Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Since #26666 was fixed, requests made with the test client that set SERVER_NAME will return a 400, where the body of the response is simply "<h1>Bad Request (400)</h1>".

To work out what's gone wrong, you have to grep the Django source code for Bad Request. Could there be a better way to communicate the cause of the problem?

Change History (5)

comment:1 by Tim Graham, 8 years ago

I think you can use the manage.py test --debug-mode option added in #27008 to get the detailed exception page.

comment:2 by Peter Inglesby, 8 years ago

Running the test with --debug-mode does tell you what the problem is, but it's only useful if you know that it exists, and even then it's not obvious that it would help.

I've been poking around core/handlers/exception.py, and was able to get the behaviour I'm after by adding signals.got_request_exception.send(sender=None, request=request) to the SuspiciousOperation branch of response_for_exception. Would this be a viable approach?

comment:3 by Tim Graham, 8 years ago

A discussion on the DevelopersMailingList would be needed to see if there's consensus to make a backwards-incompatible change there. The current behavior is documented. The commit that introduced that signal is f9cdde0cb41851e9d1eb70bd108debb75f267585.

comment:4 by Tim Graham, 8 years ago

Triage Stage: UnreviewedSomeday/Maybe
Type: UncategorizedCleanup/optimization

comment:5 by Petr Dlouhý, 5 years ago

For anyone bumping into this. I was able to determine the cause of the Bad request by adding

@override_settings(
    DEBUG=True,
)

decorator to the test class and then printing the response into file with

with open("response.html", "w") as f:
    f.write(response.content.decode())

And the error was, that I hadn't test server url in ALOWED_HOSTS.

Last edited 5 years ago by Petr Dlouhý (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top