简体   繁体   English

Django测试客户端和子域

[英]Django Test Client and Subdomains

I'm trying to figure out how to make the Django test client play nice with my app that puts each user on it's own subdomain. 我正在试图弄清楚如何让Django测试客户端与我的应用程序配合使用,将每个用户放在它自己的子域上。 ie each account has account1.myapp.com, account2.myapp.com. 即每个帐户都有account1.myapp.com,account2.myapp.com。

A user could be members of multiple subdomains (similar basecamp's model) so i handle which subdomain the request is being issued against in middleware. 用户可以是多个子域的成员(类似于basecamp的模型),因此我处理在中间件中发出请求的子域。

As I'm writing my unit tests, I realized that all requests are issued to "http://testserver" which my middleware then redirects and the subsequent 302 is not followed as it's determined to be an external request. 在我编写单元测试时,我意识到所有请求都发送到“http:// testserver”,然后我的中间件重定向,后续的302没有被跟踪,因为它确定是外部请求。

Anyone aware of a way to enable this with the test client? 有人知道如何通过测试客户端启用此功能吗? I'm currently hacking a bit in django to enable it. 我目前正在django中进行一些黑客攻击以启用它。

in your tests, when using the client, add the HTTP_HOST parameter: 在测试中,使用客户端时,添加HTTP_HOST参数:

response = c.post(reverse('my-url'), data={}, HTTP_HOST='account1.myapp.com')

on your middleware now you should see the host changed! 在您的中间件上,您应该看到主机已更改!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM