在Django测试框架中使用基本HTTP访问身份验证 [英] Using Basic HTTP access authentication in Django testing framework
本文介绍了在Django测试框架中使用基本HTTP访问身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于某些Django视图,我创建了一个执行基本HTTP访问身份验证的装饰器。但是,在用Django编写测试用例时,我花了一些时间来弄清楚如何对视图进行身份验证。这是我的方法。我希望有人会觉得这有用。
For some of my Django views I've created a decorator that performs Basic HTTP access authentication. However, while writing test cases in Django, it took me a while to work out how to authenticate to the view. Here's how I did it. I hope somebody finds this useful.
推荐答案
这是我的操作方式:
from django.test import Client
import base64
auth_headers = {
'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode('username:password'),
}
c = Client()
response = c.get('/my-protected-url/', **auth_headers)
注意:您还需要创建一个用户。
Note: You will also need to create a user.
这篇关于在Django测试框架中使用基本HTTP访问身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文