예제 #1
0
 def test_fragment(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(u.fragment is None, "no fragment")
     u = uri.URI('http://www.ics.uci.edu/pub/ietf/uri/#Related')
     self.assertTrue(u.scheme_specific_part ==
                     '//www.ics.uci.edu/pub/ietf/uri/', 'URI with fragment')
     self.assertTrue(u.fragment == 'Related', 'fragment')
예제 #2
0
 def test_compare(self):
     u1 = uri.URI("x-HTTP://www.example.com/")
     u2 = uri.URI("x-http://www.example.com/")
     self.assertTrue(
         u1.match(u2) and u2.match(u1), "Equal URIs fail to match")
     self.assertTrue(u1 == u2, "Equal URIs compare equal")
     self.assertFalse(u1 != u2, "Equal URIs compare not equal")
     self.assertTrue(u1 <= u2, "Equal URIs sort less-equal")
     self.assertTrue(u1 >= u2, "Equal URIs sort greater-equal")
     self.assertFalse(u1 < u2, "Equal URIs sort less than")
     self.assertFalse(u1 > u2, "Equal URIs sort greater than")
     self.assertTrue(hash(u1) == hash(u2), "Equal URIs, equal hash")
     u2 = uri.URI('hello.xml')
     self.assertFalse(
         u1.match(u2) or u2.match(u1), "Mismatched URIs do match")
     u1 = uri.URI("x-HTTP://www.example.com/")
     u2 = uri.URI("x-http://www2.example.com/")
     self.assertFalse(
         u1.match(u2) or u2.match(u1), "Equal URIs fail to match")
     self.assertFalse(u1 == u2, "Unequal URIs compare equal")
     self.assertTrue(u1 != u2, "Unequal URIs compare not-equal")
     self.assertTrue(u1 <= u2, "Unequal URIs sort less-equal")
     self.assertFalse(u1 >= u2, "Unequal URIs sort greater-equal")
     self.assertTrue(u1 < u2, "Unequal URIs sort less than")
     self.assertFalse(u1 > u2, "Unequal URIs sort greater than")
     self.assertFalse(hash(u1) == hash(u2), "Unequal URIs, Unequal hash")
     # check comparison against strings
     u2 = "x-http://www3.example.com/"
     self.assertFalse(u1 == u2, "Unequal URIs compare equal")
     self.assertTrue(u1 != u2, "Unequal URIs compare not-equal")
     self.assertTrue(u1 <= u2, "Unequal URIs sort less-equal")
     self.assertFalse(u1 >= u2, "Unequal URIs sort greater-equal")
     self.assertTrue(u1 < u2, "Unequal URIs sort less than")
     self.assertFalse(u1 > u2, "Unequal URIs sort greater than")
예제 #3
0
 def test_constructor(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(isinstance(u, uri.URI))
     self.assertTrue(str(u) == SIMPLE_EXAMPLE)
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     if py2:
         self.assertTrue(to_text(u) == SIMPLE_EXAMPLE)
     try:
         u = uri.URI(LIST_EXAMPLE)
         # we don't support this type of thing any more
         # self.assertTrue(str(u)==SIMPLE_EXAMPLE,"Simple from list")
     except uri.URIException:
         pass
     u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'))
     self.assertTrue(
         str(u) == '%E8%8B%B1%E5%9B%BD.xml', "Unicode example: %s" % str(u))
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
     try:
         u = uri.URI.from_octets(u8(b'\xe8\x8b\xb1\xe5\x9b\xbd.xml'),
                                 strict=True)
         self.fail("strict mode requires %-encoding")
     except uri.URIException:
         pass
     # binary string must be US-ASCII clean
     try:
         u = uri.URI.from_octets(b'Caf\xe9')
         self.fail("binary string must be US-ASCII")
     except UnicodeDecodeError:
         pass
     # but URI-encoded is OK even if it is binary
     u = uri.URI.from_octets(b'Caf%E9')
     self.assertTrue(is_unicode(u.octets),
                     "octets must be a character string")
예제 #4
0
 def test_scheme(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(u.is_absolute(), "Absolute test")
     self.assertTrue(u.scheme == 'http', "Scheme")
     self.assertTrue(u.scheme_specific_part == '//www.example.com/')
     u = uri.URI(RELATIVE_EXAMPLE)
     self.assertFalse(u.is_absolute(), "Relative test")
     self.assertTrue(u.scheme is None, "relative scheme")
     self.assertTrue(u.scheme_specific_part is None)
예제 #5
0
 def test_relative_join_examples(self):
     keys = REL_EXAMPLES.keys()
     base1 = uri.URI(REL_BASE1)
     base2 = uri.URI(REL_BASE2)
     current = uri.URI(REL_CURRENT)
     relatives = {}
     for k in keys:
         u = uri.URI(k)
         if not u.octets:  # don't test same document cases
             continue
         resolved, scheme, authority, abs_path, rel_path, query, \
             fragment = REL_EXAMPLES[k]
         logging.info("Testing: %s [*] ( %s [*] %s ) = %s", str(base1),
                      str(base2), k, resolved)
         # two-step resolution, first combines relative URLs, second
         # resolves to absolute
         resolution1 = u.resolve(base2, current)
         relatives[str(resolution1)] = relatives.get(str(resolution1),
                                                     []) + [k]
         resolution2 = str(resolution1.resolve(base1, current))
         self.assertTrue(scheme == u.scheme,
                         "%s found scheme %s" % (k, u.scheme))
         self.assertTrue(authority == u.authority,
                         "%s found authority %s" % (k, u.authority))
         self.assertTrue(abs_path == u.abs_path,
                         "%s found abs_path %s" % (k, u.abs_path))
         self.assertTrue(rel_path == u.rel_path,
                         "%s found rel_path %s" % (k, u.rel_path))
         self.assertTrue(query == u.query,
                         "%s found query %s" % (k, u.query))
         self.assertTrue(fragment == u.fragment,
                         "%s found fragment %s" % (k, u.fragment))
         self.assertTrue(
             resolved == resolution2,
             "%s [*] ( %s [*] %s ) = %s ; found %s" %
             (str(base1), str(base2), k, resolved, resolution2))
     for r in relatives.keys():
         logging.info("Testing: %s [/] %s = ( %s )", r, str(base2),
                      string.join(relatives[r], ' | '))
         u = uri.URI(r)
         # this check removes the 'current document' case
         if u.octets == 'current.doc':
             continue
         relative = str(u.relative(base2))
         # now relative should be one of the relatives!
         no_match = True
         for k in relatives[r]:
             if k == relative:
                 no_match = False
                 break
         self.assertFalse(
             no_match, "%s [/] %s = ( %s ); found %s" %
             (r, str(base2), repr(relatives[r]), relative))
예제 #6
0
 def test_compare(self):
     u1 = uri.URI(SIMPLE_EXAMPLE)
     u2 = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(
         u1.match(u2) and u2.match(u1), "Equal URIs fail to match")
     u2 = uri.URI('hello.xml')
     self.assertFalse(
         u1.match(u2) or u2.match(u1), "Mismatched URIs do match")
     u1 = uri.URI("HTTP://www.example.com/")
     u2 = uri.URI("http://www.example.com/")
     self.assertTrue(
         u1.match(u2) and u2.match(u1), "Equal URIs fail to match")
예제 #7
0
 def test_constructor(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(isinstance(u, uri.URI))
     self.assertTrue(str(u) == SIMPLE_EXAMPLE)
     try:
         u = uri.URI(LIST_EXAMPLE)
         # we don't support this type of thing any more
         # self.assertTrue(str(u)==SIMPLE_EXAMPLE,"Simple from list")
     except uri.URIException:
         pass
     u = uri.URI.from_octets(u'\u82f1\u56fd.xml')
     self.assertTrue(
         str(u) == '%E8%8B%B1%E5%9B%BD.xml', "Unicode example: %s" % str(u))
     self.assertTrue(type(u.octets) is StringType, "octest must be string")
예제 #8
0
 def test_canonicalize(self):
     u = uri.URI('X-Pyslet:50%2f50')
     u2 = u.canonicalize()
     self.assertTrue(str(u2) == 'x-pyslet:50%2f50')
     self.assertTrue(str(u2.get_canonical_root()) == 'x-pyslet:')
     u = uri.URI('X-Pyslet:50%2f50#Frag')
     u2 = u.canonicalize()
     self.assertTrue(str(u2) == 'x-pyslet:50%2f50#Frag')
     self.assertTrue(str(u.get_canonical_root()) == 'x-pyslet:')
     u = uri.URI("x-pyslet://host/path")
     self.assertTrue(str(u.get_canonical_root()) == 'x-pyslet://host')
     u = uri.URI("x-pyslet:/path")
     self.assertTrue(str(u.get_canonical_root()) == 'x-pyslet:')
     u = uri.URI("/path")
     self.assertTrue(u.get_canonical_root() is None)
예제 #9
0
 def visit_method(self, dirname, names):
     d = uri.URI.from_path(os.path.join(dirname, os.curdir))
     c = sys.getfilesystemencoding()
     for name in names:
         if name.startswith('??'):
             logging.warn("8-bit path tests limited to ASCII file names "
                          "by %s encoding", c)
             continue
         join_match = os.path.join(dirname, name)
         if is_unicode(name):
             seg_name = uri.escape_data(
                 name.encode(c), uri.is_path_segment_reserved)
         else:
             seg_name = uri.escape_data(name, uri.is_path_segment_reserved)
         u = uri.URI(seg_name)
         u = u.resolve(d)
         self.assertTrue(isinstance(u, uri.FileURL))
         joined = u.get_pathname()
         if isinstance(join_match, bytes) and is_unicode(joined):
             # if we're walking in 8-bit mode we need to downgrade to
             # compare
             joined = joined.encode(c)
         self.assertTrue(joined == join_match,
                         "Joined pathnames mismatch:\n%s\n%s" %
                         (joined, join_match))
예제 #10
0
 def test_scheme(self):
     u = uri.URI(SIMPLE_EXAMPLE)
     self.assertTrue(u.is_absolute(), "Absolute test")
     self.assertTrue(u.scheme == 'http', "Scheme")
     self.assertTrue(u.scheme_specific_part == '//www.example.com/')
     u = uri.URI(RELATIVE_EXAMPLE)
     self.assertFalse(u.is_absolute(), "Relative test")
     self.assertTrue(u.scheme is None, "relative scheme")
     self.assertTrue(u.scheme_specific_part is None)
     u = uri.URI("x-pyslet:")
     self.assertTrue(u.is_absolute(), "Relative test")
     self.assertTrue(u.scheme == 'x-pyslet', "scheme only scheme")
     self.assertTrue(u.scheme_specific_part == '')
     # missing authority
     u = uri.URI("x-pyslet:/just/a/path")
     self.assertTrue(u.is_absolute(), "Relative test")
     self.assertTrue(u.abs_path == "/just/a/path", "abs path only")
     # missing abs path
     u = uri.URI("x-pyslet://host?just-a-query")
     self.assertTrue(u.is_absolute(), "Relative test")
     self.assertTrue(u.abs_path is None, "abs path only")
     self.assertTrue(u.query == "just-a-query", "abs path only")
     self.assertTrue(str(u) == "x-pyslet://host?just-a-query")
     # relative authority
     u = uri.URI("//host/just/a/path")
     self.assertFalse(u.is_absolute(), "Relative test")
     self.assertTrue(u.abs_path == "/just/a/path", "abs path only")
     # relative missing abs path
     u = uri.URI("//host?just-a-query")
     self.assertFalse(u.is_absolute(), "Relative test")
     self.assertTrue(u.abs_path is None, "no abs path")
     self.assertTrue(u.query == "just-a-query", "abs path only")
     self.assertTrue(str(u) == "//host?just-a-query")
예제 #11
0
 def test_relative_examples(self):
     keys = REL_EXAMPLES.keys()
     base = uri.URI(REL_BASE)
     current = uri.URI(REL_CURRENT)
     relatives = {}
     for k in keys:
         logging.info("Testing relative: %s", k)
         u = uri.URI(k)
         resolved, scheme, authority, abs_path, rel_path, query, \
             fragment = REL_EXAMPLES[k]
         relatives[resolved] = relatives.get(resolved, []) + [k]
         resolution = str(u.resolve(base, current))
         self.assertTrue(scheme == u.scheme,
                         "%s found scheme %s" % (k, u.scheme))
         self.assertTrue(authority == u.authority,
                         "%s found authority %s" % (k, u.authority))
         self.assertTrue(abs_path == u.abs_path,
                         "%s found abs_path %s" % (k, u.abs_path))
         self.assertTrue(rel_path == u.rel_path,
                         "%s found rel_path %s" % (k, u.rel_path))
         self.assertTrue(query == u.query,
                         "%s found query %s" % (k, u.query))
         self.assertTrue(fragment == u.fragment,
                         "%s found fragment %s" % (k, u.fragment))
         self.assertTrue(
             resolved == resolution, "%s [*] %s = %s ; found %s" %
             (str(base), k, resolved, resolution))
     for r in relatives.keys():
         logging.info("Testing %s [/] %s = ( %s )", r, str(base),
                      string.join(relatives[r], ' | '))
         u = uri.URI(r)
         # this check removes the 'current document' case
         if not u.is_absolute():
             continue
         relative = str(u.relative(base))
         # relative should be one of the relatives!
         no_match = True
         for k in relatives[r]:
             if k == relative:
                 no_match = False
                 break
         self.assertFalse(
             no_match, "%s [/] %s = ( %s ) ; found %s" %
             (r, str(base), string.join(relatives[r], ' | '), relative))
예제 #12
0
 def visit_method(self, dirname, names):
     # Make d a directory like path by adding an empty component at the end
     d = uri.URI.from_virtual_path(dirname.join(dirname.curdir))
     for name in names:
         if unicode(name).startswith('??'):
             logging.warn("8-bit path tests limited to ASCII file names")
             continue
         join_match = dirname.join(name)
         seg_name = uri.escape_data(
             unicode(name).encode('utf-8'), uri.is_path_segment_reserved)
         u = uri.URI(seg_name)
         u = u.resolve(d)
         self.assertTrue(isinstance(u, uri.FileURL))
         joined = u.get_virtual_file_path()
         self.assertTrue(
             joined == join_match,
             "Joined pathnames mismatch:\n%s\n%s" % (joined, join_match))
예제 #13
0
 def test_absolute_examples(self):
     for k in dict_keys(ABS_EXAMPLES):
         logging.info("Testing absolute: %s", k)
         u = uri.URI(k)
         scheme, opaque_part, authority, abs_path, query, fName = \
             ABS_EXAMPLES[k]
         self.assertTrue(scheme == u.scheme,
                         "%s found scheme %s" % (k, u.scheme))
         self.assertTrue(opaque_part == u.opaque_part,
                         "%s found opaque_part %s" % (k, u.opaque_part))
         self.assertTrue(authority == u.authority,
                         "%s found authority %s" % (k, u.authority))
         self.assertTrue(abs_path == u.abs_path,
                         "%s found abs_path %s" % (k, u.abs_path))
         self.assertTrue(query == u.query,
                         "%s found query %s" % (k, u.query))
         self.assertTrue(fName == u.get_file_name(),
                         "%s found file name %s" % (k, u.get_file_name()))
예제 #14
0
 def test_relative_join_examples(self):
     base1 = uri.URI(REL_BASE1)
     base2 = uri.URI(REL_BASE2)
     current = uri.URI(REL_CURRENT)
     relatives = {}
     for k in dict_keys(REL_EXAMPLES):
         u = uri.URI(k)
         if not u.octets:  # don't test same document cases
             continue
         resolved, scheme, authority, abs_path, rel_path, query, \
             fragment = REL_EXAMPLES[k]
         logging.info("Testing: %s [*] ( %s [*] %s ) = %s",
                      str(base1), str(base2), k, resolved)
         # two-step resolution, first combines relative URLs, second
         # resolves to absolute
         resolution1 = u.resolve(base2, current)
         relatives[str(resolution1)] = relatives.get(
             str(resolution1), []) + [k]
         resolution2 = str(resolution1.resolve(base1, current))
         self.assertTrue(scheme == u.scheme,
                         "%s found scheme %s" % (k, u.scheme))
         self.assertTrue(authority == u.authority,
                         "%s found authority %s" % (k, u.authority))
         self.assertTrue(abs_path == u.abs_path,
                         "%s found abs_path %s" % (k, u.abs_path))
         self.assertTrue(rel_path == u.rel_path,
                         "%s found rel_path %s" % (k, u.rel_path))
         self.assertTrue(query == u.query,
                         "%s found query %s" % (k, u.query))
         self.assertTrue(fragment == u.fragment,
                         "%s found fragment %s" % (k, u.fragment))
         self.assertTrue(resolved == resolution2,
                         "%s [*] ( %s [*] %s ) = %s ; found %s" %
                         (str(base1), str(base2), k, resolved, resolution2))
     for r in dict_keys(relatives):
         logging.info("Testing: %s [/] %s = ( %s )", r,
                      str(base2), ' | '.join(relatives[r]))
         u = uri.URI(r)
         # this check removes the 'current document' case
         if u.octets == 'current.doc':
             continue
         relative = str(u.relative(base2))
         # now relative should be one of the relatives!
         no_match = True
         for k in relatives[r]:
             if k == relative:
                 no_match = False
                 break
         self.assertFalse(no_match, "%s [/] %s = ( %s ); found %s" %
                          (r, str(base2), repr(relatives[r]), relative))
     # catch a couple of odd cases not in the standard examples
     #   U = B [*] R1 [*] R2
     #   R3 = R1 [*] R2
     #   R1 = ../a/b
     #   R2 = ../../c/d
     #   If B = /p/q/r/s, B [*] R1 = /p/q/a/b
     #   So U = /p/q/a/b [*] ../../c/d = /p/c/d
     #   So /p/c/d = /p/q/r/s [*] R3
     #   R3 = /p/c/d [/] /p/q/r/s
     #   R3 = ../../c/d
     r1 = uri.URI('../a/b')
     r2 = uri.URI('../../c/d')
     r3 = uri.URI('../../c/d')
     self.assertTrue(str(r2.resolve(r1)) == str(r3))
     # therefore it is possible for a (relative) base to start '..'
     self.assertTrue(str(r3.relative(r1)) == str(r2))
     # but it should never be possible for the base to have more '..'
     # than the URL it resolves to, so check that raises an error
     try:
         rx = uri.URI('c/d').relative(r1)
         self.fail("c/d relative to ../a/b = " + str(rx))
     except uri.URIException as e:
         logging.info("Too many parents: %s", str(e))