async def mock_fetch_errors(*args, **kwargs): url = args[0] if 'timeout_error' in url: await timeout_mock() elif 'invalid_url' in url: raise InvalidURL(url) elif 'client_error' in url: raise ClientError
def get_redirect_url(resp) -> t.Optional[URL]: url = resp.url r_url = (resp.headers.get(hdrs.LOCATION) or resp.headers.get(hdrs.URI)) if r_url is None: return try: r_url = URL(r_url, encoded=False) except ValueError: raise InvalidURL(r_url) scheme = r_url.scheme if scheme not in ('http', 'https', ''): raise InvalidURL(r_url) elif not scheme: r_url = url.join(r_url) return r_url
async def test_invalid_url(self, mock_aioresponse: aioresponses, capsys: CaptureFixture) -> None: """ An exception can be raised a request is made to an invalid URL. """ url = "foocom" exception = InvalidURL(url=url) mock_aioresponse.get(url, exception=exception) await make_requests_and_print_results(url_list=[url], timeout=1) captured = capsys.readouterr() expected_output = "foocom is an invalid URL\n" assert expected_output in captured.out
async def download_file_from_url(url: Text) -> Text: """Download a story file from a url and persists it into a temp file. Returns the file path of the temp file that contains the downloaded content.""" from rasa.nlu import utils as nlu_utils if not nlu_utils.is_url(url): raise InvalidURL(url) async with aiohttp.ClientSession() as session: async with session.get(url, raise_for_status=True) as resp: filename = nlu_utils.create_temporary_file(await resp.read(), mode="w+b") return filename
async def test_invalid_url_then_valid_url(self, mock_aioresponse: aioresponses, capsys: CaptureFixture) -> None: """ The program can continue making requests in the event of an invalid URL exception. """ invalid_url = "barcom" valid_url = "foo.com" urls = [invalid_url, valid_url] status = 200 exception = InvalidURL(url=invalid_url) mock_aioresponse.get(invalid_url, exception=exception) mock_aioresponse.get(valid_url, status=status) await make_requests_and_print_results(url_list=urls, timeout=1) expected_output_invalid = "barcom is an invalid URL\n" expected_output_valid = "Request to foo.com responded with 200" captured = capsys.readouterr() assert expected_output_invalid in captured.out assert expected_output_valid in captured.out
@pytest.mark.asyncio() @patch( "All_home_works.hw10.task1_get_result.aiohttp.ClientSession.get", return_value=MockResponse(status=200, text="<div>"), ) async def test_fetch_response__works(get): url = "Fake URL" response = await fetch_response(url) assert response == "<div>" @pytest.mark.asyncio() @patch( "All_home_works.hw10.task1_get_result.aiohttp.ClientSession.get", side_effect=InvalidURL("Fake URL"), ) async def test_fetch_response__bad_url(get): url = "Fake URL" with pytest.raises(InvalidURL, match="Fake"): await fetch_response(url) @pytest.mark.asyncio() @patch( "All_home_works.hw10.task1_get_result.fetch_response", return_value='<Valute ID="R01235"><Value>74,1373</Value></Valute>', ) async def test_get_current_course__parse_current_course(get): url = "https://www.cbr.ru/scripts/XML_daily.asp" result = await get_current_course()