Python Django:测试页面是否已重定向到所需的 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14951356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django : Testing if the page has redirected to the desired url
提问by Deepankar Bajpeyi
In my django app, I have an authentication system. So, If I do not log in and try to access some profile's personal info, I get redirected to a login page.
在我的 django 应用程序中,我有一个身份验证系统。因此,如果我不登录并尝试访问某些个人资料的个人信息,我将被重定向到登录页面。
Now, I need to write a test case for this. The responses from the browsers I get is :
现在,我需要为此编写一个测试用例。我得到的浏览器的响应是:
GET /myprofile/data/some_id/ HTTP/1.1 302 0
GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 301 0
GET /account/login?next=/myprofile/data/some_id/ HTTP/1.1 200 6533
How do I write my test ? This what I have so far:
我该如何编写测试?这是我到目前为止所拥有的:
self.client.login(user="user", password="passwd")
response = self.client.get('/myprofile/data/some_id/')
self.assertEqual(response.status,200)
self.client.logout()
response = self.client.get('/myprofile/data/some_id/')
What could possibly come next ?
接下来可能会发生什么?
采纳答案by imposeren
Django 1.4:
Django 1.4:
https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.TestCase.assertRedirects
https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.TestCase.assertRedirects
Django 2.0:
Django 2.0:
SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='', fetch_redirect_response=True)Asserts that the response returned a status_coderedirect status, redirected to expected_url(including any GETdata), and that the final page was received with target_status_code.
If your request used the followargument, the expected_urland target_status_codewill be the url and status code for the final point of the redirect chain.
If fetch_redirect_responseis False, the final page won't be loaded. Since the test client can't fetch external URLs, this is particularly useful if expected_urlisn't part of your Django app.
Scheme is handled correctly when making comparisons between two URLs. If there isn't any scheme specified in the location where we are redirected to, the original request's scheme is used. If present, the scheme in expected_urlis the one used to make the comparisons to.
SimpleTestCase.assertRedirects(response, expected_url, status_code=302, target_status_code=200, msg_prefix='', fetch_redirect_response=True)断言响应返回status_code重定向状态,重定向到expected_url(包括任何GET数据),并且最终页面是通过target_status_code接收的。
如果您的请求使用了follow参数,则expected_url和target_status_code将是重定向链终点的 url 和状态代码。
如果fetch_redirect_response为False,则不会加载最终页面。由于测试客户端无法获取外部 URL,因此如果expected_url不是您的 Django 应用程序的一部分,这将特别有用。
在两个 URL 之间进行比较时,Scheme 处理正确。如果在我们重定向到的位置中没有指定任何方案,则使用原始请求的方案。如果存在,则expected_url 中的方案是用于进行比较的方案。
回答by luc
You can check response['Location']and see if it matchs with the expected url. Check also that status code is 302.
您可以检查response['Location']它是否与预期的 url 匹配。还要检查状态代码是否为 302。
回答by igniteflow
You could also follow the redirect with:
您还可以按照以下重定向:
response = self.client.get('/myprofile/data/some_id/', follow=True)
which would mirror the user experience in the browser and make assertions of what you expect to find there, such as:
这将反映浏览器中的用户体验并断言您希望在那里找到的内容,例如:
self.assertContains(response, "You must be logged in", status_code=401)
回答by Alan Viars
response['Location']doesn't exist in 1.9. Use this instead:
response['Location']1.9 中不存在。改用这个:
response = self.client.get('/myprofile/data/some_id/', follow=True)
last_url, status_code = response.redirect_chain[-1]
print(last_url)

