Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Handle URLs with non-ASCII chars w/ --http #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pdoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
import sys
import warnings
import urllib
from contextlib import contextmanager
from functools import lru_cache
from http.server import BaseHTTPRequestHandler, HTTPServer
Expand Down Expand Up @@ -298,7 +299,7 @@ def import_path_from_req_url(self):
if pth.endswith(suffix):
pth = pth[:-len(suffix)]
break
return pth.replace('/', '.')
return urllib.parse.unquote(pth).replace('/', '.')


def module_path(m: pdoc.Module, ext: str):
Expand Down
13 changes: 12 additions & 1 deletion pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class CliTest(unittest.TestCase):
os.path.join('example_pkg', '_private'),
os.path.join('example_pkg', '_private', 'index.html'),
os.path.join('example_pkg', '_private', 'module.html'),
os.path.join('example_pkg', 'non_äšçii'),
os.path.join('example_pkg', 'non_äšçii', 'index.html'),
os.path.join('example_pkg', 'subpkg'),
os.path.join('example_pkg', 'subpkg', '_private.html'),
os.path.join('example_pkg', 'subpkg', 'index.html'),
Expand Down Expand Up @@ -470,7 +472,7 @@ def setUp(self):

def test_module(self):
modules = {
EXAMPLE_MODULE: ('', ('index', 'module', 'subpkg', 'subpkg2')),
EXAMPLE_MODULE: ('', ('index', 'module', 'non_äšçii', 'subpkg', 'subpkg2')),
EXAMPLE_MODULE + '.subpkg2': ('.subpkg2', ('subpkg2.module',)),
}
with chdir(TESTS_BASEDIR):
Expand Down Expand Up @@ -1683,6 +1685,15 @@ def test_http(self):
html = resp.read()
self.assertIn(b'DictReader', html)

def test_non_ascii_url(self):
from urllib.parse import quote
with self._http([os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)]) as url:
quoted = f'{url}{EXAMPLE_MODULE}/' + quote('non_äšçii')
with urlopen(quoted, timeout=3) as resp:
self.assertEqual(resp.status, 200)
html = resp.read()
self.assertIn('ünicøđe_ftw'.encode('utf-8'), html)

def test_file(self):
with chdir(os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE)):
with self._http(['_relative_import']) as url:
Expand Down
Loading