Django测试(django.test.client)拨打电话,许多标头都不起作用

时间:2017-12-08 14:20:16

标签: python django

  

Django(2.0)

     

Python 3.5.2

没有REMOTE_ADDR,效果非常好:

client = Client()

response = client.put('/api/v1/person/address/',{},
    HTTP_API_KEY=self.model_login_director.token)

REMOTE_ADDR来电中的put()无效(但适用于GETPOST次请求)。

client = Client()

response = client.put('/api/v1/person/address/',{},
    REMOTE_ADDR='127.0.0.8',
    HTTP_API_KEY=self.model_login_director.token)

消息错误

Traceback (most recent call last):
      File "/home/wborba/project/willcode/sofia/api/test/testsControllerPersonAddress.py", line 577, in test_person_address_update_address_id_missing
        HTTP_API_KEY=self.model_login_director.token)
      File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 548, in put
        response = super().put(path, data=data, content_type=content_type, secure=secure, **extra)
      File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 366, in put
        secure=secure, **extra)
      File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 404, in generic
        return self.request(**r)
      File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 485, in request
        raise exc_value
      File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
        response = get_response(request)
      File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 128, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 126, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 69, in view
        return self.dispatch(request, *args, **kwargs)
      File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 89, in dispatch
        return handler(request, *args, **kwargs)
      File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
        return view_func(*args, **kwargs)
      File "/usr/lib/python3.5/contextlib.py", line 30, in inner
        return func(*args, **kwds)
      File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 62, in _wrapper
        return bound_func(*args, **kwargs)
      File "/usr/lib/python3.5/contextlib.py", line 30, in inner
        return func(*args, **kwds)
      File "/home/wborba/project/willcode/sofia/api/Business/Auth.py", line 145, in wrap
        return function(request, model_login, *args, **kwargs)
      File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 58, in bound_func
        return func.__get__(self, type(self))(*args2, **kwargs2)
    TypeError: put() takes 2 positional arguments but 3 were given

2 个答案:

答案 0 :(得分:0)

在实例化Client()

时,您应该传递标题信息

来自docs

  

class Client(enforce_csrf_checks=False, **defaults)

     

它不需要   施工时的争论。但是,您可以使用关键字   用于指定某些默认标头的参数。例如,这将发送   每个请求中的User-Agent HTTP标头:

     

c = Client(HTTP_USER_AGENT='Mozilla/5.0')

因此,您应在创建REMOTE_ADDR时传递Client标头:

client = Client(REMOTE_ADDR='127.0.0.8')

response = client.put('/api/v1/person/address/',{},
    HTTP_API_KEY=self.model_login_director.token)

答案 1 :(得分:0)

解决!

这只是我缺乏关注。 在满足PUT请求的方法中,缺少一些必需参数,例如:

def put(self,request,model_login,*args,**kwargs):

我曾与https://docs.djangoproject.com/en/2.0/topics/class-based-views/

合作过