drt*_*drt 2 python-3.x python-requests
我想在我的单元测试中模拟 requests.Response() 对象,我从下面的链接中得到了一个提示。
如何在python中将数据模拟为request.Response类型
在这里,我可以设置status_code值(不是@Property),我想设置@Property 的值text或content
类 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! csrfKey not found')
Run Code Online (Sandbox Code Playgroud)
send_request 返回 response
rzl*_*vmp 10
text值可以通过使用私有属性来设置_content(它应该是bytes):
import requests as r\n\nres = r.Response()\nres.status_code = 404\n\nutf_string = \'{"error": "page not found", "check UTF8 characters": "\xe3\x81\x82\xe3\x82\xa2"}\'\nbytes_string = utf_string.encode(\'utf-8\')\nres._content = bytes_string\n\nprint(\'Status code:\', res.status_code)\nprint(\'RAW:\', f\'[{type(res.content)}]\', res.content)\nprint(\'Private RAW:\', f\'[{type(res._content)}]\', res._content)\nprint(\'Decoded text:\', f\'[{type(res.text)}]\', res.text)\nprint(\'JSON converted:\', f\'[{type(res.json())}]\', res.json())\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nStatus code: 404\nRAW: [<class \'bytes\'>] b\'{"error": "page not found", "check UTF8 characters": "\\xe3\\x81\\x82\\xe3\\x82\\xa2"}\'\nPrivate RAW: [<class \'bytes\'>] b\'{"error": "page not found", "check UTF8 characters": "\\xe3\\x81\\x82\\xe3\\x82\\xa2"}\'\nDecoded text: [<class \'str\'>] {"error": "page not found", "check UTF8 characters": "\xe3\x81\x82\xe3\x82\xa2"}\nJSON converted: [<class \'dict\'>] {\'error\': \'page not found\', \'check UTF8 characters\': \'\xe3\x81\x82\xe3\x82\xa2\'}\nRun Code Online (Sandbox Code Playgroud)\n
我已经浏览了python文档并弄清楚了......
解决方案是->
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
my_text = """<html>
<body>
<form method="post" name="NavForm">
<input id="csrfKey" name="csrfKey" type="hidden" value="JEK7schtDx5IVcH1eOWKN9lFH7ptcwHD/"/>
</form>
</body>
</html>
"""
type(response).text = mock.PropertyMock(return_value=my_text)
auth_strategy.send_request = mock.MagicMock(return_value=response)
session, auth_result = auth_strategy.do()
self.assertTrue(session, 'Test Failed! Something went wrong')
self.assertTrue('JEK7schtDx5IVcH1eOWKN9lFH7ptcwHD' in auth_result['for_next_url_params']['csrfKey'],
'Test Failed! csrfKey not found')
Run Code Online (Sandbox Code Playgroud)
我添加了PropertyMock周围的文本,代码更改是-->
type(response).text = mock.PropertyMock(return_value=my_text)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2909 次 |
| 最近记录: |