每个人。我正在尝试为使用django-tastypie和http basic auth实现的RESTful API编写测试。所以,我有以下代码:Django测试客户端http基本身份验证用于发布请求
def http_auth(username, password):
credentials = base64.encodestring('%s:%s' % (username, password)).strip()
auth_string = 'Basic %s' % credentials
return auth_string
class FileApiTest(TestCase):
fixtures = ['test/fixtures/test_users.json']
def setUp(self):
self.extra = {
'HTTP_AUTHORIZATION': http_auth('testuser', 'qwerty')
}
def test_folder_resource(self):
response = self.client.get('/api/1.0/folder/', **self.extra)
self.assertEqual(response.status_code, 200)
def test_folder_resource_post(self):
response = self.client.post('/api/1.0/folder/', **self.extra)
self.assertNotEqual(response.status_code, 401)
GET请求已完成,返回状态代码200.但POST请求始终返回401。我确信我做错了什么。有什么建议?
也许检查你的Meta:授权资源?它说什么? – 2011-05-21 14:05:16
'授权= DjangoAuthorization()' – dmrz 2011-05-25 18:19:34
aw,废话,我的意思是“身份验证”这就是你在这里测试。不同之处在于认证==“你是谁”,授权==“你能做到吗?” – 2011-05-25 18:38:32