]>
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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
27 #include "wx/cppunit.h"
29 // Test wxURL & wxURI compat?
30 #define TEST_URL ( 1 && wxUSE_URL )
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class URITestCase
: public CppUnit::TestCase
42 CPPUNIT_TEST_SUITE( URITestCase
);
45 CPPUNIT_TEST( Paths
);
46 CPPUNIT_TEST( NormalResolving
);
47 CPPUNIT_TEST( ComplexResolving
);
48 CPPUNIT_TEST( ReallyComplexResolving
);
49 CPPUNIT_TEST( QueryFragmentResolving
);
50 CPPUNIT_TEST( BackwardsResolving
);
51 CPPUNIT_TEST( Assignment
);
52 CPPUNIT_TEST( Comparison
);
54 CPPUNIT_TEST( URLCompat
);
56 CPPUNIT_TEST_SUITE_END();
61 void NormalResolving();
62 void ComplexResolving();
63 void ReallyComplexResolving();
64 void QueryFragmentResolving();
65 void BackwardsResolving();
73 DECLARE_NO_COPY_CLASS(URITestCase
)
76 // register in the unnamed registry so that these tests are run by default
77 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
79 // also include in it's own registry so that these tests can be run alone
80 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
82 URITestCase::URITestCase()
87 #define URI_TEST(uristring, cond) \
88 uri = new wxURI(wxT(uristring));\
89 CPPUNIT_ASSERT(cond);\
92 #define URI_PRINT(uri)\
93 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
94 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
95 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
96 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
97 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
98 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
99 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
101 void URITestCase::IPv4()
106 URI_TEST("http://user:password@192.168.1.100:5050/path",
107 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
109 URI_TEST("http://user:password@192.255.1.100:5050/path",
110 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
113 URI_TEST("http://user:password@192.256.1.100:5050/path",
114 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
117 void URITestCase::IPv6()
121 // IPv6address = 6( h16 ":" ) ls32
122 // / "::" 5( h16 ":" ) ls32
123 // / [ h16 ] "::" 4( h16 ":" ) ls32
124 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
125 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
126 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
127 // / [ *4( h16 ":" ) h16 ] "::" ls32
128 // / [ *5( h16 ":" ) h16 ] "::" h16
129 // / [ *6( h16 ":" ) h16 ] "::"
130 // ls32 = ( h16 ":" h16 ) / IPv4address
132 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
133 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
135 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
136 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
138 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
139 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
141 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
142 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
145 void URITestCase::Paths()
150 URI_TEST("http://user:password@192.256.1.100:5050/../path",
151 uri
->GetPath() == wxT("/path"));
153 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
154 uri
->GetPath() == wxT("/"));
156 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
157 uri
->GetPath() == wxT("/path/"));
159 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
160 uri
->GetPath() == wxT("/path/"));
162 URI_TEST("path/john/../../../joe",
163 uri
->BuildURI() == wxT("../joe"));
167 #define URI_TEST_RESOLVE(string, eq, strict) \
168 uri = new wxURI(wxT(string));\
169 uri->Resolve(masteruri, strict);\
170 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
173 #define URI_TEST(string, eq) \
174 URI_TEST_RESOLVE(string, eq, true);
177 //examples taken from RFC 2396.bis
179 void URITestCase::NormalResolving()
181 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
184 URI_TEST("g:h" ,"g:h")
185 URI_TEST("g" ,"http://a/b/c/g")
186 URI_TEST("./g" ,"http://a/b/c/g")
187 URI_TEST("g/" ,"http://a/b/c/g/")
188 URI_TEST("/g" ,"http://a/g")
189 URI_TEST("//g" ,"http://g")
190 URI_TEST("?y" ,"http://a/b/c/d;p?y")
191 URI_TEST("g?y" ,"http://a/b/c/g?y")
192 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
193 URI_TEST("g#s" ,"http://a/b/c/g#s")
194 URI_TEST("g?y#s","http://a/b/c/g?y#s")
195 URI_TEST(";x" ,"http://a/b/c/;x")
196 URI_TEST("g;x" ,"http://a/b/c/g;x")
197 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
199 URI_TEST("" ,"http://a/b/c/d;p?q")
200 URI_TEST("." ,"http://a/b/c/")
201 URI_TEST("./" ,"http://a/b/c/")
202 URI_TEST(".." ,"http://a/b/")
203 URI_TEST("../" ,"http://a/b/")
204 URI_TEST("../g" ,"http://a/b/g")
205 URI_TEST("../..","http://a/")
206 URI_TEST("../../" , "http://a/")
207 URI_TEST("../../g" , "http://a/g")
210 void URITestCase::ComplexResolving()
212 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
216 URI_TEST("/./g" ,"http://a/g")
217 URI_TEST("/../g" ,"http://a/g")
218 URI_TEST("g." ,"http://a/b/c/g.")
219 URI_TEST(".g" ,"http://a/b/c/.g")
220 URI_TEST("g.." ,"http://a/b/c/g..")
221 URI_TEST("..g" ,"http://a/b/c/..g")
224 //"../../../g" = "http://a/g"
225 //"../../../../g" = "http://a/g"
227 void URITestCase::ReallyComplexResolving()
229 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
232 //even more odder path examples
233 URI_TEST("./../g" ,"http://a/b/g")
234 URI_TEST("./g/." ,"http://a/b/c/g/")
235 URI_TEST("g/./h" ,"http://a/b/c/g/h")
236 URI_TEST("g/../h" ,"http://a/b/c/h")
237 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
238 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
241 void URITestCase::QueryFragmentResolving()
243 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
246 //query/fragment ambigiousness
247 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
248 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
249 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
250 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
253 void URITestCase::BackwardsResolving()
255 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
259 URI_TEST("http:g" , "http:g") //strict
261 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
264 void URITestCase::Assignment()
266 wxURI
uri1(wxT("http://mysite.com")),
267 uri2(wxT("http://mysite2.com"));
271 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
274 void URITestCase::Comparison()
276 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
283 void URITestCase::URLCompat()
285 wxURL
url(wxT("http://user:password@wxwidgets.org"));
287 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
289 wxInputStream
* pInput
= url
.GetInputStream();
291 CPPUNIT_ASSERT( pInput
!= NULL
);
293 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
295 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
297 CPPUNIT_ASSERT( url
== uri
);
301 CPPUNIT_ASSERT( urlcopy
== url
);
302 CPPUNIT_ASSERT( urlcopy
== uri
);
306 CPPUNIT_ASSERT( uricopy
== url
);
307 CPPUNIT_ASSERT( uricopy
== urlcopy
);
308 CPPUNIT_ASSERT( uricopy
== uri
);
309 #if WXWIN_COMPATIBILITY_2_4
310 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );