小编drt*_*drt的帖子

如何为 python 的 requests.Response.text 属性设置值?

我想在我的单元测试中模拟 requests.Response() 对象,我从下面的链接中得到了一个提示。

如何在python中将数据模拟为request.Response类型

在这里,我可以设置status_code值(不是@Property),我想设置@Property 的值textcontent

类 UsernamePasswordAuthStrategyTest(TestCase):

def test_do_success(self):
    content = self._factory.get_reader(ReaderType.CONTENT).read('MY_TEST')
    auth_strategy = UsernamePasswordAuthStrategy(content)
    # mock send_request method response
    response = Response()
    response.status_code = 200
    # How could I achieve below line? 
    response.text = """<html>
            <body>
                <form method="post" name="NavForm">
                    <input id="csrfKey" name="csrfKey" type="hidden" value="JEK7schtDx5IVcH1eOWKN9lFH7ptcwHD/"/>
                </form>
            </body>
        </html>"""
    auth_strategy.send_request = mock.MagicMock(return_value=response)
    session, auth_result = auth_strategy.do()  # {'for_next_url_params': {'csrfKey': 'T4WNcz+hXqrxVa5R9o2HXkDm8pNZEi4k/'}}
    self.assertTrue(session, 'Test Failed! Something went wrong')
    self.assertTrue('for_next_url_params' in auth_result and 'csrfKey' in auth_result['for_next_url_params'],
                    'Test Failed! …
Run Code Online (Sandbox Code Playgroud)

python-3.x python-requests

2
推荐指数
2
解决办法
2909
查看次数

标签 统计

python-3.x ×1

python-requests ×1