I've got a GAE/Django project and I'm trying to make a functional-test enviroment work with WebTest, the project layout is as follows:
/gaeroot
/djangoroot
wsgi.py
urls.py
...
/anapp
urls.py
...
/tests
test_functional.py
wsgi.py (generated by GAE's version of django-admin.py django1.5):
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoroot.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
test_functional.py:
import unittest
import webtest
from djangoroot.wsgi import application
class TestHomePage(unittest.TestCase):
def setUp(self):
self.testapp = webtest.TestApp(application)
def test_get_method_ok(self):
response = self.testapp.get('/path')
self.assertEqual(response.status_int, 200, response)
The failing test message:
Traceback (most recent call last):
...
line 14, in test_get_method_ok
self.assertEqual(response.status_int, 200, response)
AssertionError: Response: 301 MOVED PERMANENTLY
Content-Type: text/html; charset=utf-8
Location: http://localhost:80/path/
Why is throwing a redirect code to the same path, the only thing I can think of is that some code of django is responsible for the redirection because as you can see from the directory tree I have a two level url configuration.
On the other side, why is using port 80? when I test it on the browser it shows an 8080 port, and it shouldn't use a port at all since WebTest
it's supposed not to use a port at all since it's testing the WSGI interface right?.
Top level urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^path/', include('djangoroot.anapp.urls')),
)
App level urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('djangoroot.anapp.views',
url(r'^$', 'home', name='anapp_home'),
)
The browser shows the corret page on the same url, I took the WebTest example from google's support pages, so the problem should be the GAE/Django interop.
Thanks in advance and let me know if you need more info.
The problem seems to be on the django.conf.urls.url
function since I tested the root urls.py
file and it worked for the root path /
with no redirection, but it did redirected me with a path other than the root, I could find nothing that seemed being redirecting my urls on the Django source files.
I found an alternative on the Webtest
documentation:
resp = self.testapp.get('/path')
resp = resp.maybe_follow()
with the maybe_follow
method you eventually get the final page.
Edit
Finally I found the problem in this line:
response = self.testapp.get('/path')
replace it with this:
response = self.testapp.get('/path/')
It looks like Django
redirects the urls to the propper path with the /
at the end.