def testSwapOutComponent(self): line = "http://www.hello.com/hello1/hello2/?hello&there&a=1&b=2#hello" uri = HTTPURI.constructUsingStr(line) user_info = HTTPUserInfo("user", "password") authority = HTTPAuthority(user_info, "www.there.com", 80) uri.setAuthority(authority) result_str = uri.toNormalizedString() self.assertEqual( result_str, "http://*****:*****@www.there.com:80/hello1/hello2/?hello&there&a=1&b=2#hello" ) line = "ftp://ftp.is.co.za/rfc/rfc1808.txt" uri = URI.constructUsingStr(line) user_info = UserInfo("jane_doe") authority = uri.getAuthority() authority.setUserInfoObj(user_info) result_str = uri.toNormalizedString() self.assertEqual(result_str, "ftp://[email protected]/rfc/rfc1808.txt") line = "http://www.hello.com/hello1/hello2/?hello=there#hello" uri = HTTPURI.constructUsingStr(line) user_info = HTTPUserInfo("user", None) uri.getAuthority().setUserInfoObj(user_info) result_str = uri.toNormalizedString() self.assertEqual( result_str, "http://[email protected]/hello1/hello2/?hello=there#hello") line = "http://[email protected]/hello1/hello2/?hello=there#hello" uri = URI.constructUsingStr(line) uri.getAuthority().setUserInfoObj(None) result_str = uri.toNormalizedString() self.assertEqual( result_str, "http://www.hello.com/hello1/hello2/?hello=there#hello")
def testSwapOutComponent(self): line = "http://www.hello.com/hello1/hello2/?hello&there&a=1&b=2#hello" uri = HTTPURI.constructUsingStr(line) user_info = HTTPUserInfo("user", "password") authority = HTTPAuthority(user_info, "www.there.com", 80) uri.setAuthority(authority) result_str = uri.toNormalizedString() self.assertEqual(result_str, "http://*****:*****@www.there.com:80/hello1/hello2/?hello&there&a=1&b=2#hello") line = "ftp://ftp.is.co.za/rfc/rfc1808.txt" uri = URI.constructUsingStr(line) user_info = UserInfo("jane_doe") authority = uri.getAuthority() authority.setUserInfoObj(user_info) result_str = uri.toNormalizedString() self.assertEqual(result_str, "ftp://[email protected]/rfc/rfc1808.txt") line = "http://www.hello.com/hello1/hello2/?hello=there#hello" uri = HTTPURI.constructUsingStr(line) user_info = HTTPUserInfo("user", None) uri.getAuthority().setUserInfoObj(user_info) result_str = uri.toNormalizedString() self.assertEqual(result_str, "http://[email protected]/hello1/hello2/?hello=there#hello") line = "http://[email protected]/hello1/hello2/?hello=there#hello" uri = URI.constructUsingStr(line) uri.getAuthority().setUserInfoObj(None) result_str = uri.toNormalizedString() self.assertEqual(result_str, "http://www.hello.com/hello1/hello2/?hello=there#hello")
def getComponentStrTuple(line, is_http_like=False, do_normalize=False): uri = None if is_http_like == True: uri = HTTPURI.constructUsingStr(line) else: uri = URI.constructUsingStr(line) components = uri.getComponents() scheme, authority, path, query, fragment = components scheme_str = scheme.toString(do_normalize) if scheme != None else None authority_str = authority.toString( do_normalize) if authority != None else None have_scheme = uri.haveScheme() if do_normalize == True: path.removeDotSegments() path_str = path.toString(have_scheme) if path != None else None query_str = None if query != None: if query.isHTTPQuery() == True: query_str = query.toString(do_normalize) else: query_str = query.toString() fragment_str = fragment.toString() if fragment != None else None component_str_tuple = (scheme_str, authority_str, path_str, query_str, fragment_str) return component_str_tuple
def testRemoveDotSegments(self): uri_str1 = "ftp://ftp.hello.com/a/b/c/../d" uri1 = URI.constructUsingStr(uri_str1) result1 = uri1.toNormalizedString() self.assertEqual(result1, "ftp://ftp.hello.com/a/b/d") uri_str2 = "http://www.hello.com/a/b/c/.././../d/../e/f" uri2 = HTTPURI.constructUsingStr(uri_str2) result2 = uri2.toNormalizedString() self.assertEqual(result2, "http://www.hello.com/a/e/f") uri_str3 = "http://www.hello.com/a/b/c/." uri3 = HTTPURI.constructUsingStr(uri_str3) result3 = uri3.toNormalizedString() self.assertEqual(result3, "http://www.hello.com/a/b/c/")
def testComparison(self): line1 = "example://a/b/c/%7Bfoo%7D" uri1 = URI.constructUsingStr(line1) line2 = "eXAMPLE://a/./b/../b/%63/%7bfoo%7d" uri2 = URI.constructUsingStr(line2) self.assertTrue(uri1.isEquivalentTo(uri2)) line1 = "http://a/b/c/%7Bfoo%7D" uri1 = URI.constructUsingStr(line1) line2 = "hTTP://a/./b/../b/%63/%7bfoo%7d" uri2 = URI.constructUsingStr(line2) self.assertTrue(uri1.isEquivalentTo(uri2)) line1 = "http://www.hello.com/" uri1 = URI.constructUsingStr(line1) line2 = "http://www.there.com/" uri2 = URI.constructUsingStr(line2) self.assertFalse(uri1.isEquivalentTo(uri2))
def testComponentPercentCoding(self): result1 = URI.percentEncode("www./.com", HOST_COMPONENT) self.assertEqual(result1, "www.%2F.com") result2 = URI.percentEncode("a&b", HTTP_QUERY_KEY_COMPONENT) self.assertEqual(result2, "a%26b") result3 = URI.percentEncode("a=b+2&b=hello there + bye", QUERY_COMPONENT) self.assertEqual(result3, "a=b%2B2&b=hello+there+%2B+bye") encoded_str1 = "www.%2F.com" result1 = URI.percentDecode(encoded_str1, True, False) self.assertEqual(result1, "www./.com") encoded_str2 = "a%26b" result2 = URI.percentDecode(encoded_str2, True, False) self.assertEqual(result2, "a&b") encoded_str3 = "a=b%2B2&b=hello+there+%2B+bye" result3 = URI.percentDecode(encoded_str3, True, False) self.assertEqual(result3, "a=b+2&b=hello there + bye")
def getComponentStrTuple(line, is_http_like = False, do_normalize = False): uri = None if is_http_like == True: uri = HTTPURI.constructUsingStr(line) else: uri = URI.constructUsingStr(line) components = uri.getComponents() scheme, authority, path, query, fragment = components scheme_str = scheme.toString(do_normalize) if scheme != None else None authority_str = authority.toString(do_normalize) if authority != None else None have_scheme = uri.haveScheme() if do_normalize == True: path.removeDotSegments() path_str = path.toString(have_scheme) if path != None else None query_str = None if query != None: if query.isHTTPQuery() == True: query_str = query.toString(do_normalize) else: query_str = query.toString() fragment_str = fragment.toString() if fragment != None else None component_str_tuple = (scheme_str, authority_str, path_str, query_str, fragment_str) return component_str_tuple
def testChDir(self): line = "http://www.hello.com/" uri = URI.constructUsingStr(line) path = uri.getPath() path.chdir("there", True) self.assertEqual(uri.toString(), "http://www.hello.com/there/")
def getRRResult(base_URI_str, relative_URI_str, is_strict = False): relative_URI = URI.constructUsingStr(relative_URI_str) base_URI = HTTPURI.constructUsingStr(base_URI_str) result_URI = relative_URI.resolve(base_URI, is_strict) result = result_URI.toNormalizedString() return result
def testPercentEncode(self): line = "hi, +-%" result_str = URI.percentEncode(line, NOT_A_COMPONENT) self.assertEqual(result_str, "%68%69%2C+%2B%2D%25")
def testPercentDecode(self): line = "%68%69%2C+%2B%2D%25" result_str1 = URI.percentDecode(line, True, False) self.assertEqual(result_str1, "hi, +-%") result_str2 = URI.percentDecode(line, True, True) self.assertEqual(result_str2, "hi%2C %2B-%25")
def getRRResult(base_URI_str, relative_URI_str, is_strict=False): relative_URI = URI.constructUsingStr(relative_URI_str) base_URI = HTTPURI.constructUsingStr(base_URI_str) result_URI = relative_URI.resolve(base_URI, is_strict) result = result_URI.toNormalizedString() return result