Skip to content

Commit 5f4924f

Browse files
committed
added exception handling for endpoint checking timeouts; added tests for relme code; bumped version for publish
1 parent c7f4705 commit 5f4924f

File tree

8 files changed

+82
-20
lines changed

8 files changed

+82
-20
lines changed

ronkyuu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__email__ = 'bear@bear.im'
99
__copyright__ = 'Copyright (c) 2013-2015 by Mike Taylor and Kartik Prabhu'
1010
__license__ = 'MIT'
11-
__version__ = '0.3.1'
11+
__version__ = '0.3.2'
1212
__url__ = 'https://github.com/bear/ronkyuu'
1313
__download_url__ = 'https://pypi.python.org/pypi/ronkyuu'
1414
__description__ = 'Webmention Manager'

ronkyuu/webmention.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,27 @@ def discoverEndpoint(url, test_urls=True):
132132

133133
# status, webmention
134134
href = None
135-
r = requests.get(url,verify=False)
136-
if r.status_code == requests.codes.ok:
137-
try:
138-
link = parse_link_header(r.headers['link'])
139-
href = link.get('webmention', '') or link.get('http://webmention.org', '') or link.get('http://webmention.org/', '') or link.get('https://webmention.org', '') or link.get('https://webmention.org/', '')
140-
141-
# force searching in the HTML if not found
142-
if not href:
143-
raise AttributeError
144-
except (KeyError, AttributeError):
145-
href = findEndpoint(r.text)
146-
147-
if href is not None:
148-
href = urljoin(url, href)
149-
150-
return (r.status_code, href)
135+
try:
136+
r = requests.get(url, verify=False)
137+
rc = r.status_code
138+
if rc == requests.codes.ok:
139+
try:
140+
link = parse_link_header(r.headers['link'])
141+
href = link.get('webmention', '') or link.get('http://webmention.org', '') or link.get('http://webmention.org/', '') or link.get('https://webmention.org', '') or link.get('https://webmention.org/', '')
142+
143+
# force searching in the HTML if not found
144+
if not href:
145+
raise AttributeError
146+
except (KeyError, AttributeError):
147+
href = findEndpoint(r.text)
148+
149+
if href is not None:
150+
href = urljoin(url, href)
151+
except (requests.exceptions.RequestException, requests.exceptions.ConnectionError,
152+
requests.exceptions.HTTPError, requests.exceptions.URLRequired,
153+
requests.exceptions.TooManyRedirects, requests.exceptions.Timeout):
154+
rc = 500
155+
return (rc, href)
151156

152157

153158
def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchDomain=None):

tests/data/mentions_empty.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mentions_empty.html
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
</head>
6+
<body>
7+
</body>
8+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link href="https://bear.im/webmention" rel="webmention"/>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link href="https://bear.im/webmention" rel="webmention http://webmention.org"/>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link href="https://bear.im/webmention" rel="http://webmention.org/"/>
6+
</head>
7+
<body>
8+
</body>
9+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link href="https://bear.im/webmention" rel="webmention http://webmention.org"/>
6+
</head>
7+
<body>
8+
</body>
9+
</html>

tests/test_mentions.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
import os
34
import unittest
45
from httmock import urlmatch, HTTMock
56

@@ -11,9 +12,21 @@
1112
post_html = ''.join(open('./tests/data/mentions_post.html').readlines())
1213
tantek_html = ''.join(open('./tests/data/mentions_tantek.html').readlines())
1314

14-
event_config = { "handler_path": "./tests/test_event_handlers",
15-
16-
}
15+
path_testdata = './tests/data/'
16+
17+
html = {}
18+
for n in range(0, 4):
19+
f = 'mentions_link_in_head_%02d.html' % n
20+
html[f] = []
21+
with open(os.path.join(path_testdata, f), 'r') as h:
22+
html[f].append(h.read())
23+
24+
with open(os.path.join(path_testdata, 'mentions_empty.html'), 'r') as h:
25+
empty_html = h.read()
26+
27+
@urlmatch(netloc=r'(.*\.)?ronkyuu\.io$')
28+
def link_mock(url, request):
29+
return post_html
1730

1831
@urlmatch(netloc=r'(.*\.)?bear\.im$')
1932
def bear_im_mock(url, request):

0 commit comments

Comments
 (0)