-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_link_validation.py
94 lines (77 loc) · 3.35 KB
/
test_link_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
"""
import unittest
from mechanical_markdown import MechanicalMarkdown
from fake_http_server import FakeHttpServer
from termcolor import colored
class LinkValidationTests(unittest.TestCase):
def setUp(self):
self.command_ouputs = []
self.server = FakeHttpServer()
self.server.start()
self.host_port = f'localhost:{self.server.get_port()}'
def tearDown(self):
self.server.shutdown_server()
def test_link_validation(self):
test_data = f"""
A link that should work: [Mechanical Markdown](http://{self.host_port}/dapr/mechanical-markdown)
A link that gives other valid status codes: [Mechanical Markdown](http://{self.host_port}/dapr/204)
Relative links not currently supported: [Relative Link](examples/README.md)
"""
self.server.set_response_codes((200, 204))
mm = MechanicalMarkdown(test_data)
success, report = mm.execute_steps(False, validate_links=True)
self.assertTrue(success)
expected_report = f"""
External link validation:
\thttp://{self.host_port}/dapr/mechanical-markdown Status: {colored('200', 'green')}
\thttp://{self.host_port}/dapr/204 Status: {colored('204', 'green')}
"""
self.assertEqual(expected_report, report)
def test_link_validation_fails_for_broken_link(self):
test_data = f"""
A link that should not work: [Mechanical Markdown](http://{self.host_port}/dapr/mechanical-markdown/a_bad_link)
A request to a non-existant host: [Mechanical Markdown](https://0.0.0.0/a_bad_link)
"""
mm = MechanicalMarkdown(test_data)
self.server.set_response_codes((404, ))
success, report = mm.execute_steps(False, validate_links=True, link_retries=1)
self.assertFalse(success)
expected_report = f"""
External link validation:
\thttp://{self.host_port}/dapr/mechanical-markdown/a_bad_link Status: {colored('404', 'red')}
\thttps://0.0.0.0/a_bad_link Status: {colored('Connection Failed', 'red')}
"""
self.assertEqual(expected_report, report)
def test_link_validation_ignores_links_marked_ignore(self):
test_data = f"""
A link that should work: [Mechanical Markdown](http://{self.host_port}/dapr/mechanical-markdown)
<!-- IGNORE_LINKS -->
This link should be ignored: [Mechanical Markdown](https://0.0.0.0/a_bad_link)
<!-- END_IGNORE -->
"""
mm = MechanicalMarkdown(test_data)
self.server.set_response_codes((200, ))
success, report = mm.execute_steps(False, validate_links=True)
self.assertTrue(success)
expected_report = f"""
External link validation:
\thttp://{self.host_port}/dapr/mechanical-markdown Status: {colored('200', 'green')}
\thttps://0.0.0.0/a_bad_link Status: {colored('Ignored', 'yellow')}
"""
self.assertEqual(expected_report, report)
def test_failed_links_are_retried(self):
test_data = f"""
A flaky link: [Mechanical Markdown](http://{self.host_port}/dapr/mechanical-markdown)
"""
mm = MechanicalMarkdown(test_data)
self.server.set_response_codes((503, 503, 200))
success, report = mm.execute_steps(False, validate_links=True)
self.assertTrue(success)
expected_report = f"""
External link validation:
\thttp://{self.host_port}/dapr/mechanical-markdown Status: {colored('200', 'green')}
"""
self.assertEqual(expected_report, report)