]>
git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
   1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        tests/uris/uris.cpp 
   3 // Purpose:     wxURI unit test 
   7 // Copyright:   (c) 2004 Ryan Norton 
   8 /////////////////////////////////////////////////////////////////////////////// 
  10 // ---------------------------------------------------------------------------- 
  12 // ---------------------------------------------------------------------------- 
  27 // Test wxURL & wxURI compat? 
  28 #define TEST_URL ( 1 && wxUSE_URL ) 
  30 // ---------------------------------------------------------------------------- 
  32 // ---------------------------------------------------------------------------- 
  34 class URITestCase 
: public CppUnit::TestCase
 
  40     CPPUNIT_TEST_SUITE( URITestCase 
); 
  43         CPPUNIT_TEST( Paths 
); 
  44         CPPUNIT_TEST( NormalResolving 
); 
  45         CPPUNIT_TEST( ComplexResolving 
); 
  46         CPPUNIT_TEST( ReallyComplexResolving 
); 
  47         CPPUNIT_TEST( QueryFragmentResolving 
); 
  48         CPPUNIT_TEST( BackwardsResolving 
); 
  49         CPPUNIT_TEST( Assignment 
); 
  50         CPPUNIT_TEST( Comparison 
); 
  51         CPPUNIT_TEST( Unescaping 
); 
  52         CPPUNIT_TEST( FileScheme 
); 
  54         CPPUNIT_TEST( URLCompat 
); 
  55 #if 0 && wxUSE_PROTOCOL_HTTP 
  56         CPPUNIT_TEST( URLProxy  
); 
  59     CPPUNIT_TEST_SUITE_END(); 
  64     void NormalResolving(); 
  65     void ComplexResolving(); 
  66     void ReallyComplexResolving(); 
  67     void QueryFragmentResolving(); 
  68     void BackwardsResolving(); 
  76 #if 0 && wxUSE_PROTOCOL_HTTP 
  81     DECLARE_NO_COPY_CLASS(URITestCase
) 
  84 // register in the unnamed registry so that these tests are run by default 
  85 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase 
); 
  87 // also include in it's own registry so that these tests can be run alone 
  88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" ); 
  90 URITestCase::URITestCase() 
  95 #define URI_TEST(uristring, cond) \ 
  96     uri = new wxURI(wxT(uristring));\ 
  97     CPPUNIT_ASSERT(cond);\ 
 100 #define URI_PRINT(uri)\ 
 101     wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\ 
 102     wxPrintf(wxT("USER:%s\n"), uri.GetUser());\ 
 103     wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\ 
 104     wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\ 
 105     wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\ 
 106     wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\ 
 107     wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment()); 
 109 void URITestCase::IPv4() 
 114     URI_TEST("http://user:password@192.168.1.100:5050/path", 
 115             uri
->GetHostType() == wxURI_IPV4ADDRESS
); 
 117     URI_TEST("http://user:password@192.255.1.100:5050/path", 
 118             uri
->GetHostType() == wxURI_IPV4ADDRESS
); 
 121     URI_TEST("http://user:password@192.256.1.100:5050/path", 
 122             uri
->GetHostType() != wxURI_IPV4ADDRESS
); 
 125 void URITestCase::IPv6() 
 129     // IPv6address   =                            6( h16 ":" ) ls32 
 130     //               /                       "::" 5( h16 ":" ) ls32 
 131     //               / [               h16 ] "::" 4( h16 ":" ) ls32 
 132     //               / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 
 133     //               / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 
 134     //               / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32 
 135     //               / [ *4( h16 ":" ) h16 ] "::"              ls32 
 136     //               / [ *5( h16 ":" ) h16 ] "::"              h16 
 137     //               / [ *6( h16 ":" ) h16 ] "::" 
 138     // ls32          = ( h16 ":" h16 ) / IPv4address 
 140     URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path", 
 141                 uri
->GetHostType() == wxURI_IPV6ADDRESS
); 
 143     URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path", 
 144                 uri
->GetHostType() == wxURI_IPV6ADDRESS
); 
 146     URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path", 
 147                 uri
->GetHostType() == wxURI_IPV6ADDRESS
); 
 149     URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path", 
 150                 uri
