📄 uritest.as
字号:
/*
Copyright (c) 2008, Adobe Systems Incorporated
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Adobe Systems Incorporated nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.adobe.net
{
import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;
import com.adobe.net.URI;
import com.adobe.net.IURIResolver;
public class URITest extends TestCase implements IURIResolver
{
public function URITest( methodName:String=null )
{
super( methodName );
}
/**
*
*/
public function testParsing() : void
{
var mt:String = "";
parseAndTest("http://test.com", "http", mt, mt, "test.com",
mt, mt, mt, mt, mt);
parseAndTest("http://test.com/", "http", mt, mt, "test.com",
mt, "/", mt, mt, mt);
parseAndTest("http://test.com:80", "http", mt, mt, "test.com",
"80", mt, mt, mt, mt);
parseAndTest("http://www.test.com/my/path/file.html",
"http", mt, mt, "www.test.com", mt, "/my/path/file.html", mt, mt, mt);
parseAndTest("http://www.test.com/?query=yes&name=bob",
"http", mt, mt, "www.test.com", mt, "/", "query=yes&name=bob", mt, mt);
parseAndTest("http://www.test.com/index.html#foo",
"http", mt, mt, "www.test.com", mt, "/index.html", mt, "foo", mt);
parseAndTest("http://www.test.com/#foo", "http",
mt, mt, "www.test.com", mt, "/", mt, "foo", mt);
parseAndTest("http://www.test.com/?test=1©=yes",
"http", mt, mt, "www.test.com", mt, "/", "test=1©=yes", mt, mt);
// Test everything
parseAndTest("https://bobarino:password37$%25@test.com:9100/path/to/file.html?param1=foo¶m2=bar#anchor",
"https", "bobarino", "password37$%", "test.com", "9100",
"/path/to/file.html", "param1=foo¶m2=bar", "anchor", mt);
// Test everything with escaped characters and characters that
// may be considered borderline.
parseAndTest("https://bobarino%3A:pass%3Aword37$@test.com:9100/pa%3Fth/to/fi:le.html?param1=:¶m2=?#anchor%23",
"https", "bobarino:", "pass:word37$", "test.com", "9100",
"/pa?th/to/fi:le.html", "param1=:¶m2=?", "anchor#", mt);
// Test the common case where people embed another URI in the
// query part of the base URI. This is often used when
// communicating through a proxy. We don't want to break or
// escape more characters than needed.
parseAndTest("http://jim:%25password%25@someproxy.com:8877/proxy.php?url=http://othersite.com/path/to/file.html&proxyparam=doit#proxyanchor",
"http", "jim", "%password%", "someproxy.com", "8877",
"/proxy.php", "url=http://othersite.com/path/to/file.html&proxyparam=doit",
"proxyanchor", mt);
}
public function testRelativeParsing() : void
{
var mt:String = "";
// Yes, "www.test.com" is a relative URI. To us, it seems natural that
// it should be the host name, but without a scheme, its relative.
// For example, "www.tar.gz" is a UNIX tar/gzip file and is relative.
// Whats the difference between "www.test.com" and "www.tar.gz"? If it
// has no scheme, it is relative. Period. URI::UnknownToURI tries to
// be smart, but the problem is that it could make a wrong guess in
// certain situations.
parseAndTest("www.test.com", mt, mt, mt, mt, mt,
"www.test.com", mt, mt, mt);
// Just a fragment (anchor)
parseAndTest("#foo", mt, mt, mt, mt, mt, mt, mt,
"foo", mt);
// queries can have ';' as an alternative separator to '&'
parseAndTest("?query=yes;this=that#foo",
mt, mt, mt, mt, mt, mt, "query=yes;this=that", "foo", mt);
parseAndTest("/images?blah:somequery",
mt, mt, mt, mt, mt, "/images", "blah:somequery", mt, mt);
parseAndTest("/images?blah:somequery#anchor",
mt, mt, mt, mt, mt, "/images", "blah:somequery", "anchor", mt);
}
public function testUnknownParsing() : void
{
var mt:String = "";
// Something missing the scheme
parseUnknownAndTest(
"www.somesite.com", // input
"http://www.somesite.com", // expected
"http", mt, mt, "www.somesite.com", mt, mt, mt ,mt ,mt);
// URI missing a scheme, but has slashes
parseUnknownAndTest(
"//server/path/to/file.html", // input
"http://server/path/to/file.html", // expected
"http", mt, mt, "server", mt, "/path/to/file.html", mt, mt, mt);
// Missing scheme with everything else
parseUnknownAndTest(
"://user:pass@server:8080/path/to/file.html?param1=value1#anchor",
"http://user:pass@server:8080/path/to/file.html?param1=value1#anchor",
"http", "user", "pass", "server", "8080",
"/path/to/file.html", "param1=value1", "anchor", mt);
// Make sure the unknown handles the case of a real and valid URI
parseUnknownAndTest(
"http://user:pass@www.somesite.com:200/path/to/file.html?query#foo",
"http://user:pass@www.somesite.com:200/path/to/file.html?query#foo",
"http", "user", "pass", "www.somesite.com", "200",
"/path/to/file.html", "query", "foo", mt);
// A valid non-hierarchical URI
parseUnknownAndTest("mailto:bob@smith.com",
"mailto:bob@smith.com",
"mailto", mt, mt, mt, mt, mt, mt, mt, "bob@smith.com");
// malformed URI. Some people like to type backslashes
// instead of forward slashes.
parseUnknownAndTest(
"http:\\\\somesite.com\\path\\to\\file.html",
"http://somesite.com/path/to/file.html",
"http", mt, mt, "somesite.com", mt, "/path/to/file.html", mt, mt, mt);
// A valid relative URI. Note that UnknownToURI will only detect
// relative paths that start with ".", or "..". Otherwise, it will
// guess that the path is just something missing the scheme.
parseUnknownAndTest(
"../../images/logo.gif",
"../../images/logo.gif",
mt, mt, mt, mt, mt, "../../images/logo.gif", mt, mt, mt);
}
public function testNonHierarchical() : void
{
var mt:String = "";
parseAndTest("mailto:bob@smith.com",
"mailto", mt, mt, mt, mt, mt, mt, mt, "bob@smith.com");
parseAndTest("about:blank?foo=bar#baz",
"about", mt, mt, mt, mt, mt, "foo=bar", "baz", "blank");
}
/**
* This tests the getRelation() method of URI. Given two URI's,
* determine the relation of the two relative to the object that
* getRelation() is called on.
*/
public function testEquivalence() : void
{
// The same
parseAndCompare("http://a/b/c/d", "http://a/b/c/d", URI.EQUAL, URI.EQUAL);
// Parent/child
parseAndCompare("http://a/b/c/d/", "http://a/b/c/d/e/f/", URI.PARENT, URI.CHILD);
// Not related. They are in different branches.
parseAndCompare("http://a/b/c/g", "http://a/b/c/d/e/f/", URI.NOT_RELATED, URI.NOT_RELATED);
// special case
parseAndCompare("http://somesite.com/", "http://somesite.com", URI.EQUAL, URI.EQUAL);
// non-hierarchical
parseAndCompare("mailto:bob@smith.com", "mailto:bob@smith.com", URI.EQUAL, URI.EQUAL);
parseAndCompare("mailto:bob@smith.com", "mailto:sue@company.com", URI.NOT_RELATED, URI.NOT_RELATED);
parseAndCompare("mailto:bob@smith.com", "http://test.com", URI.NOT_RELATED, URI.NOT_RELATED);
// different schemes
parseAndCompare("http://test.com", "https://test.com", URI.NOT_RELATED, URI.NOT_RELATED);
// test default port
parseAndCompare("http://test.com:80", "http://test.com", URI.EQUAL, URI.EQUAL);
parseAndCompare("http://test.com:81", "http://test.com", URI.NOT_RELATED, URI.NOT_RELATED);
// test dynamic resolution.
URI.resolver = this;
parseAndCompare("http://test.com", "http://test.org", URI.EQUAL, URI.EQUAL);
// GetRelation() does not take into account query and fragment
// parts. This is probably an edge case that we do not handle
// at this time. If we ever modify GetRelation() to handle
// that case, we need to add test cases here.
}
public function testEscaping() : void
{
var uri:URI = new URI();
uri.setParts("http", "test.com", "80", "/my path/with %weird/chars",
"search=~&name={bob}", "five%");
assertEquals("Characters not properly escaped.",
"http://test.com:80/my%20path/with%20%25weird/chars?search=~&name={bob}#five%25",
uri.toString());
var test:String = "http://test.com/ funky/{path}";
uri = new URI(test);
uri.forceEscape();
assertEquals("Characters not properly escaped.",
"http://test.com/%20%20funky/{path}", uri.toString());
// Make sure the display version does not include escaped chars.
assertEquals("toDisplayString() failed.", test, uri.toDisplayString());
}
public function parseAndCompare(str1:String, str2:String, rel1:int, rel2:int) : void
{
var uri1:URI, uri2:URI;
var result1:int, result2:int;
uri1 = new URI(str1);
uri2 = new URI(str2);
result1 = uri1.getRelation(uri2);
result2 = uri2.getRelation(uri1); // swapped
assertTrue("Relation check failed.", result1 == rel1);
assertTrue("Relation check failed.", result2 == rel2);
// Make sure nothing got modified.
assertEquals("getRelation() modified uri1.", str1, uri1.toString());
assertEquals("getRelation() modified uri2.", str2, uri2.toString());
}
protected function parseUnknownAndTest(
inURI:String, expectedURI:String, scheme:String,
username:String, password:String, authority:String,
port:String, path:String, query:String, fragment:String,
nonHierarchical:String) : void
{
var uri:URI = new URI();
uri.unknownToURI(inURI);
// We expect all cases to produce a valid URI
assertTrue("URI is invalid.", uri.isValid());
// Make sure we get out what we expect
assertEquals("URI.toString() should output what we input.", expectedURI, 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -