def test_connection_error(mock_requests): url = "http://example.com" match = url_re.search(url) assert match mck = MagicMock() logger = MagicMock() assert print_url_title(match=match, message=mck, logger=logger) is None assert logger.warning.called
def test_link_announce_404(mock_requests): url = 'http://example.com' mock_requests.add(mock_requests.GET, url, status=404) match = url_re.search(url) assert match mck = MagicMock() logger = MagicMock() assert print_url_title(match=match, message=mck, logger=logger) is None mck.assert_not_called()
def test_read_timeout(mock_requests): url = "http://example.com" def callback(resp): raise requests.ReadTimeout() mock_requests.add_callback('GET', url, callback) match = url_re.search(url) assert match mck = MagicMock() logger = MagicMock() assert print_url_title(match=match, message=mck, logger=logger) is None logger.debug.assert_called_with('Read timeout reached for %r', url)
def test_search(): for text, out in SEARCH: match = url_re.search(text) assert match and match.group() == out
"http://www.talos.principle": (STD_HTML, "In the beginning were the words"), "http://www.nonexistent.lol": ("", False), "http://www.much-newlines.backslashn": (("\n" * 500) + STD_HTML, "new lines!"), "http://completely.invalid": ("\x01\x01\x02\x03\x05\x08\x13", False), "http://large.amounts.of.text": ( STD_HTML + ("42" * 512 * 4096) + "</body>", "here have a couple megs of text", ), "http://star.trek.the.next.title": (STD_HTML, "47" * 512 * 4096), "http://bare.title": ("<title>{}</title>", "here has title"), } @pytest.mark.parametrize( "match,test_str,res", [(url_re.search(a), b.format(c), c) for a, (b, c) in TESTS.items()], ids=lambda case: str(getattr(case, 'string', case))[:100], ) def test_link_announce(match, test_str, res, mock_requests): mock_requests.add(RequestsMock.GET, match.string, body=test_str, stream=True) mck = MagicMock() logger = MagicMock() print_url_title(match=match, message=mck, logger=logger) if res: if len(res) > MAX_TITLE: res = res[:MAX_TITLE] + " ... [trunc]" mck.assert_called_with("Title: \x02" + res + "\x02") else: mck.assert_not_called()