->GetHostType() == wxURI_IPV6ADDRESS
); 
 153 void URITestCase::Paths() 
 158     URI_TEST("http://user:password@192.256.1.100:5050/../path", 
 159         uri
->GetPath() == wxT("/path")); 
 161     URI_TEST("http://user:password@192.256.1.100:5050/path/../", 
 162         uri
->GetPath() == wxT("/")); 
 164     URI_TEST("http://user:password@192.256.1.100:5050/path/.", 
 165         uri
->GetPath() == wxT("/path/")); 
 167     URI_TEST("http://user:password@192.256.1.100:5050/path/./", 
 168         uri
->GetPath() == wxT("/path/")); 
 170     URI_TEST("path/john/../../../joe", 
 171         uri
->BuildURI() == wxT("../joe")); 
 175 #define URI_TEST_RESOLVE(string, eq, strict) \ 
 176         uri = new wxURI(wxT(string));\ 
 177         uri->Resolve(masteruri, strict);\ 
 178         CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\ 
 181 #define URI_TEST(string, eq) \ 
 182         URI_TEST_RESOLVE(string, eq, true); 
 185 //examples taken from RFC 2396.bis 
 187 void URITestCase::NormalResolving() 
 189     wxURI 
masteruri(wxT("http://a/b/c/d;p?q")); 
 192     URI_TEST("g:h"  ,"g:h") 
 193     URI_TEST("g"    ,"http://a/b/c/g") 
 194     URI_TEST("./g"  ,"http://a/b/c/g") 
 195     URI_TEST("g/"   ,"http://a/b/c/g/") 
 196     URI_TEST("/g"   ,"http://a/g") 
 197     URI_TEST("//g"  ,"http://g") 
 198     URI_TEST("?y"   ,"http://a/b/c/d;p?y") 
 199     URI_TEST("g?y"  ,"http://a/b/c/g?y") 
 200     URI_TEST("#s"   ,"http://a/b/c/d;p?q#s") 
 201     URI_TEST("g#s"  ,"http://a/b/c/g#s") 
 202     URI_TEST("g?y#s","http://a/b/c/g?y#s") 
 203     URI_TEST(";x"   ,"http://a/b/c/;x") 
 204     URI_TEST("g;x"  ,"http://a/b/c/g;x") 
 205     URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s") 
 207     URI_TEST(""     ,"http://a/b/c/d;p?q") 
 208     URI_TEST("."    ,"http://a/b/c/") 
 209     URI_TEST("./"   ,"http://a/b/c/") 
 210     URI_TEST(".."   ,"http://a/b/") 
 211     URI_TEST("../"  ,"http://a/b/") 
 212     URI_TEST("../g" ,"http://a/b/g") 
 213     URI_TEST("../..","http://a/") 
 214     URI_TEST("../../"        ,  "http://a/") 
 215     URI_TEST("../../g"       ,  "http://a/g") 
 218 void URITestCase::ComplexResolving() 
 220     wxURI 
masteruri(wxT("http://a/b/c/d;p?q")); 
 224     URI_TEST("/./g"   ,"http://a/g") 
 225     URI_TEST("/../g"  ,"http://a/g") 
 226     URI_TEST("g."     ,"http://a/b/c/g.") 
 227     URI_TEST(".g"     ,"http://a/b/c/.g") 
 228     URI_TEST("g.."    ,"http://a/b/c/g..") 
 229     URI_TEST("..g"    ,"http://a/b/c/..g") 
 232    //"../../../g"    =  "http://a/g" 
 233    //"../../../../g" =  "http://a/g" 
 235 void URITestCase::ReallyComplexResolving() 
 237     wxURI 
masteruri(wxT("http://a/b/c/d;p?q")); 
 240     //even more odder path examples 
 241     URI_TEST("./../g" ,"http://a/b/g") 
 242     URI_TEST("./g/."  ,"http://a/b/c/g/") 
 243     URI_TEST("g/./h"  ,"http://a/b/c/g/h") 
 244     URI_TEST("g/../h" ,"http://a/b/c/h") 
 245     URI_TEST("g;x=1/./y"     ,  "http://a/b/c/g;x=1/y") 
 246     URI_TEST("g;x=1/../y"    ,  "http://a/b/c/y") 
 249 void URITestCase::QueryFragmentResolving() 
 251     wxURI 
masteruri(wxT("http://a/b/c/d;p?q")); 
 254     //query/fragment ambigiousness 
 255     URI_TEST("g?y/./x","http://a/b/c/g?y/./x") 
 256     URI_TEST("g?y/../x"      ,  "http://a/b/c/g?y/../x") 
 257     URI_TEST("g#s/./x","http://a/b/c/g#s/./x") 
 258     URI_TEST("g#s/../x"      ,  "http://a/b/c/g#s/../x") 
 261 void URITestCase::BackwardsResolving() 
 263     wxURI 
