]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/uris/uris.cpp
Adding technotes about the release system and about adding apps/screenshots to the...
[wxWidgets.git] / tests / uris / uris.cpp
index 69a9128d9c29fe512174801aa56f1820918e9509..1d05f04541f2563b69c925cb692ef32a47f77e85 100644 (file)
@@ -11,7 +11,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
-#include "wx/wxprec.h"
+#include "testprec.h"
 
 #ifdef __BORLANDC__
     #pragma hdrstop
 #endif // WX_PRECOMP
 
 #include "wx/uri.h"
+#include "wx/url.h"
 
-#include "wx/cppunit.h"
+// Test wxURL & wxURI compat?
+#define TEST_URL ( 1 && wxUSE_URL )
 
 // ----------------------------------------------------------------------------
 // test class
@@ -46,6 +48,14 @@ private:
         CPPUNIT_TEST( BackwardsResolving );
         CPPUNIT_TEST( Assignment );
         CPPUNIT_TEST( Comparison );
+        CPPUNIT_TEST( Unescaping );
+        CPPUNIT_TEST( FileScheme );
+#if TEST_URL
+        CPPUNIT_TEST( URLCompat );
+#if wxUSE_PROTOCOL_HTTP
+        CPPUNIT_TEST( URLProxy  );
+#endif
+#endif
     CPPUNIT_TEST_SUITE_END();
 
     void IPv4();
@@ -58,6 +68,13 @@ private:
     void BackwardsResolving();
     void Assignment();
     void Comparison();
+    void Unescaping();
+    void FileScheme();
+
+#if TEST_URL
+    void URLCompat();
+    void URLProxy();
+#endif
 
     DECLARE_NO_COPY_CLASS(URITestCase)
 };
@@ -92,7 +109,7 @@ void URITestCase::IPv4()
     wxURI* uri;
 
 
-    URI_TEST("http://user:password@192.168.1.100:5050/path", 
+    URI_TEST("http://user:password@192.168.1.100:5050/path",
             uri->GetHostType() == wxURI_IPV4ADDRESS);
 
     URI_TEST("http://user:password@192.255.1.100:5050/path",
@@ -117,17 +134,17 @@ void URITestCase::IPv6()
     //               / [ *5( h16 ":" ) h16 ] "::"              h16
     //               / [ *6( h16 ":" ) h16 ] "::"
     // ls32          = ( h16 ":" h16 ) / IPv4address
-    
-    URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path", 
+
+    URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
                 uri->GetHostType() == wxURI_IPV6ADDRESS);
 
-    URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path", 
+    URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
                 uri->GetHostType() == wxURI_IPV6ADDRESS);
 
-    URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path", 
+    URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
                 uri->GetHostType() == wxURI_IPV6ADDRESS);
 
-    URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path", 
+    URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
                 uri->GetHostType() == wxURI_IPV6ADDRESS);
 }
 
@@ -136,27 +153,27 @@ void URITestCase::Paths()
     wxURI* uri;
 
     //path tests
