⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uritest.as

📁 Flex的JSON类库
💻 AS
📖 第 1 页 / 共 2 页
字号:
				assertEquals("URI.password should be the same.", password, uri.password);
				assertEquals("URI.authority should be the same.", authority, uri.authority);
				assertEquals("URI.port should be the same.", port, uri.port);
				assertEquals("URI.path should be the same.", path, uri.path);
				assertEquals("URI.query should be the same.", query, uri.query);
				assertEquals("URI.fragment should be the same.", fragment, uri.fragment);	
			}
			else
			{
				assertEquals("URI.scheme should be the same.", scheme, uri.scheme);
				assertEquals("URI.nonHierarchical should be the same.", nonHierarchical, uri.nonHierarchical);
				assertEquals("URI.query should be the same.", query, uri.query);
				assertEquals("URI.fragment should be the same.", fragment, uri.fragment);
			}
		}
		
		/**
		 * This function takes a URI in string form and a set
		 * of expected results for each of the URI parts.  This
		 * function loads the 'inURI' string into a URI object
		 * and then compares the resulting object parts to the
		 * given expected result strings.
		 */
		protected function parseAndTest(inURI:String,
			scheme:String, username:String, password:String,
			authority:String, port:String,
			path:String, query:String, fragment:String,
			nonHierarchical:String) : void
		{
			var result:Boolean = true;
		
			// Construct our test URI
			var uri:URI = new URI(inURI);
		
			// Make sure we get out what we put in.
			assertEquals("URI.toString() should output what we input.", inURI, uri.toString());
		
			if (uri.isHierarchical())
			{
				assertEquals("URI.scheme should be the same.", scheme, uri.scheme);
				assertEquals("URI.username should be the same.", username, uri.username);
				assertEquals("URI.password should be the same.", password, uri.password);
				assertEquals("URI.authority should be the same.", authority, uri.authority);
				assertEquals("URI.port should be the same.", port, uri.port);
				assertEquals("URI.path should be the same.", path, uri.path);
				assertEquals("URI.query should be the same.", query, uri.query);
				assertEquals("URI.fragment should be the same.", fragment, uri.fragment);
			}
			else
			{
				assertEquals("URI.scheme should be the same.", scheme, uri.scheme);
				assertEquals("URI.nonHierarchical should be the same.", nonHierarchical, uri.nonHierarchical);
				assertEquals("URI.query should be the same.", query, uri.query);
				assertEquals("URI.fragment should be the same.", fragment, uri.fragment);
			}
			
			// Now perform the test the other way.  Set all the members
			// and compare the toString() to the given URI.
			if (nonHierarchical.length > 0)
			{
				uri = new URI();
				
				uri.scheme = scheme;
				uri.nonHierarchical = nonHierarchical;
				uri.query = query;
				uri.fragment = fragment;
				
				assertEquals("URI.toString() should be the same.", uri.toString(), inURI);
			}
			else
			{
				uri = new URI();
				
				uri.scheme = scheme;
				uri.username = username;
				uri.password = password;
				uri.authority = authority;
				uri.port = port;
				uri.path = path;
				uri.query = query;
				uri.fragment = fragment;
				
				assertEquals("URI.toString() should be the same.", inURI, uri.toString());
			}
		}
		
		/**
		 * This test case runs through a large number of cases that exercises
		 * the chdir(), makeAbsolute(), and makeRelative() functions.
		 */
		public function testAbsRelChDir() : void
		{
			// ***
			// These tests are taken directly from the URI RFC.
			// ***
		
			// Note that the ';' is part of the file name.  It's not
			// some special character.
			var base:String = "http://a/b/c/d;p?q";
		
			// A full URI completely replaces the old one.  The RFC used "g:h" for
			// their example, but we use "gj:hk" because we assume that a single
			// character scheme is most likely a "C:\" style path.  The test is
			// the same.
			doTestChDir(base, "gj:hk",	"gj:hk", true);
		
			doTestChDir(base, "g",		"http://a/b/c/g");
			doTestChDir(base, "./g",		"http://a/b/c/g");
			doTestChDir(base, "g/",		"http://a/b/c/g/");
			doTestChDir(base, "/g",		"http://a/g");
		
			// The "//" is the delimiter for an authority.
			// This kind of "path" replaces everything after
			// the scheme.
			doTestChDir(base, "//g",		"http://g", true);
			
			doTestChDir(base, "?y",		"http://a/b/c/?y");
			doTestChDir(base, "g?y",		"http://a/b/c/g?y");
			doTestChDir(base, "#s",		"http://a/b/c/d;p?q#s");
			doTestChDir(base, "g#s",		"http://a/b/c/g#s");
			doTestChDir(base, "g?y#s",	"http://a/b/c/g?y#s");
			doTestChDir(base, ";x",		"http://a/b/c/;x");
			doTestChDir(base, "g;x",		"http://a/b/c/g;x");
			doTestChDir(base, "g;x?y#s",	"http://a/b/c/g;x?y#s");
			doTestChDir(base, ".",		"http://a/b/c/");
			doTestChDir(base, "./",		"http://a/b/c/");
			doTestChDir(base, "..",		"http://a/b/");
			doTestChDir(base, "../",		"http://a/b/");
			doTestChDir(base, "../g",	"http://a/b/g");
			doTestChDir(base, "../..",	"http://a/");
			doTestChDir(base, "../../",	"http://a/");
			doTestChDir(base, "../../g",	"http://a/g");
		
			// *** Abnormal cases ***
			// These are cases where the input goes beyond the scope of the URI.
			// We need to handle these gracefully if possible.
			doTestChDir(base, "../../../g",	"http://a/g");
			doTestChDir(base, "../../../../g",	"http://a/g");
		
			// These are absolute.  Notice that the "." and ".." are not
			// collapsed here.  We won't do Abs/Rel tests on them (the true)
			// because they just too abnormal.  Just make sure ChDir works.
			doTestChDir(base, "/./g",	"http://a/./g", true);
			doTestChDir(base, "/../g",	"http://a/../g", true);
		
			doTestChDir(base, "g.",		"http://a/b/c/g.");
			doTestChDir(base, ".g",		"http://a/b/c/.g");
			doTestChDir(base, "g..",		"http://a/b/c/g..");
			doTestChDir(base, "..g",		"http://a/b/c/..g");
			doTestChDir(base, "./../g",	"http://a/b/g");
			doTestChDir(base, "./g/.",	"http://a/b/c/g/");
			doTestChDir(base, "g/./h",	"http://a/b/c/g/h");
			doTestChDir(base, "g/../h",	"http://a/b/c/h");
			doTestChDir(base, "g;x=1/./y",	"http://a/b/c/g;x=1/y");
			doTestChDir(base, "g;x=1/../y",	"http://a/b/c/y");
		
			// All client applications remove the query component from the base URI
			// before resolving relative URI.  However, some applications fail to
			// separate the reference's query and/or fragment components from a
			// relative path before merging it with the base path.  This error is
			// rarely noticed, since typical usage of a fragment never includes the
			// hierarchy ("/") character, and the query component is not normally
			// used within relative references.
			doTestChDir(base, "g?y/./x",	"http://a/b/c/g?y/./x");
			doTestChDir(base, "g?y/../x","http://a/b/c/g?y/../x");
			doTestChDir(base, "g#s/./x",	"http://a/b/c/g#s/./x");
			doTestChDir(base, "g#s/../x","http://a/b/c/g#s/../x");
		
			// Custom checks dealing with relative URI's as the base.
			base = "../../a/b/c";
		
			doTestChDir(base, "d",		"../../a/b/d", true);
			doTestChDir(base, "../d",		"../../a/d", true);
			doTestChDir(base, "../../d",  "../../d", true);
			doTestChDir(base, "../../../d",	"../../../d", true);
		
			// One last crazy relative path
			base = "../../../a";
			doTestChDir(base, "../../../../d", "../../../../../../../d", true);
		
		
			// Lastly, a specific check for known edge case
			base = "http://a/b/c/";
			var uri:URI = new URI(base);
			var baseURI:URI = new URI(base);
		
			// Making a URI relative to itself
			uri.makeRelativeURI(baseURI);
			assertEquals("makeRelativeURI() failed for identity case.", "./", uri.toString());
		}
		
		
		/**
		 * The main guts of testAbsRelChDir().  This takes an initial
		 * URI, a chdir path, and an expected result URI.  It constructs
		 * a URI object using the initial URI string, then chdirs to
		 * the directory specified by 'chPath'.  The expected result is
		 * in 'expectedURI'.  This also performs some tests using
		 * makeAbsoluteURI() and makeRelativeURI() because we have all
		 * the necessary data to exercise all of these.
		 * 
		 * @param inURI	the initial URI
		 * @param chPath	the path to "cd" to.
		 * @param expectedURI	the expected result of executing the "cd" on inURI.
		 * @param skipAbsRelTests	if true, this will skip the makeAbsolute
		 *     and makeRelative tests.  The only time you would want to pass
		 *     true is when inURI is relative.
		 */
		private function doTestChDir(inURI:String, chPath:String,
			expectedURI:String, skipAbsRelTests:Boolean = false) : void
		{
			var uri:URI = new URI(inURI);
		
			assertEquals("chdir() sanity check", inURI, uri.toString());
		
			uri.chdir(chPath);
		
			assertEquals("chdir() failed.", expectedURI, uri.toString());
		
			// We have enough info to do absolute and relative operations, so
			// lets do those too, but only if it makes sense to do them. 
			// This function may be called with all relative paths (testing
			// chdir for cases using only relative paths).  We don't want to
			// do makeAbsoluteURI() and makeRelativeURI() tests in that case.
			if (skipAbsRelTests)
			{
				// Caller knows the input is not appropriate for makeRelative and
				// makeAbsolute.  Just stop here.
				return;
			}
		
			/////////////////////////////////////////////////////////////////
			// Test makeAbsoluteURI
			var abs:URI = new URI(inURI);
			var rel:URI = new URI(chPath);
		
			// Make sure we get out what we put in
			assertEquals("Absolute URI failed to parse.", inURI, abs.toString());
			assertEquals("chPath failed to parse.", chPath, rel.toString());
		
			rel.makeAbsoluteURI(abs);
			assertEquals("makeAbsoluteURI() failed.", expectedURI, rel.toString());
			
			// Make sure the passed URI didn't get modified.
			assertEquals("ERROR! makeAbsoluteURI() modified the passed URI.", inURI, abs.toString());
	
			
			/////////////////////////////////////////////////////////////////
			// Test makeRelativeURI
			var toRel:URI = new URI(expectedURI);
			var base:URI = new URI(inURI);
			var path:URI = new URI(chPath);
		
				// Make sure we get out what we put in
			assertEquals("expectedURI failed to parse.", expectedURI, toRel.toString());
		
			toRel.makeRelativeURI(base);
			
			// verification that makeRelativeURI() did what it was supposed to
			// do is handled below.
			
			// Make sure the passed URI didn't get modified
			assertEquals("ERROR! makeRelativeURI modified the passed URI.", inURI, base.toString());
		
			/////////////////////////////////////////////////////////////////
			// We should be back to the relative path we were given.
			// To test this, we ChDir on the inURI with the relative
			// path we generated and the one we were given.  If the
			// result is the same, we are good.  The reason we can't
			// directly compare the two is because often we are given
			// "./file" as the chPath, but we will generate "file"
			var test1:URI = new URI(inURI);
			var test2:URI = new URI(inURI);
				
			test1.chdir(toRel.toString());
			test2.chdir(chPath);
		
			assertTrue("Relative path verification failed.", test1.getRelation(test2) == URI.EQUAL);
		}
		
		// Interface for IURIResolver
		public function resolve(uri:URI) : URI
		{
			if (uri == null)
				return null;
				
			if (uri.authority == "test.org")
				uri.authority = "test.com";
				
			return uri;
		}
		
	} // end class
} // end package

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -