Skip to content

Commit a26dd48

Browse files
committed
Text # quoting in safe_url_string
1 parent bad299a commit a26dd48

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

tests/test_url.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ def test_safe_url_port_number(self):
219219
safe_url_string(u"http://www.example.com:/résumé?q=résumé"),
220220
"http://www.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9")
221221

222+
def test_safe_url_string_preserve_nonfragment_hash(self):
223+
# don't decode `%23` to `#`
224+
self.assertEqual(safe_url_string("http://www.example.com/path/to/%23/foo/bar"),
225+
"http://www.example.com/path/to/%23/foo/bar")
226+
self.assertEqual(safe_url_string("http://www.example.com/path/to/%23/foo/bar#frag"),
227+
"http://www.example.com/path/to/%23/foo/bar#frag")
228+
self.assertEqual(safe_url_string("http://www.example.com/path/to/%23/foo/bar?url=http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2F%23%2Fbar%2Ffoo"),
229+
"http://www.example.com/path/to/%23/foo/bar?url=http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2F%23%2Fbar%2Ffoo")
230+
self.assertEqual(safe_url_string("http://www.example.com/path/to/%23/foo/bar?url=http%3A%2F%2Fwww.example.com%2F%2Fpath%2Fto%2F%23%2Fbar%2Ffoo#frag"),
231+
"http://www.example.com/path/to/%23/foo/bar?url=http%3A%2F%2Fwww.example.com%2F%2Fpath%2Fto%2F%23%2Fbar%2Ffoo#frag")
232+
222233
def test_safe_download_url(self):
223234
self.assertEqual(safe_download_url('http://www.example.org'),
224235
'http://www.example.org/')

w3lib/url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def safe_url_string(url, encoding='utf8', path_encoding='utf8', quote_path=True)
7575

7676
# default encoding for path component SHOULD be UTF-8
7777
if quote_path:
78-
path = quote(to_bytes(parts.path, path_encoding), _safe_chars)
78+
path = quote(to_bytes(parts.path, path_encoding), _path_safe_chars)
7979
else:
8080
path = to_native_str(parts.path)
8181

0 commit comments

Comments
 (0)