pyresttest - 无法从 DELETE 请求中读取正文

pyresttest - Unable to read body from a DELETE request

考虑以下用于从数据库中删除实体的模板。

- test:
    - group: "Basic Test"
    - name: "Delete 10.24.39.202"
    - url: "/v1/switch"
    - method: 'DELETE'
    - expected_status: [200]
    - body: '{"ip_address": "10.24.39.202"}'
    - headers: {'Authorization': 'Basic ZGV2ZWw6WjNGSmVFTlliMUJvYlVSclVrOWhXRVp3ZDIwNFUzQktSekpzTjBnMGVYVT0=',
                'Content-Type': 'application/json'}

这不起作用,解码正文时出现以下错误:

  Traceback (most recent call last):
  File "/home/stack/git/bwc/bwc/server/switch.py", line 1880, in delete
    request = json.loads(self.request.body)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

根据更新日志,pyresttest 支持删除请求中的正文

1.6.0 Mon Oct 12 07:30:00 2015 -0400
BETA: support setting request body on all request types, if present
Allows (for example) DELETE methods that set a request body
Caveat: does not set length if 0

我确定我使用的是 1.6.0 版本。可能是什么问题?

您不应该使用 DELETE 请求发送负载。来自 RFC 7231 § 4.3.5:

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

即使 pyresttest 支持它,它也是非标准的并且正在使用的服务器/代理可能不支持它。

这是测试框架 (PyRestTest) 中的 honest to goodness bug,现已修复。根本原因是 PyCurl 想要使用与 POST 请求相同的方式设置 DELETE(大小来自 POSTFIELDSIZE),而不是像 PUT (INFIELDSIZE) 那样。