I want to mock the requests.Response() object in my unit test, I got a hint from below link.
How to mock data as request.Response type in python
Here, I can just set the status_code
value (which is not a @Property), I want to set the value for @Property text
or content
class 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')
send_request
returns the response