我正在尝试使用tastepie 创建一个POST api。当我从邮递员那里运行时,它工作正常。但是当我为它创建 django 测试时,数据没有进来,request.POST
而是进来了request.body
。有没有一种类似于使用 django 测试的邮递员的方法来测试它?我知道它正在进来,request.body
但为什么它是从邮递员那里工作的?我也跟着这个(https://django-tastypie.readthedocs.org/en/latest/testing.html)但同样的问题。
api.py
class CustResource(Resource):
class Meta:
resource_name = 'customer'
authentication = ApiKeyAuthentication()
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/register%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('register_me'), name="api_register_me")
]
def register_me(self, request, **kwargs):
self.method_check(request, allowed=['post'])
self.is_authenticated(request)
response = {"status": 'error', 'data':{'error_code': '9999'},
'message':'some issue'}
return self.create_response(request, response)
测试.py
class RestApiTests(TestCase):
def setUp(self):
"""
create
"""
self.user = User.objects.create_user(username="anuj",email="anuj@gmail.com",password="testing")
self.apikey = ApiKey.objects.get(user=self.user)
def test1(self):
"""
"""
client = Client()
json_str = json.dumps({'format':'json','username':'anuj','api_key':'fc32bd921bb6386c945229e675704818b29dadf0'})
response = client.post('/api/v1/customer/register/',data=json_str, content_type='application/json')
print response.status_code
self.assertEqual(response.status_code, 200, msg="Wrong status code")