I want to mock ConnectionError for my API. I'm creating a python package for one software which if running on localhost:8080
then it will give result correctly. But if the software is not running, then I want to catch it. I'm able to write function using httpx.ConnectError
to get this exception, but how can I write test for the same ?
my function is like this
def get_workspace(self, workspace: str) -> Union[WorkspaceModel, GSResponse]:
try:
Client = self.http_client
responses = Client.get(f"workspaces/{workspace}")
if responses.status_code == 200:
return WorkspaceModel.parse_obj(responses.json())
else :
results = self.response_recognise(responses)
return results
except httpx.TimeoutException as exc:
res = {}
res['code'] = 504
res['response'] = "Timeout Error"
return GSResponse.parse_obj(res)
except httpx.NetworkError as exc:
res = {}
res['code'] = 503
res['response'] = "Geoserver unavailable"
return GSResponse.parse_obj(res)