def assertResultCount(self, test_file, expected_num): with requests_mock.Mocker() as m: InvalidLinkBear.check_prerequisites = lambda *args: True uut = InvalidLinkBear(self.section, Queue()) m.add_matcher(custom_matcher) for line, num in zip(test_file, expected_num): outputs = list(uut.run('testline', [line])) self.assertEqual(num, len(outputs))
def test_run(self): # Valid Links valid_file = """ http://httpbin.org/status/200 http://httpbin.org/status/201 http://httpbin.org/status/401 # Unauthorized # Parentheses https://en.wikipedia.org/wiki/Hello_(Adele_song)/200 # Quotes "https://github.com/coala/coala-bears/issues/200" 'http://httpbin.org/status/203' ('http://httpbin.org/status/200').install_command() `https://coala.io/200` # Markup/down stuff <http://httpbin.org/status/202> http://httpbin.org/status/204..... [httpbin](http://httpbin.org/status/200) [http://httpbin.org/status/200](http://httpbin.org/status/200) |http://httpbin.org/status/200| <h3>Something http://httpbin.org/status/200</h3> repo=\\"http://httpbin.org/status/200\\" # Templated URLs can't be checked "http://httpbin.org/{status}/404".format(...) "http://httpbin.org/$status/404" # Not a link http://not a link dot com http://www.%s.com http://www.%d.com http://www.%f.com # Redirect http://httpbin.org/status/301 http://httpbin.org/status/302 # Example.com URLs should be ignored http://sub.example.com/404 """.splitlines() self.assertResult(valid_file=valid_file) invalid_file = """ http://coalaisthebest.com/ http://httpbin.org/status/404 http://httpbin.org/status/410 http://httpbin.org/status/500 http://httpbin.org/status/503 """.splitlines() with requests_mock.Mocker() as m: m.add_matcher(custom_matcher) self.check_line_result_count( InvalidLinkBear(self.section, Queue()), invalid_file, [1, 1, 1, 1, 1])
def test_variable_timeouts(self): nt = { 'https://google.com/timeout/test/2/3/4/5/something': 10, 'https://facebook.com/timeout': 2, '*': 25 } file_contents = """ https://facebook.com/ https://google.com/ https://coala.io/som/thingg/page/123 """.splitlines() def response(status_code, *args, **kwargs): res = requests.Response() res.status_code = status_code return res with unittest.mock.patch( 'tests.general.InvalidLinkBearTest.requests.head', return_value=response(status_code=200)) as mock: uut = InvalidLinkBear(self.section, Queue()) self.assertEqual([x.message for x in list(uut.run('file', file_contents, network_timeout=nt))], []) with self.assertLogs(logging.getLogger()) as log: self.assertEqual([x.message for x in list(uut.run('file', file_contents, timeout=20))], []) self.assertEqual(log.output, ['WARNING:root:The setting `timeout` is ' 'deprecated. Please use `network_timeout` ' 'instead.']) self.assertEqual([x.message for x in list(uut.run('file', ['https://gitmate.io']))], []) mock.assert_has_calls([ unittest.mock.call('https://facebook.com/', timeout=2, allow_redirects=False), unittest.mock.call('https://google.com/', timeout=10, allow_redirects=False), unittest.mock.call('https://coala.io/som/thingg/page/123', timeout=25, allow_redirects=False), unittest.mock.call('https://facebook.com/', timeout=20, allow_redirects=False), unittest.mock.call('https://google.com/', timeout=20, allow_redirects=False), unittest.mock.call('https://coala.io/som/thingg/page/123', timeout=20, allow_redirects=False), unittest.mock.call('https://gitmate.io', timeout=15, allow_redirects=False)])
def assertResult(self, valid_file=None, invalid_file=None): with requests_mock.Mocker() as m: InvalidLinkBear.check_prerequisites = lambda *args: True uut = InvalidLinkBear(self.section, Queue()) m.add_matcher(custom_matcher) if valid_file: out = uut.execute("valid", valid_file) self.assertEqual(out, []) if invalid_file: out = uut.execute("invalid", invalid_file) self.assertNotEqual(out, []) self.assertNotEqual(out, None)
def assertResult(self, valid_file=None, invalid_file=None, settings={}): with requests_mock.Mocker() as m: InvalidLinkBear.check_prerequisites = lambda *args: True uut = InvalidLinkBear(self.section, Queue()) m.add_matcher(custom_matcher) if valid_file: out = list(uut.run('valid', valid_file, **settings)) self.assertEqual(out, []) if invalid_file: out = list(uut.run('invalid', invalid_file, **settings)) self.assertNotEqual(out, []) self.assertNotEqual(out, None)
def assertResult(self, valid_file=None, invalid_file=None, settings={}): with requests_mock.Mocker() as m: InvalidLinkBear.check_prerequisites = lambda *args: True uut = InvalidLinkBear(self.section, Queue()) for name, value in settings.items(): self.section.append(Setting(name, value)) m.add_matcher(custom_matcher) if valid_file: out = uut.execute("valid", valid_file) self.assertEqual(out, []) if invalid_file: out = uut.execute("invalid", invalid_file) self.assertNotEqual(out, []) self.assertNotEqual(out, None)
def assertSeverity(self, file, severity, settings={}): """ Test the warnings in each line of the file to match the given severity. :param file: The ``file`` to be checked. :param severity: The severity level of the warnings in each line of the file. """ severity_tag = RESULT_SEVERITY.reverse[severity] with requests_mock.Mocker() as m: uut = InvalidLinkBear(self.section, Queue()) m.add_matcher(custom_matcher) outputs = list(uut.run('testfile', file, **settings)) for out in outputs: out_dict = out.to_string_dict() self.assertEqual(severity_tag, out_dict['severity'])
def test_variable_timeouts(self): nt = { 'https://google.com/timeout/test/2/3/4/5/something': 10, 'https://facebook.com/timeout': 2 } file_contents = """ https://facebook.com/ https://google.com/ https://coala.io/som/thingg/page/123 """.splitlines() def response(status_code, *args, **kwargs): res = requests.Response() res.status_code = status_code return res with unittest.mock.patch( 'tests.general.InvalidLinkBearTest.requests.head', return_value=response(status_code=200)) as mock: uut = InvalidLinkBear(self.section, Queue()) self.assertEqual([ x.message for x in list( uut.run('file', file_contents, network_timeout=nt)) ], []) mock.assert_has_calls([ unittest.mock.call('https://facebook.com/', timeout=2, allow_redirects=False), unittest.mock.call('https://google.com/', timeout=10, allow_redirects=False), unittest.mock.call('https://coala.io/som/thingg/page/123', timeout=15, allow_redirects=False) ])
def setUp(self): self.ub_check_prerequisites = URLBear.check_prerequisites self.section = Section('') URLBear.check_prerequisites = lambda *args: True self.uut = InvalidLinkBear(self.section, Queue())