﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21766	Can't use reverse() or is_valid_path() with dynamically set TestCase.urls	paulmelnikow	nobody	"I'm writing a component and need to override urls in a TestCase. This works fine if I set urls to a string with a module name, but the common middleware raises an exception if I specify a set of patterns.

{{{
Traceback (most recent call last):
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/test/utils.py"", line 249, in inner
    return test_func(*args, **kwargs)
  File ""/Users/pnm/code/django-url-bug/tests.py"", line 26, in test_hello_2
    resp = self.client.get('/hello')
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/test/client.py"", line 473, in get
    response = super(Client, self).get(path, data=data, **extra)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/test/client.py"", line 280, in get
    return self.request(**r)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/test/client.py"", line 426, in request
    response = self.handler(environ)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/test/client.py"", line 109, in __call__
    response = self.get_response(request)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/core/handlers/base.py"", line 196, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/core/handlers/base.py"", line 90, in get_response
    response = middleware_method(request)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/middleware/common.py"", line 71, in process_request
    if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/core/urlresolvers.py"", line 573, in is_valid_path
    resolve(path, urlconf)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/core/urlresolvers.py"", line 453, in resolve
    return get_resolver(urlconf).resolve(path)
  File ""/Users/pnm/code/django-url-bug/venv/lib/python2.7/site-packages/django/utils/functional.py"", line 30, in wrapper
    if mem_args in cache:
TypeError: unhashable type: 'list'
}}}

If the patterns end with slashes, or if APPEND_SLASH is False, the problem goes away.

Here's code with a test example. In the test below, {{{test_hello_2}}} fails with the stack trace above. {{{test_hello_1}}} and {{{test_hello_3}}} pass.

{{{#!python
from django.test import TestCase
from django.test.utils import override_settings

def view_func(r):
    from django.http import HttpResponse
    return HttpResponse('Hello, World!')

class UrlTestOverriddenExplicit(TestCase):
    from django.conf.urls import patterns, url
    urls = patterns('',
        url(r'^hello/$', view_func),
    )

    def test_hello_1(self):
        resp = self.client.get('/hello/')
        self.assertEquals(resp.status_code, 200)

class UrlTestOverriddenExplicitWithoutSlash(TestCase):
    from django.conf.urls import patterns, url
    urls = patterns('',
        url(r'^hello$', view_func),
    )

    def test_hello_2(self):
        resp = self.client.get('/hello')
        self.assertEquals(resp.status_code, 200)

    @override_settings(APPEND_SLASH=False)
    def test_hello_3(self):
        resp = self.client.get('/hello')
        self.assertEquals(resp.status_code, 200)
}}}"	Bug	closed	Uncategorized	1.6	Normal	invalid			Unreviewed	0	0	0	0	0	0