-    URI_TEST("http://user:password@192.256.1.100:5050/../path", 
+    URI_TEST("http://user:password@192.256.1.100:5050/../path",
         uri->GetPath() == wxT("/path"));
 
-    URI_TEST("http://user:password@192.256.1.100:5050/path/../", 
+    URI_TEST("http://user:password@192.256.1.100:5050/path/../",
         uri->GetPath() == wxT("/"));
 
-    URI_TEST("http://user:password@192.256.1.100:5050/path/.", 
+    URI_TEST("http://user:password@192.256.1.100:5050/path/.",
         uri->GetPath() == wxT("/path/"));
 
-    URI_TEST("http://user:password@192.256.1.100:5050/path/./", 
+    URI_TEST("http://user:password@192.256.1.100:5050/path/./",
         uri->GetPath() == wxT("/path/"));
-    URI_TEST("path/john/../../../joe", 
-        uri->Get() == wxT("../joe"));
+
+    URI_TEST("path/john/../../../joe",
+        uri->BuildURI() == wxT("../joe"));
 }
 #undef URI_TEST
 
 #define URI_TEST_RESOLVE(string, eq, strict) \
         uri = new wxURI(wxT(string));\
         uri->Resolve(masteruri, strict);\
-        CPPUNIT_ASSERT(uri->Get() == wxT(eq));\
+        CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
         delete uri;
 
 #define URI_TEST(string, eq) \
@@ -169,31 +186,31 @@ void URITestCase::NormalResolving()
 {
     wxURI masteruri(wxT("http://a/b/c/d;p?q"));
     wxURI* uri;
-    
+
     URI_TEST("g:h"  ,"g:h")
-   URI_TEST("g"    ,"http://a/b/c/g")
-   URI_TEST("./g"  ,"http://a/b/c/g")
-   URI_TEST("g/"   ,"http://a/b/c/g/")
-   URI_TEST("/g"   ,"http://a/g")
-   URI_TEST("//g"  ,"http://g")
-   URI_TEST("?y"   ,"http://a/b/c/d;p?y")
+    URI_TEST("g"    ,"http://a/b/c/g")
+    URI_TEST("./g"  ,"http://a/b/c/g")
+    URI_TEST("g/"   ,"http://a/b/c/g/")
+    URI_TEST("/g"   ,"http://a/g")
+    URI_TEST("//g"  ,"http://g")
+    URI_TEST("?y"   ,"http://a/b/c/d;p?y")
     URI_TEST("g?y"  ,"http://a/b/c/g?y")
-  URI_TEST("#s"   ,"http://a/b/c/d;p?q#s")
-       URI_TEST("g#s"  ,"http://a/b/c/g#s")
-   URI_TEST("g?y#s","http://a/b/c/g?y#s")
-   URI_TEST(";x"   ,"http://a/b/c/;x")
-   URI_TEST("g;x"  ,"http://a/b/c/g;x")
-  URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
-
-   URI_TEST(""     ,"http://a/b/c/d;p?q")
-   URI_TEST("."    ,"http://a/b/c/")
-   URI_TEST("./"   ,"http://a/b/c/")
-   URI_TEST(".."   ,"http://a/b/")
-   URI_TEST("../"  ,"http://a/b/")
-   URI_TEST("../g" ,"http://a/b/g")
-   URI_TEST("../..","http://a/")
-   URI_TEST("../../"        ,  "http://a/")
-   URI_TEST("../../g"       ,  "http://a/g")
+    URI_TEST("#s"   ,"http://a/b/c/d;p?q#s")
+    URI_TEST("g#s"  ,"http://a/b/c/g#s")
+    URI_TEST("g?y#s","http://a/b/c/g?y#s")
+    URI_TEST(";x"   ,"http://a/b/c/;x")
+    URI_TEST("g;x"  ,"http://a/b/c/g;x")
+    URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
+
+    URI_TEST(""     ,"http://a/b/c/d;p?q")
+    URI_TEST("."    ,"http://a/b/c/")
+    URI_TEST("./"   ,"http://a/b/c/")
+    URI_TEST(".."   ,"http://a/b/")
+    URI_TEST("../"  ,"http://a/b/")
+    URI_TEST("../g" ,"http://a/b/g")
+    URI_TEST("../..","http://a/")
+    URI_TEST("../../"        ,  "http://a/")
+    URI_TEST("../../g"       ,  "http://a/g")
 }
 
 void URITestCase::ComplexResolving()
@@ -202,12 +219,12 @@ void URITestCase::ComplexResolving()
     wxURI* uri;
 
     //odd path examples
-   URI_TEST("/./g"   ,"http://a/g")
-   URI_TEST("/../g"  ,"http://a/g")
-   URI_TEST("g."     ,"http://a/b/c/g.")
-   URI_TEST(".g"     ,"http://a/b/c/.g")
-   URI_TEST("g.."    ,"http://a/b/c/g..")
-   URI_TEST("..g"    ,"http://a/b/c/..g")
+    URI_TEST("/./g"   ,"http://a/g")
+    URI_TEST("/../g"  ,"http://a/g")
+    URI_TEST("g."     ,"http://a/b/c/g.")
+    URI_TEST(".g"     ,"http://a/b/c/.g")
+    URI_TEST("g.."    ,"http://a/b/c/g..")
+    URI_TEST("..g"    ,"http://a/b/c/..g")
 }
    //Should Fail
    //"../../../g"    =  "http://a/g"
@@ -219,12 +236,12 @@ void URITestCase::ReallyComplexResolving()
     wxURI* uri;
 
     //even more odder path examples
-   URI_TEST("./../g" ,"http://a/b/g")
-   URI_TEST("./g/."  ,"http://a/b/c/g/")
-   URI_TEST("g/./h"  ,"http://a/b/c/g/h")
-   URI_TEST("g/../h" ,"http://a/b/c/h")
-   URI_TEST("g;x=1/./y"     ,  "http://a/b/c/g;x=1/y")
-   URI_TEST("g;x=1/../y"    ,  "http://a/b/c/y")
+    URI_TEST("./../g" ,"http://a/b/g")
+    URI_TEST("./g/."  ,"http://a/b/c/g/")
+    URI_TEST("g/./h"  ,"http://a/b/c/g/h")
+    URI_TEST("g/../h" ,"http://a/b/c/h")
+    URI_TEST("g;x=1/./y"     ,  "http://a/b/c/g;x=1/y")
+    URI_TEST("g;x=1/../y"    ,  "http://a/b/c/y")
 }
 
 void URITestCase::QueryFragmentResolving()
@@ -232,11 +249,11 @@ void URITestCase::QueryFragmentResolving()
     wxURI masteruri(wxT("http://a/b/c/d;p?q"));
     wxURI* uri;
 
-   //query/fragment ambigiousness
-   URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
-   URI_TEST("g?y/../x"      ,  "http://a/b/c/g?y/../x")
-   URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
-   URI_TEST("g#s/../x"      ,  "http://a/b/c/g#s/../x")
+    //query/fragment ambigiousness
+    URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
+    URI_TEST("g?y/../x"      ,  "http://a/b/c/g?y/../x")
+    URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
+    URI_TEST("g#s/../x"      ,  "http://a/b/c/g#s/../x")
 }
 
 void URITestCase::BackwardsResolving()
