Skip to content

Commit e899b31

Browse files
authored
Merge pull request #54 from zivy/updateTesting
Update URL testing.
2 parents ccc2d8c + 64f2994 commit e899b31

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

tests/test_notebooks.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from enchant import DictWithPWL
1212

1313
from lxml.html import document_fromstring, etree
14-
from urllib.request import urlopen, URLError
14+
from urllib.request import urlopen, URLError, Request
1515

1616

1717
"""
@@ -141,6 +141,13 @@ def static_analysis(self, path):
141141
#######################
142142
# Check that all the links in the markdown cells are valid/accessible.
143143
#######################
144+
145+
# Some sites require a user-agent header as they block requests from scripts. Provide a common user-agent so that we can
146+
# check the links are accessible.
147+
headers = {
148+
"User-Agent": "Mozilla/5.0 (Macintosh; ARM64 Mac OS X 14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
149+
}
150+
144151
no_broken_links = True
145152

146153
cells_and_broken_links = []
@@ -154,12 +161,13 @@ def static_analysis(self, path):
154161
if (
155162
"http" not in document_link[2]
156163
): # Local file (url uses forward slashes, windows backwards).
157-
url = "file:///" + os.path.abspath(
158-
document_link[2]
159-
).replace("\\", "/")
164+
request = Request(
165+
"file:///"
166+
+ os.path.abspath(document_link[2]).replace("\\", "/")
167+
)
160168
else: # Remote file.
161-
url = document_link[2]
162-
urlopen(url)
169+
request = Request(document_link[2], headers=headers)
170+
urlopen(request)
163171
except URLError:
164172
broken_links.append(url)
165173
if broken_links:

0 commit comments

Comments
 (0)