使用多主机中间件测试 django 页面
背景:我正在使用中间件 django-multihost (http://effbot.org/zone/django-multihost.htm) 来允许我的单个 django 应用程序响应同一项目中的不同主机名。中间件根据 Host:
HTTP 请求标头更改 ROOT_URLCONF
(即哪个 urls.py
文件)。
这很好用,但我想编写一些简单的集成测试来检查页面是否正确加载。下面是一个基本测试的示例,用于检查 /trends
页面是否加载以及页面上是否包含“Trends for Today”文本:
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 )
问题是,这始终使用默认的 ROOT_URLCONF 而不是使用默认的 ROOT_URLCONF 加载中间件将调用。如果我像 client.get("http://secondarysite/trends")
那样将主机名显式放入 URL 中,也没关系。
如何使用 django 测试客户端在其他虚拟站点上进行测试?我想在测试中调用中间件,以便我可以测试该逻辑。但如果我需要的话,我会做一些黑客行为,比如在测试期间设置 ROOT_URLCONF ,但我不知道该怎么做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Django 客户端,您可以像这样设置 HTTP 标头:
基本上,
get
函数采用参数字典,它将用作 HTTP 标头。要遵守 DRY,您可以在 unittest 类的setup()
中执行类似的操作:在您的测试方法中:
注意:我不确定这是否适用于您的问题。然而,这是我能想到的设置标题的唯一方法。
我能想到的另一个解决方案是在测试类上设置 URLConf,如下所示:
但是,这不会测试中间件。
Using the Django Client you can set HTTP headers like this:
Basically the
get
functions take a dict of arguments, which it will use as HTTP headers. To adhere to DRY, you could do something like this insetup()
of the unittest class:And in your test method:
Note: I'm not sure if this will work for your problem. This is however the only way I can think of to set the headers.
Another solution I can think of is to set the URLConf on your test class, like this:
This would however not test the middleware.
http://docs.djangoproject.com/ en/dev/topics/testing/#django.test.client.Client.get:
因此,您可能会尝试覆盖 HTTP_HOST 标头。
http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.get:
So, you might try to override HTTP_HOST header.