Add merge_paths function and tests for it

This commit is contained in:
Ian Cordasco
2014-07-26 11:26:55 -05:00
parent 958c9cf738
commit 40fb0ee57b
2 changed files with 58 additions and 0 deletions

View File

@@ -197,3 +197,14 @@ hier_part = '(//%s%s|%s|%s|%s)' % (
ABSOLUTE_URI_MATCHER = re.compile('^%s:%s(\?%s)$' % (
component_pattern_dict['scheme'], hier_part, QUERY_MATCHER.pattern
))
# Path merger as defined in http://tools.ietf.org/html/rfc3986#section-5.2.3
def merge_paths(base_uri, relative_path):
"""Merge a base URI's path with a relative URI's path."""
if base_uri.path is None and base_uri.authority is not None:
return '/' + relative_path
else:
path = base_uri.path or ''
index = path.rfind('/')
return path[:index] + '/' + relative_path

47
tests/test_misc.py Normal file
View File

@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
from rfc3986.uri import URIReference
from rfc3986.misc import merge_paths
def test_merge_paths_with_base_path_without_base_authority():
"""Demonstrate merging with a base URI without an authority."""
base = URIReference(scheme=None,
authority=None,
path='/foo/bar/bogus',
query=None,
fragment=None)
expected = '/foo/bar/relative'
assert merge_paths(base, 'relative') == expected
def test_merge_paths_with_base_authority_and_path():
"""Demonstrate merging with a base URI with an authority and path."""
base = URIReference(scheme=None,
authority='authority',
path='/foo/bar/bogus',
query=None,
fragment=None)
expected = '/foo/bar/relative'
assert merge_paths(base, 'relative') == expected
def test_merge_paths_without_base_authority_or_path():
"""Demonstrate merging with a base URI without an authority or path."""
base = URIReference(scheme=None,
authority=None,
path=None,
query=None,
fragment=None)
expected = '/relative'
assert merge_paths(base, 'relative') == expected
def test_merge_paths_with_base_authority_without_path():
"""Demonstrate merging with a base URI without an authority or path."""
base = URIReference(scheme=None,
authority='authority',
path=None,
query=None,
fragment=None)
expected = '/relative'
assert merge_paths(base, 'relative') == expected