Skip to content

Commit

Permalink
URLBear: Regex for percentage-encoded URLs
Browse files Browse the repository at this point in the history
Add regex for percentage-encoded URls like
https://img.shields.io/badge/Maintained%3F-yes-green.svg

Fixes #1290
  • Loading branch information
refeed authored and gitmate-bot committed Oct 16, 2017
1 parent ffe13e3 commit 2ab24c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bears/general/URLBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ def extract_links_from_file(file, link_ignore_regex, link_ignore_list):
[^.:%\s_/?#[\]@\\]+ # Initial part of domain
\. # A required dot `.`
(
(?:[^\s()%\'"`<>|\\\[\]]+) # Path name
# This part does not allow
# any parenthesis: balanced or
# unbalanced.
| # OR
\([^\s()%\'"`<>|\\\[\]]*\) # Path name contained within ()
((?:%[A-Fa-f0-9][A-Fa-f0-9])*[^\s()%\'"`<>|\\\[\]]+)
# Path name
# This part allows precentage
# encoding like %3F
# and does not allow
# any parenthesis: balanced or
# unbalanced.
| # OR
\((?:%[A-Fa-f0-9][A-Fa-f0-9])*[^\s()%\'"`<>|\\\[\]]*\)
# Path name contained within ()
# This part allows path names that
# are explicitly enclosed within one
# set of parenthesis.
Expand Down
17 changes: 17 additions & 0 deletions tests/general/URLBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def test_detect_url_result(self):
[3, 'http://www.google.com/404',
404, LINK_CONTEXT.no_context])

def test_precentage_encoded_url(self):
valid_file = """
# A url with a precentage-encoded character in path
https://img.shields.io/badge/Maintained%3F-yes-green.svg/200
""".splitlines()

with requests_mock.Mocker() as m:
m.add_matcher(custom_matcher)

result = get_results(self.uut, valid_file)
self.assertEqual(result[0].contents,
[3,
('https://img.shields.io/badge/Maintained%3F-'
'yes-green.svg/200'),
200,
LINK_CONTEXT.no_context])


class URLResultTest(unittest.TestCase):

Expand Down

0 comments on commit 2ab24c9

Please sign in to comment.