Background: I'm using the middleware django-multihost
(http://effbot.org/zone/django-multihost.htm) to allow my single django app to respond to different hostnames from the same project. The middleware changes the ROOT_URLCONF
(i.e. which urls.py
file) based on the Host:
HTTP request header.
This works great, but I want to write some simple integration tests that check that the pages are loading properly. Here's an example of a basic test that checks if the /trends
page loads and that it has the text "Trends for Today" on it:
def test_homepage_loads(self):
client = Client()
client.login(username = 'testing', password = 'testing')
page = client.get("/trends", follow = follow_redirects)
self.assertEquals(page.status_code, 200)
self.assertTrue( page.content.find('Trends for Today') > 0 )
Problem is, this always loads using the default ROOT_URLCONF rather than the one the Middleware would invoke. Doesn't matter if I explicitly put the hostname into the url as in client.get("http://secondarysite/trends")
.
How can I test on the other virtual sites with the django test client? I'd like to invoke the middleware in the test so I can test that logic. But if I need to I'd do something hacky like set ROOT_URLCONF for the duration of the tests, but I'm not sure how to do that.