使用多主机中间件测试 django 页面

发布于 2024-10-01 09:35:40 字数 898 浏览 2 评论 0原文

背景:我正在使用中间件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦等七年七年为一梦 2024-10-08 09:35:40

使用 Django 客户端,您可以像这样设置 HTTP 标头:

client.get(url, HTTP_HOST = 'my.host.com')

基本上,get 函数采用参数字典,它将用作 HTTP 标头。要遵守 DRY,您可以在 unittest 类的 setup() 中执行类似的操作:

self.request_headers = { 'HTTP_HOST': 'foo' }

在您的测试方法中:

client.get(url, **self.request_headers)

注意:我不确定这是否适用于您的问题。然而,这是我能想到的设置标题的唯一方法。

我能想到的另一个解决方案是在测试类上设置 URLConf,如下所示:

class Mytest(TestCase):
    urls = 'my.urlconf'

但是,这不会测试中间件。

Using the Django Client you can set HTTP headers like this:

client.get(url, HTTP_HOST = 'my.host.com')

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 in setup() of the unittest class:

self.request_headers = { 'HTTP_HOST': 'foo' }

And in your test method:

client.get(url, **self.request_headers)

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:

class Mytest(TestCase):
    urls = 'my.urlconf'

This would however not test the middleware.

半世晨晓 2024-10-08 09:35:40

http://docs.djangoproject.com/ en/dev/topics/testing/#django.test.client.Client.get

The extra keyword arguments parameter can be used to specify headers to be sent in the request

因此,您可能会尝试覆盖 HTTP_HOST 标头。

http://docs.djangoproject.com/en/dev/topics/testing/#django.test.client.Client.get:

The extra keyword arguments parameter can be used to specify headers to be sent in the request

So, you might try to override HTTP_HOST header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文