AsyncHTTPTestCase Post 请求传递数据

bha*_*arc 4 python tornado

尝试POST使用AsyncHTTPTestCasePython 3.4 Tornado 4.1 版测试请求。

from tornado.testing import AsyncHTTPTestCase
from myapp  import myclass

class TestFooHandler(AsyncHTTPTestCase):

    def get_app(self):
        return myclass.application

    def test_post_handler(self):
        import urllib.parse
        post_body = urllib.parse.urlencode({"key":"val"})
        response = self.fetch("/foo", method="POST", data=post_body)
        self.assertEqual(response.code, 200)
Run Code Online (Sandbox Code Playgroud)

代码失败并显示消息: unexpected keyword argument 'data'

这是完整的跟踪:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/testing.py", line 120, in __call__
    result = self.orig_method(*args, **kwargs)
  File "/home/gub/App/unit_tests/test_cors.py", line 22, in test_post_handler
    response = self.fetch("/foo", method="POST", data=post_body)
  File "/usr/local/lib/python3.4/dist-packages/tornado/testing.py", line 380, in fetch
    self.http_client.fetch(self.get_url(path), self.stop, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/tornado/httpclient.py", line 227, in fetch
    request = HTTPRequest(url=request, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'data'
Run Code Online (Sandbox Code Playgroud)

上面的代码有什么不正确的地方?

Ben*_*ell 5

参数名称是body,不是dataself.fetch("/foo", method="POST", body=post_body)