Opened 4 years ago

Last modified 5 months 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 Changed 4 years ago by Tim Graham

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

comment:2 Changed 4 years ago by Peter Inglesby

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 Changed 4 years ago by Tim Graham

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 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedSomeday/Maybe
Type: UncategorizedCleanup/optimization

comment:5 Changed 5 months ago by Petr Dlouhý

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 months ago by Petr Dlouhý (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top