Skip to content

Commit

Permalink
Changes per Issue mikechambers#160 of original fork.
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Apr 30, 2012
1 parent 24c6c16 commit 5eaa6f6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 18 deletions.
38 changes: 27 additions & 11 deletions src/com/adobe/net/URI.as
Expand Up @@ -280,7 +280,7 @@ package com.adobe.net
// we probably parsed a C:\ type path or no scheme
return false;
}
else if (verifyAlpha(_scheme) == false)
else if (verifyScheme(_scheme) == false)
return false; // Scheme contains bad characters
}

Expand Down Expand Up @@ -432,6 +432,13 @@ package com.adobe.net
// Trim off the "//"
baseURI = baseURI.substr(2, baseURI.length - 2);
}
else if (_scheme == "app" || _scheme == "app-storage") {
/* This is an AIR local file URI scheme. It doesn't appear to be heirarchical
due to the single slash, but we can treat it as such. It's really
a fancy file:// URI relative to one of the special directories used
by AIR for local data storage. */
_nonHierarchical = "";
}
else
{
// This is a non-hierarchical URI like "mailto:bob@mail.com"
Expand Down Expand Up @@ -567,20 +574,27 @@ package com.adobe.net

/**
* @private
* Checks if the given string only contains a-z or A-Z.
* Checks if the given string only contains characters
* valid for a URI scheme as per RFC 3986.
*
* http://tools.ietf.org/html/rfc3986
*
* Must begin with a letter, and them contain any number
* of letters, numbers, or "+", "-", or "."
*/
protected function verifyAlpha(str:String) : Boolean
public static function verifyScheme(str:String) : Boolean
{
var pattern:RegExp = /[^a-z]/;
var pattern:RegExp = /^[a-z][a-z0-9.+\-]*/;
var index:int;

str = str.toLowerCase();
index = str.search(pattern);

if (index == -1)
if (index != -1) {
return true;
else
return false;
}

return false;
}

/**
Expand Down Expand Up @@ -1453,14 +1467,16 @@ package com.adobe.net

thisExtension = getExtension(true);

if (thisExtension == "")
if (thisExtension == "") {
return false;
}

// Compare the extensions ignoring case
if (compareStr(thisExtension, extension, false) == 0)
if (compareStr(thisExtension, extension, false)) {
return true;
else
return false;
}

return false;
}


Expand Down
110 changes: 103 additions & 7 deletions tests/src/com/adobe/net/URITest.as
Expand Up @@ -32,12 +32,12 @@

package com.adobe.net
{
import com.adobe.net.IURIResolver;
import com.adobe.net.URI;

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 )
Expand Down Expand Up @@ -287,7 +287,8 @@ package com.adobe.net
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());
var actualURI : String = uri.toString();
assertEquals("URI.toString() should output what we input.", expectedURI, actualURI);

if (uri.isHierarchical())
{
Expand Down Expand Up @@ -328,7 +329,8 @@ package com.adobe.net
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());
var actualURI:String = uri.toString();
assertEquals("URI.toString() should output what we input.", inURI, actualURI);

if (uri.isHierarchical())
{
Expand Down Expand Up @@ -360,7 +362,8 @@ package com.adobe.net
uri.query = query;
uri.fragment = fragment;

assertEquals("URI.toString() should be the same.", uri.toString(), inURI);
var actualURI:String = uri.toDisplayString();
assertEquals("URI.toString() should be the same.", actualURI, inURI);
}
else
{
Expand Down Expand Up @@ -585,5 +588,98 @@ package com.adobe.net
return uri;
}

public function testFileType() : void
{
const TEST_EXTENSION : String = "xml";
const TEST_EXTENSION_OTHER : String = "csv";
const TEST_EXTENSION_UPPERCASE : String = "XML";

const TEST_FILENAME : String = "foo.xml";
const TEST_URI : String = "http://adobe.com/foo.xml";

// try with a full URI
var http_uri : URI = new URI( TEST_URI );

assertFalse( "URI should NOT match different extension.", http_uri.isOfFileType( TEST_EXTENSION_OTHER ) );
assertTrue( "URI should have matched test extension.", http_uri.isOfFileType( TEST_EXTENSION ) );
assertTrue( "URI should have matched uppercase test extension.", http_uri.isOfFileType( TEST_EXTENSION_UPPERCASE ) );

// try with just a filename
var file_uri : URI = new URI( TEST_FILENAME );

assertFalse( "URI should NOT match different extension.", file_uri.isOfFileType( TEST_EXTENSION_OTHER ) );
assertTrue( "URI should have matched test extension.", file_uri.isOfFileType( TEST_EXTENSION ) );
assertTrue( "URI should have matched uppercase test extension.", file_uri.isOfFileType( TEST_EXTENSION_UPPERCASE ) );
}

public function testSpecialAIRParsing():void {
var mt:String = "";
// Test Adobe AIR special URIs for embedded assets and local temp files
parseAndTest("app:/my/path/file.html",
"app", mt, mt, mt, mt, "/my/path/file.html", mt, mt, mt);

parseAndTest("app-storage:/my/path/file.html",
"app-storage", mt, mt, mt, mt, "/my/path/file.html", mt, mt, mt);
}

public function testVerifyScheme():void {
assertTrue( URI.verifyScheme( "HTTP" ) );
assertTrue( URI.verifyScheme( "http" ) );
assertTrue( URI.verifyScheme( "app-storage" ) );
assertTrue( URI.verifyScheme( "git+ssh" ) );
assertTrue( URI.verifyScheme( "z39.50s" ) );
assertTrue( URI.verifyScheme( "A0K" ) );
assertTrue( URI.verifyScheme( "X" ) );

assertFalse( URI.verifyScheme( "0" ) );
assertFalse( URI.verifyScheme( "0ff" ) );
}

public function testUnsualSchemes():void {
var mt:String = "";
var uri : URI;

/* Checks if the given string only contains characters
* valid for a URI scheme as per RFC 3986.
*
* http://tools.ietf.org/html/rfc3986
*
* Must begin with a letter, and them contain any number
* of letters, numbers, or "+", "-", or "."
*/

// May contain plusses
parseAndTest("git+ssh://github.com/mikechambers/as3corelib.git",
"git+ssh", mt, mt, "github.com", mt, "/mikechambers/as3corelib.git", mt, mt, mt);

// May contain periods, and numbers for that matter
parseAndTest("z39.50r://test.com/database",
"z39.50r", mt, mt, "test.com", mt, "/database", mt, mt, mt);

// May contain hyphens
parseAndTest("view-source:www.test.com/my/path/file.html",
"view-source", mt, mt, mt, mt, mt, mt, mt, "www.test.com/my/path/file.html");

// Normalize upper case schemes to lowercase.
uri = new URI("HTTP://www.test.com/my/path/file.html");
assertEquals( "www.test.com", uri.authority );
assertEquals( "/my/path/file.html", uri.path );
assertEquals( mt, uri.fragment );
assertEquals( mt, uri.query );

// First character of scheme must be an alphabetic character - not valid in these cases
uri = new URI("0http://www.test.com/my/path/file.html");
assertEquals( "unknown:", uri.toString() );

uri = new URI(".http://www.test.com/my/path/file.html");
assertEquals( "unknown:", uri.toString() );

uri = new URI("+http://www.test.com/my/path/file.html");
assertEquals( "unknown:", uri.toString() );

uri = new URI("-http://www.test.com/my/path/file.html");
assertEquals( "unknown:", uri.toString() );
}

} // end class
} // end package
} // end package

0 comments on commit 5eaa6f6

Please sign in to comment.