I am trying to add a mock API call for requests.post external API call. I tried few things but I get following response for print(response)
How can i get 200 for response.status_code from the mock?
Sample code to provide the gist of the issue
<code> @action(detail=True, methods=["post"], url_path="change")
def change_user(self, request, **kwargs):
try:
url = 'https://www.w3schools.com/python/demopage.php'
myobj = {'somekey': 'somevalue'}
response = requests.post(url, json = myobj)
print(response)
if 200 <= response.status_code <= 299:
return Response(
response.text,
status=response.status_code,
)
else:
return Response(
"Internal Server Error",
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except Exception as e:
LOG.exception("exception processing request ", reason=e)
raise e
@mock.patch("requests.post")
def test(self, mock_post):
mock_response = requests.Response()
mock_response.status_code = 200
mock_post.return_value = mock_response
response = self.api_client.post("/change")
self.assertEqual(200, response.status_code, response.json())
</code>
<code> @action(detail=True, methods=["post"], url_path="change")
def change_user(self, request, **kwargs):
try:
url = 'https://www.w3schools.com/python/demopage.php'
myobj = {'somekey': 'somevalue'}
response = requests.post(url, json = myobj)
print(response)
if 200 <= response.status_code <= 299:
return Response(
response.text,
status=response.status_code,
)
else:
return Response(
"Internal Server Error",
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except Exception as e:
LOG.exception("exception processing request ", reason=e)
raise e
@mock.patch("requests.post")
def test(self, mock_post):
mock_response = requests.Response()
mock_response.status_code = 200
mock_post.return_value = mock_response
response = self.api_client.post("/change")
self.assertEqual(200, response.status_code, response.json())
</code>
@action(detail=True, methods=["post"], url_path="change")
def change_user(self, request, **kwargs):
try:
url = 'https://www.w3schools.com/python/demopage.php'
myobj = {'somekey': 'somevalue'}
response = requests.post(url, json = myobj)
print(response)
if 200 <= response.status_code <= 299:
return Response(
response.text,
status=response.status_code,
)
else:
return Response(
"Internal Server Error",
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except Exception as e:
LOG.exception("exception processing request ", reason=e)
raise e
@mock.patch("requests.post")
def test(self, mock_post):
mock_response = requests.Response()
mock_response.status_code = 200
mock_post.return_value = mock_response
response = self.api_client.post("/change")
self.assertEqual(200, response.status_code, response.json())