async def test_post_request(aresponses): """Test POST requests are handled correctly.""" aresponses.add(MATCH_HOST, "/api/post", "POST", aresponses.Response(status=200, text="OK")) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) response = await client._request("post", method="POST") assert response == "OK"
async def test_text_request(aresponses): """Test non JSON response is handled correctly.""" aresponses.add( MATCH_HOST, "/api/text", "GET", aresponses.Response(status=200, text="OK"), ) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) response = await client._request("text") assert response == "OK"
async def test_http_error500(aresponses): """Test HTTP 500 response handling.""" aresponses.add( MATCH_HOST, "/api/system/status", "GET", aresponses.Response(text="Internal Server Error", status=500), ) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) with pytest.raises(RadarrError): assert await client._request("system/status")
async def test_http_error404(aresponses): """Test HTTP 404 response handling.""" aresponses.add( MATCH_HOST, "/api/system/status", "GET", aresponses.Response(text="Not Found!", status=404), ) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) with pytest.raises(RadarrResourceNotFound): assert await client._request("system/status")
async def test_http_error403(aresponses): """Test HTTP 403 response handling.""" aresponses.add( MATCH_HOST, "/api/system/status", "GET", aresponses.Response(text="Forbidden", status=403), ) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) with pytest.raises(RadarrAccessRestricted): assert await client._request("system/status")
async def test_request_base_path(aresponses): """Test API running on different base path.""" aresponses.add( MATCH_HOST, "/api/v3/system/status", "GET", aresponses.Response(text="GOTCHA!", status=200), ) async with ClientSession() as session: client = Client(HOST, API_KEY, base_path="/api/v3", session=session) response = await client._request("system/status") assert response == "GOTCHA!"
async def test_timeout(aresponses): """Test request timeout from the API.""" # Faking a timeout by sleeping async def response_handler(_): await asyncio.sleep(2) return aresponses.Response(body="Timeout!") aresponses.add(MATCH_HOST, "/api/system/status", "GET", response_handler) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session, request_timeout=1) with pytest.raises(RadarrConnectionError): assert await client._request("system/status")
async def test_internal_session(aresponses): """Test JSON response is handled correctly with internal session.""" aresponses.add( MATCH_HOST, "/api/system/status", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text='{"status": "OK"}', ), ) async with Client(HOST, API_KEY) as client: response = await client._request("system/status") assert response["status"] == "OK"
async def test_http_error500_json(aresponses): """Test HTTP 500 json response handling.""" aresponses.add( MATCH_HOST, "/api/system/status", "GET", aresponses.Response( status=500, headers={"Content-Type": "application/json"}, body='{"status": "NOK"}', ), ) async with ClientSession() as session: client = Client(HOST, API_KEY, session=session) with pytest.raises(RadarrError): response = await client._request("system/status") assert response assert response["status"] == "NOK"
async def test_request_port(aresponses): """Test the handling of non-standard API port.""" aresponses.add( f"{HOST}:{NON_STANDARD_PORT}", "/api/system/status", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text='{"status": "OK"}', ), ) async with ClientSession() as session: client = Client(host=HOST, api_key=API_KEY, port=NON_STANDARD_PORT, session=session) response = await client._request("system/status") assert response["status"] == "OK"
async def test_client_error(): """Test HTTP client error.""" async with ClientSession() as session: client = Client("#", API_KEY, session=session) with pytest.raises(RadarrConnectionError): assert await client._request("system/status")