@@ -244,10 +261,10 @@ void URITestCase::BackwardsResolving()
     wxURI masteruri(wxT("http://a/b/c/d;p?q"));
     wxURI* uri;
 
-   //"NEW" 
-   URI_TEST("http:g" ,  "http:g")         //strict
-   //bw compat
-   URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false); 
+    //"NEW"
+    URI_TEST("http:g" ,  "http:g")         //strict
+    //bw compat
+    URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
 }
 
 void URITestCase::Assignment()
@@ -257,10 +274,141 @@ void URITestCase::Assignment()
 
     uri2 = uri1;
 
-    CPPUNIT_ASSERT(uri1.Get() == uri2.Get());
+    CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
 }
 
 void URITestCase::Comparison()
 {
     CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
 }
+
+void URITestCase::Unescaping()
+{
+    wxString orig = wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
+                    wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
+
+    wxString works= wxURI(orig).BuildUnescapedURI();
+
+    CPPUNIT_ASSERT(orig.IsSameAs(works) == false);
+
+    wxString orig2 = wxT("http://test.com/of/file%3A%2F%")
+                     wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
+                     wxT("5Cbroken%5Csadly%5B1%5D.rss");
+
+    wxString works2 = wxURI::Unescape(orig2);
+    wxString broken2 = wxURI(orig2).BuildUnescapedURI();
+
+    CPPUNIT_ASSERT(works2.IsSameAs(broken2));
+
+}
+
+void URITestCase::FileScheme()
+{
+    //file:// variety (NOT CONFORMANT TO THE RFC)
+    CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath() 
+                    == wxT("e:/wxcode/script1.xml") );
+
+    //file:/// variety
+    CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath() 
+                    == wxT("/e:/wxcode/script1.xml") );
+
+    //file:/ variety
+    CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath() 
+                    == wxT("/e:/wxcode/script1.xml") );
+
+    //file: variety
+    CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath() 
+                    == wxT("e:/wxcode/script1.xml") );
+}
+
+#if TEST_URL
+
+const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
+                                   wxT("http://www.163.com"),
+                                   wxT("http://www.sina.com.cn") };
+
+#include "wx/url.h"
+#include "wx/file.h"
+
+void URITestCase::URLCompat()
+{
+    wxURL url(wxT("http://user:password@wxwidgets.org"));
+
+    CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
+
+    wxInputStream* pInput = url.GetInputStream();
+
+    CPPUNIT_ASSERT( pInput != NULL );
+
+    CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
+
+    wxURI uri(wxT("http://user:password@wxwidgets.org"));
+
+    CPPUNIT_ASSERT( url == uri );
+
+    wxURL urlcopy(uri);
+
+    CPPUNIT_ASSERT( urlcopy == url );
+    CPPUNIT_ASSERT( urlcopy == uri );
+
+    wxURI uricopy(url);
+
+    CPPUNIT_ASSERT( uricopy == url );
+    CPPUNIT_ASSERT( uricopy == urlcopy );
+    CPPUNIT_ASSERT( uricopy == uri );
+#if WXWIN_COMPATIBILITY_2_4
+    CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
+#endif
+    CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
+
+    wxURI test(wxT("file:\"myf\"ile.txt"));
+
+    CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
+    CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
+    CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
+
+    // these could be put under a named registry since they take some
+    // time to complete
+#if 0
+    // Test problem urls (reported not to work some time ago by a user...)
+    for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
+    {
+        wxURL urlProblem(pszProblemUrls[i]);
+        CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
+
+        wxInputStream* is = urlProblem.GetInputStream();
+        CPPUNIT_ASSERT(is != NULL);
+
+        wxFile fOut(_T("test.html"), wxFile::write);
+        wxASSERT(fOut.IsOpened());
+
+        char buf[1001];
+        for( ;; )
+        {
+            is->Read(buf, 1000);
+            size_t n = is->LastRead();
+            if ( n == 0 )
+                break;
+            buf[n] = 0;
+            fOut.Write(buf, n);
+        }
+
+        delete is;
+    }
+#endif
+}
+
+#if wxUSE_PROTOCOL_HTTP
+void URITestCase::URLProxy()
+{
+    wxURL url(wxT("http://www.asite.com/index.html"));
+    url.SetProxy(wxT("pserv:3122"));
+
+    wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
+    wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
+    wxInputStream *data = url2.GetInputStream();
+    CPPUNIT_ASSERT(data != NULL);
+}
+#endif // wxUSE_PROTOCOL_HTTP
+
+#endif // TEST_URL