[Django] #21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG = True

23 views
Skip to first unread message

Django

unread,
Nov 16, 2013, 1:33:17 PM11/16/13
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
---------------------------------------------+------------------------
Reporter: aaugustin | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
This is a fairly obscure bug and it was interesting to diagnose ;-)

I ran into it while working on the Django Debug Toolbar. Since the DDT
isn't displayed when `DEBUG = False`, its tests run with `DEBUG = True`. I
admit that this isn't a common use case. (Of course, I could change the
function that controls whether the toolbar is shown to display it during
tests regardless of `DEBUG`. But this isn't the point.) Since the toolbar
doesn't handle any uploaded files, `MEDIA_URL` isn't set and defaults to
the empty string.

In these circumstances, `_MediaFilesHandler.should_handle(path)` will
always return `True` because any `path` starts with the empty string. Then
`_MediaFilesHandler.get_response(request)` will fail to find the media
file corresponding to `path` and catch the resulting `Http404`. When
`DEBUG = False`, `get_response` will continue after the try/except block
and call the parent `get_response`, which will return the expected result.
But when `DEBUG = True`, `get_response` will happily return the 404 debug
page! NB: all the code I've described is actually in `StaticFilesHandler`,
which `_MediaFilesHandler` inherits.

I believe there are actually two bugs here:
- `_MediaFilesHandler` shouldn't be applied when `MEDIA_URL` isn't set —
and while we're there, `StaticFilesHandler` shouldn't be applied either
when `STATIC_URL` isn't set.
- `get_response` shouldn't swallow `Http404` exceptions when `DEBUG =
False` — this behavior has hidden the previous problem until now.

--
Ticket URL: <https://code.djangoproject.com/ticket/21451>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2013, 1:38:16 PM11/16/13
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
-----------------------------------+--------------------------------------

Reporter: aaugustin | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by aaugustin):

I'm attaching a patch against Django 1.6 with three changes:

- a regression test
- a change in `StaticFilesHandler.get_response` that makes the test fail
when `DEBUG = False`. I tested by commenting out the settings override.
- a change in `LiveServerThread.run` that makes the test pass regardless
of the value of `DEBUG`.

I didn't spend too much time on this and I'm not sure this is the best
solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/21451#comment:1>

Django

unread,
Nov 28, 2013, 4:39:33 PM11/28/13
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
-----------------------------------+------------------------------------

Reporter: aaugustin | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by timo):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/21451#comment:2>

Django

unread,
May 26, 2014, 4:37:58 PM5/26/14
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
-----------------------------------+------------------------------------

Reporter: aaugustin | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by hatem@…):

I have just ran into this issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/21451#comment:3>

Django

unread,
Jun 3, 2014, 12:06:23 PM6/3/14
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
-----------------------------------+------------------------------------
Reporter: aaugustin | Owner: gchp
Type: Bug | Status: assigned

Component: Testing framework | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by gchp):

* owner: nobody => gchp
* status: new => assigned
* has_patch: 0 => 1


Comment:

I submitted this patch in https://github.com/django/django/pull/2759 - and
just added a couple of small comments.

I did some testing around not applying `StaticFilesHandler` when
`STATIC_URL` is not set. I don't think this is actually possible however,
as `LiveServerTestCase` in Django 1.6 used the `staticfiles` app, which in
turn required the `STATIC_URL` setting to be set.

--
Ticket URL: <https://code.djangoproject.com/ticket/21451#comment:4>

Django

unread,
Jun 7, 2014, 2:03:07 PM6/7/14
to django-...@googlegroups.com
#21451: LiveServerTestCase returns a 404 on all URLs when MEDIA_URL = '' and DEBUG
= True
-----------------------------------+------------------------------------
Reporter: aaugustin | Owner: gchp
Type: Bug | Status: closed

Component: Testing framework | Version: 1.6
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by timo):

* status: assigned => closed
* resolution: => fixed


Comment:

On the PR, Greg noted that this was fixed in 1.7 as part of the removal
the static files dependencies in `LiveServerTestCase` [e909ceae].

--
Ticket URL: <https://code.djangoproject.com/ticket/21451#comment:5>

Reply all
Reply to author
Forward
0 new messages