【问题标题】:Setting remote_addr in flask test_request_context在烧瓶 test_request_context 中设置 remote_addr
【发布时间】:2017-08-09 18:10:35
【问题描述】:

如何在烧瓶 test_request_context 中设置 remote_addr 属性?这个问题:Get IP Address when testing flask application through nosetests(和其他类似的问题)解释了如何使用 get/post/whatever 测试客户端调用来做到这一点,但我没有使用那些(在这种情况下)。相反,我得到一个 test_request_context 然后调用一个函数,从而允许我单独测试由我的视图函数调用的函数。

编辑:澄清一下,我的测试代码如下所示:

with app.test_request_context():
    result=my_function_which_expects_to_be_called_from_a_request_context()
<check result however>

所以我绝不会使用测试客户端调用。

【问题讨论】:

    标签: python testing flask


    【解决方案1】:

    将相同的参数传递给test_request_context,就像你传递给client.get 一样。两者都以相同的方式在内部设置 WSGI 环境。

    with app.test_request_context(environ_base={'REMOTE_ADDR': '10.1.2.3'}):
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多