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 testManualURICreation(self): scheme = Scheme("http") user_info = HTTPUserInfo("user", None) host = "www.hello.com" port = 80 authority = HTTPAuthority(user_info, host, port) path = Path(["hello1", "hello2"], ABSOLUTE, True) query = HTTPQuery([("a", "1"), ("b", "2")], ["hello", "there"]) fragment = Fragment("hello") http_uri = HTTPURI(scheme, authority, path, query, fragment) result_str = http_uri.toNormalizedString() self.assertEqual(result_str, "http://[email protected]:80/hello1/hello2/?hello&there&a=1&b=2#hello")
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 testManualURICreation(self): scheme = Scheme("http") user_info = HTTPUserInfo("user", None) host = "www.hello.com" port = 80 authority = HTTPAuthority(user_info, host, port) path = Path(["hello1", "hello2"], ABSOLUTE, True) query = HTTPQuery([("a", "1"), ("b", "2")], ["hello", "there"]) fragment = Fragment("hello") http_uri = HTTPURI(scheme, authority, path, query, fragment) result_str = http_uri.toNormalizedString() self.assertEqual( result_str, "http://[email protected]:80/hello1/hello2/?hello&there&a=1&b=2#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 testNormalization(self): line = "hTtP://wWw.hEllo.coM/hello1/hello2/%2f/?there&hello&b=2&a=1#hello" uri = HTTPURI.constructUsingStr(line) result = uri.toNormalizedString() self.assertEqual( result, "http://www.hello.com/hello1/hello2/%2F/?hello&there&a=1&b=2#hello" )
def testQuerySingletons(self): line = "http://www.hello.com/hello1/hello2/?hi&hello&a=1&b=2#hello" uri = HTTPURI.constructUsingStr(line) query = uri.getQuery() singleton_list = query.getUnencodedSingletonValueList(True) self.assertEqual(len(singleton_list), 2) self.assertEqual(singleton_list[0], "hello") self.assertEqual(singleton_list[1], "hi")
def testQueryParameters(self): line = "http://www.hello.com/hello1/hello2/?hello&there&b=2&a=1#hello" uri = HTTPURI.constructUsingStr(line) query = uri.getQuery() key_value_pairs = query.getUnencodedQueryKeyValuePairs(True) self.assertEqual(len(key_value_pairs), 2) self.assertEqual(key_value_pairs[0], ("a", "1")) self.assertEqual(key_value_pairs[1], ("b", "2"))
def testHTTPURI(self): # test username, password, port line = "http://www.hello.com/hello1/hello2/?hello&there&a=1&b=2#hello" uri = HTTPURI.constructUsingStr(line) http_authority = uri.getAuthority() http_user_info = HTTPUserInfo("person:frog", "sharpener^eraser") http_authority.setPortInt(80) http_authority.setUserInfoObj(http_user_info) result_str = uri.toNormalizedString() self.assertEqual(result_str, "http://person%3Afrog:sharpener%[email protected]:80/hello1/hello2/?hello&there&a=1&b=2#hello") self.assertEqual(http_authority.getPortInt(), 80) self.assertEqual(http_user_info.getUsernameUnencodedStr(), "person:frog") self.assertEqual(http_user_info.getPasswordUnencodedStr(), "sharpener^eraser")
def testHTTPURI(self): # test username, password, port line = "http://www.hello.com/hello1/hello2/?hello&there&a=1&b=2#hello" uri = HTTPURI.constructUsingStr(line) http_authority = uri.getAuthority() http_user_info = HTTPUserInfo("person:frog", "sharpener^eraser") http_authority.setPortInt(80) http_authority.setUserInfoObj(http_user_info) result_str = uri.toNormalizedString() self.assertEqual( result_str, "http://person%3Afrog:sharpener%[email protected]:80/hello1/hello2/?hello&there&a=1&b=2#hello" ) self.assertEqual(http_authority.getPortInt(), 80) self.assertEqual(http_user_info.getUsernameUnencodedStr(), "person:frog") self.assertEqual(http_user_info.getPasswordUnencodedStr(), "sharpener^eraser")
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 testNormalization(self): line = "hTtP://wWw.hEllo.coM/hello1/hello2/%2f/?there&hello&b=2&a=1#hello" uri = HTTPURI.constructUsingStr(line) result = uri.toNormalizedString() self.assertEqual(result, "http://www.hello.com/hello1/hello2/%2F/?hello&there&a=1&b=2#hello")
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 testGetDomain(self): line = "http://www.hello.com/hello1/hello2/?hello#hello" uri = HTTPURI.constructUsingStr(line) authority = uri.getAuthority() domain_str = authority.getDomainUnencoded() self.assertEqual(domain_str, "hello.com")
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