每個人。我正在嘗試爲使用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