masteruri(wxT("http://a/b/c/d;p?q")); 
 267     URI_TEST("http:g" ,  "http:g")         //strict 
 269     URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false); 
 272 void URITestCase::Assignment() 
 274     wxURI 
uri1(wxT("http://mysite.com")), 
 275           uri2(wxT("http://mysite2.com")); 
 279     CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI()); 
 282 void URITestCase::Comparison() 
 284     CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com"))); 
 287 void URITestCase::Unescaping() 
 289     wxString orig 
= wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C") 
 290                     wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss"); 
 292     wxString works
= wxURI(orig
).BuildUnescapedURI(); 
 294     CPPUNIT_ASSERT(orig
.IsSameAs(works
) == false); 
 296     wxString orig2 
= wxT("http://test.com/of/file%3A%2F%") 
 297                      wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%") 
 298                      wxT("5Cbroken%5Csadly%5B1%5D.rss"); 
 300     wxString works2 
= wxURI::Unescape(orig2
); 
 301     wxString broken2 
= wxURI(orig2
).BuildUnescapedURI(); 
 303     CPPUNIT_ASSERT(works2
.IsSameAs(broken2
)); 
 307 void URITestCase::FileScheme() 
 309     //file:// variety (NOT CONFORMANT TO THE RFC) 
 310     CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()  
 311                     == wxT("e:/wxcode/script1.xml") ); 
 314     CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()  
 315                     == wxT("/e:/wxcode/script1.xml") ); 
 318     CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()  
 319                     == wxT("/e:/wxcode/script1.xml") ); 
 322     CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()  
 323                     == wxT("e:/wxcode/script1.xml") ); 
 328 const wxChar
* pszProblemUrls
[] = { wxT("http://www.csdn.net"), 
 329                                    wxT("http://www.163.com"), 
 330                                    wxT("http://www.sina.com.cn") }; 
 335 void URITestCase::URLCompat() 
 337     wxURL 
url(wxT("http://user:password@wxwidgets.org")); 
 339     CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
); 
 341     wxInputStream
* pInput 
= url
.GetInputStream(); 
 343     CPPUNIT_ASSERT( pInput 
!= NULL 
); 
 345     CPPUNIT_ASSERT( url 
== wxURL(wxT("http://user:password@wxwidgets.org")) ); 
 347     wxURI 
uri(wxT("http://user:password@wxwidgets.org")); 
 349     CPPUNIT_ASSERT( url 
== uri 
); 
 353     CPPUNIT_ASSERT( urlcopy 
== url 
); 
 354     CPPUNIT_ASSERT( urlcopy 
== uri 
); 
 358     CPPUNIT_ASSERT( uricopy 
== url 
); 
 359     CPPUNIT_ASSERT( uricopy 
== urlcopy 
); 
 360     CPPUNIT_ASSERT( uricopy 
== uri 
); 
 361     CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") ); 
 363     wxURI 
test(wxT("file:\"myf\"ile.txt")); 
 365     CPPUNIT_ASSERT( test
.BuildURI() == wxT("file:%22myf%22ile.txt") ); 
 366     CPPUNIT_ASSERT( test
.GetScheme() == wxT("file") ); 
 367     CPPUNIT_ASSERT( test
.GetPath() == wxT("%22myf%22ile.txt") ); 
 369     // these could be put under a named registry since they take some 
 372     // Test problem urls (reported not to work some time ago by a user...) 
 373     for ( size_t i 
= 0; i 
< WXSIZEOF(pszProblemUrls
); ++i 
) 
 375         wxURL 
urlProblem(pszProblemUrls
[i
]); 
 376         CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
); 
 378         wxInputStream
* is 
= urlProblem
.GetInputStream(); 
 379         CPPUNIT_ASSERT(is 
!= NULL
); 
 381         wxFile 
fOut(_T("test.html"), wxFile::write
); 
 382         wxASSERT(fOut
.IsOpened()); 
 388             size_t n 
= is
->LastRead(); 
 400 // the purpose of this test is unclear, it seems to be unfinished so disabling 
 402 #if 0 && wxUSE_PROTOCOL_HTTP 
 403 void URITestCase::URLProxy() 
 405     wxURL 
url(wxT("http://www.asite.com/index.html")); 
 406     url
.SetProxy(wxT("pserv:3122")); 
 408     wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080")); 
 409     wxURL 
url2(wxT("http://server-name/path/to/file?query_data=value")); 
 410     wxInputStream 
*data 
= url2
.GetInputStream(); 
 411     CPPUNIT_ASSERT(data 
!= NULL
); 
 413 #endif // wxUSE_PROTOCOL_HTTP