1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

fix url parser for urls ending in slash

This commit is contained in:
Kelly Brazil
2022-07-23 13:33:12 -07:00
parent 8b3228797e
commit 73d0bb8bbd
6 changed files with 39 additions and 9 deletions

View File

@ -1,5 +1,8 @@
jc changelog jc changelog
20220723 v1.20.4
- Fix URL string parser path list for URLs ending in a forward slash
20220723 v1.20.3 20220723 v1.20.3
- Add URL string parser - Add URL string parser
- Add Email Address string parser - Add Email Address string parser

View File

@ -6,7 +6,7 @@ import importlib
from typing import Dict, List, Iterable, Union, Iterator from typing import Dict, List, Iterable, Union, Iterator
from jc import appdirs from jc import appdirs
__version__ = '1.20.3' __version__ = '1.20.4'
parsers = [ parsers = [
'acpi', 'acpi',

View File

@ -318,14 +318,14 @@ def parse(
encoded_path_list = encoded_path.replace('/', '', 1).split('/') encoded_path_list = encoded_path.replace('/', '', 1).split('/')
decoded_path_list = decoded_path.replace('/', '', 1).split('/') decoded_path_list = decoded_path.replace('/', '', 1).split('/')
if path_list == ['']: if path_list[-1] == '':
path_list = None path_list.pop()
if encoded_path_list == ['']: if encoded_path_list[-1] == '':
encoded_path_list = None encoded_path_list.pop()
if decoded_path_list == ['']: if decoded_path_list[-1] == '':
decoded_path_list = None decoded_path_list.pop()
if normalized.query: if normalized.query:
query_obj = parse_qs(normalized.query) query_obj = parse_qs(normalized.query)

View File

@ -1,4 +1,4 @@
.TH jc 1 2022-07-23 1.20.3 "JSON Convert" .TH jc 1 2022-07-23 1.20.4 "JSON Convert"
.SH NAME .SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools and file-types \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools and file-types
.SH SYNOPSIS .SH SYNOPSIS

View File

@ -5,7 +5,7 @@ with open('README.md', 'r') as f:
setuptools.setup( setuptools.setup(
name='jc', name='jc',
version='1.20.3', version='1.20.4',
author='Kelly Brazil', author='Kelly Brazil',
author_email='kellyjonbrazil@gmail.com', author_email='kellyjonbrazil@gmail.com',
description='Converts the output of popular command-line tools and file-types to JSON.', description='Converts the output of popular command-line tools and file-types to JSON.',

View File

@ -92,5 +92,32 @@ class MyTests(unittest.TestCase):
self.assertEqual(jc.parsers.url.parse(data, quiet=True), expected) self.assertEqual(jc.parsers.url.parse(data, quiet=True), expected)
def test_http_path_ends_in_slash(self):
"""
Test HTTP URL with a forward slash as the last part of the path
"""
data = r'https://pypi.org/project/jc/1.20.3/'
expected = json.loads(r'''{"url":"https://pypi.org/project/jc/1.20.3/","scheme":"https","netloc":"pypi.org","path":"/project/jc/1.20.3/","path_list":["project","jc","1.20.3"],"query":null,"query_obj":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null,"encoded":{"url":"https://pypi.org/project/jc/1.20.3/","scheme":"https","netloc":"pypi.org","path":"/project/jc/1.20.3/","path_list":["project","jc","1.20.3"],"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null},"decoded":{"url":"https://pypi.org/project/jc/1.20.3/","scheme":"https","netloc":"pypi.org","path":"/project/jc/1.20.3/","path_list":["project","jc","1.20.3"],"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null}}''')
self.assertEqual(jc.parsers.url.parse(data, quiet=True), expected)
def test_http_path_only_slash(self):
"""
Test HTTP URL with a forward slash as the last only part of the path
"""
data = r'https://pypi.org/'
expected = json.loads(r'''{"url":"https://pypi.org/","scheme":"https","netloc":"pypi.org","path":"/","path_list":null,"query":null,"query_obj":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null,"encoded":{"url":"https://pypi.org/","scheme":"https","netloc":"pypi.org","path":"/","path_list":null,"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null},"decoded":{"url":"https://pypi.org/","scheme":"https","netloc":"pypi.org","path":"/","path_list":null,"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null}}''')
self.assertEqual(jc.parsers.url.parse(data, quiet=True), expected)
def test_http_path_no_end_slash(self):
"""
Test HTTP URL with no forward slash at the end
"""
data = r'https://pypi.org'
expected = json.loads(r'''{"url":"https://pypi.org","scheme":"https","netloc":"pypi.org","path":null,"path_list":null,"query":null,"query_obj":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null,"encoded":{"url":"https://pypi.org","scheme":"https","netloc":"pypi.org","path":null,"path_list":null,"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null},"decoded":{"url":"https://pypi.org","scheme":"https","netloc":"pypi.org","path":null,"path_list":null,"query":null,"fragment":null,"username":null,"password":null,"hostname":"pypi.org","port":null}}''')
self.assertEqual(jc.parsers.url.parse(data, quiet=True), expected)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()