django tastypie - 发布请求测试不起作用

时间:2015-03-21 10:39:29

标签: python django tastypie django-tests

我正在尝试使用tastypie创建一个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)

tests.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")

0 个答案:

没有答案