Add test for escape_path.

This commit is contained in:
Jason R. Coombs 2021-01-30 11:32:12 -05:00
parent 2bd244dde9
commit 210eb5b529

View File

@ -1,3 +1,5 @@
# coding: utf-8
from __future__ import print_function
import os, sys, urllib, textwrap
@ -270,6 +272,16 @@ def get_alias(aliases, path_unicode, default):
return uri.from_string_dirnode(aliases[alias]).to_string(), path[colon+1:]
def escape_path(path):
# type: (str) -> str
"""
Return path quoted to US-ASCII.
>>> path = u'/føö/bar/☃'
>>> escape_path(path)
'/f%C3%B8%C3%B6/bar/%E2%98%83'
>>> escape_path(path).encode('ascii')
b'/f%C3%B8%C3%B6/bar/%E2%98%83'
"""
# this always returns bytes, specifically US-ASCII, valid URL characters
segments = path.split("/")
return "/".join([urllib.quote(unicode_to_url(s)) for s in segments])