]>
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 wxUSE_URL
30 // Define this as 1 to test network connections, this is disabled by default as
31 // some machines running automatic builds don't allow outgoing connections and
33 #define TEST_NETWORK 0
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class URITestCase
: public CppUnit::TestCase
45 CPPUNIT_TEST_SUITE( URITestCase
);
48 CPPUNIT_TEST( Paths
);
49 CPPUNIT_TEST( NormalResolving
);
50 CPPUNIT_TEST( ComplexResolving
);
51 CPPUNIT_TEST( ReallyComplexResolving
);
52 CPPUNIT_TEST( QueryFragmentResolving
);
53 CPPUNIT_TEST( BackwardsResolving
);
54 CPPUNIT_TEST( Assignment
);
55 CPPUNIT_TEST( Comparison
);
56 CPPUNIT_TEST( Unescaping
);
57 CPPUNIT_TEST( FileScheme
);
59 CPPUNIT_TEST( URLCompat
);
60 #if 0 && wxUSE_PROTOCOL_HTTP
61 CPPUNIT_TEST( URLProxy
);
64 CPPUNIT_TEST_SUITE_END();
69 void NormalResolving();
70 void ComplexResolving();
71 void ReallyComplexResolving();
72 void QueryFragmentResolving();
73 void BackwardsResolving();
81 #if 0 && wxUSE_PROTOCOL_HTTP
86 DECLARE_NO_COPY_CLASS(URITestCase
)
89 // register in the unnamed registry so that these tests are run by default
90 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
92 // also include in it's own registry so that these tests can be run alone
93 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
95 URITestCase::URITestCase()
100 #define URI_TEST(uristring, cond) \
101 uri = new wxURI(wxT(uristring));\
102 CPPUNIT_ASSERT(cond);\
105 #define URI_PRINT(uri)\
106 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
107 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
108 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
109 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
110 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
111 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
112 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
114 void URITestCase::IPv4()
119 URI_TEST("http://user:password@192.168.1.100:5050/path",
120 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
122 URI_TEST("http://user:password@192.255.1.100:5050/path",
123 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
126 URI_TEST("http://user:password@192.256.1.100:5050/path",
127 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
130 void URITestCase::IPv6()
134 // IPv6address = 6( h16 ":" ) ls32
135 // / "::" 5( h16 ":" ) ls32
136 // / [ h16 ] "::" 4( h16 ":" ) ls32
137 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
138 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
139 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
140 // / [ *4( h16 ":" ) h16 ] "::" ls32
141 // / [ *5( h16 ":" ) h16 ] "::" h16
142 // / [ *6( h16 ":" ) h16 ] "::"
143 // ls32 = ( h16 ":" h16 ) / IPv4address
145 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
146 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
148 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
149 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
151 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
152 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
154 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
155 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
158 void URITestCase::Paths()
163 URI_TEST("http://user:password@192.256.1.100:5050/../path",
164 uri
->GetPath() == wxT("/path"));
166 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
167 uri
->GetPath() == wxT("/"));
169 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
170 uri
->GetPath() == wxT("/path/"));
172 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
173 uri
->GetPath() == wxT("/path/"));
175 URI_TEST("path/john/../../../joe",
176 uri
->BuildURI() == wxT("../joe"));
180 #define URI_TEST_RESOLVE(string, eq, strict) \
181 uri = new wxURI(wxT(string));\
182 uri->Resolve(masteruri, strict);\
183 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
186 #define URI_TEST(string, eq) \
187 URI_TEST_RESOLVE(string, eq, true);
190 //examples taken from RFC 2396.bis
192 void URITestCase::NormalResolving()
194 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
197 URI_TEST("g:h" ,"g:h")
198 URI_TEST("g" ,"http://a/b/c/g")
199 URI_TEST("./g" ,"http://a/b/c/g")
200 URI_TEST("g/" ,"http://a/b/c/g/")
201 URI_TEST("/g" ,"http://a/g")
202 URI_TEST("//g" ,"http://g")
203 URI_TEST("?y" ,"http://a/b/c/d;p?y")
204 URI_TEST("g?y" ,"http://a/b/c/g?y")
205 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
206 URI_TEST("g#s" ,"http://a/b/c/g#s")
207 URI_TEST("g?y#s","http://a/b/c/g?y#s")
208 URI_TEST(";x" ,"http://a/b/c/;x")
209 URI_TEST("g;x" ,"http://a/b/c/g;x")
210 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
212 URI_TEST("" ,"http://a/b/c/d;p?q")
213 URI_TEST("." ,"http://a/b/c/")
214 URI_TEST("./" ,"http://a/b/c/")
215 URI_TEST(".." ,"http://a/b/")
216 URI_TEST("../" ,"http://a/b/")
217 URI_TEST("../g" ,"http://a/b/g")
218 URI_TEST("../..","http://a/")
219 URI_TEST("../../" , "http://a/")
220 URI_TEST("../../g" , "http://a/g")
223 void URITestCase::ComplexResolving()
225 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
229 URI_TEST("/./g" ,"http://a/g")
230 URI_TEST("/../g" ,"http://a/g")
231 URI_TEST("g." ,"http://a/b/c/g.")
232 URI_TEST(".g" ,"http://a/b/c/.g")
233 URI_TEST("g.." ,"http://a/b/c/g..")
234 URI_TEST("..g" ,"http://a/b/c/..g")
237 //"../../../g" = "http://a/g"
238 //"../../../../g" = "http://a/g"
240 void URITestCase::ReallyComplexResolving()
242 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
245 //even more odder path examples
246 URI_TEST("./../g" ,"http://a/b/g")
247 URI_TEST("./g/." ,"http://a/b/c/g/")
248 URI_TEST("g/./h" ,"http://a/b/c/g/h")
249 URI_TEST("g/../h" ,"http://a/b/c/h")
250 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
251 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
254 void URITestCase::QueryFragmentResolving()
256 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
259 //query/fragment ambigiousness
260 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
261 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
262 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
263 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
266 void URITestCase::BackwardsResolving()
268 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
272 URI_TEST("http:g" , "http:g") //strict
274 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
277 void URITestCase::Assignment()
279 wxURI
uri1(wxT("http://mysite.com")),
280 uri2(wxT("http://mysite2.com"));
284 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
287 void URITestCase::Comparison()
289 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
292 void URITestCase::Unescaping()
294 wxString orig
= wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
295 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
297 wxString works
= wxURI(orig
).BuildUnescapedURI();
299 CPPUNIT_ASSERT(orig
.IsSameAs(works
) == false);
301 wxString orig2
= wxT("http://test.com/of/file%3A%2F%")
302 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
303 wxT("5Cbroken%5Csadly%5B1%5D.rss");
305 wxString works2
= wxURI::Unescape(orig2
);
306 wxString broken2
= wxURI(orig2
).BuildUnescapedURI();
308 CPPUNIT_ASSERT(works2
.IsSameAs(broken2
));
312 void URITestCase::FileScheme()
314 //file:// variety (NOT CONFORMANT TO THE RFC)
315 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
316 == wxT("e:/wxcode/script1.xml") );
319 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
320 == wxT("/e:/wxcode/script1.xml") );
323 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
324 == wxT("/e:/wxcode/script1.xml") );
327 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
328 == wxT("e:/wxcode/script1.xml") );
333 const wxChar
* pszProblemUrls
[] = { wxT("http://www.csdn.net"),
334 wxT("http://www.163.com"),
335 wxT("http://www.sina.com.cn") };
340 void URITestCase::URLCompat()
342 wxURL
url(wxT("http://user:password@wxwidgets.org"));
344 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
347 wxInputStream
* pInput
= url
.GetInputStream();
349 CPPUNIT_ASSERT( pInput
!= NULL
);
352 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
354 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
356 CPPUNIT_ASSERT( url
== uri
);
360 CPPUNIT_ASSERT( urlcopy
== url
);
361 CPPUNIT_ASSERT( urlcopy
== uri
);
365 CPPUNIT_ASSERT( uricopy
== url
);
366 CPPUNIT_ASSERT( uricopy
== urlcopy
);
367 CPPUNIT_ASSERT( uricopy
== uri
);
368 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
370 wxURI
test(wxT("file:\"myf\"ile.txt"));
372 CPPUNIT_ASSERT( test
.BuildURI() == wxT("file:%22myf%22ile.txt") );
373 CPPUNIT_ASSERT( test
.GetScheme() == wxT("file") );
374 CPPUNIT_ASSERT( test
.GetPath() == wxT("%22myf%22ile.txt") );
376 // these could be put under a named registry since they take some
379 // Test problem urls (reported not to work some time ago by a user...)
380 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
382 wxURL
urlProblem(pszProblemUrls
[i
]);
383 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
385 wxInputStream
* is
= urlProblem
.GetInputStream();
386 CPPUNIT_ASSERT(is
!= NULL
);
388 wxFile
fOut(_T("test.html"), wxFile::write
);
389 wxASSERT(fOut
.IsOpened());
395 size_t n
= is
->LastRead();
407 // the purpose of this test is unclear, it seems to be unfinished so disabling
409 #if 0 && wxUSE_PROTOCOL_HTTP
410 void URITestCase::URLProxy()
412 wxURL
url(wxT("http://www.asite.com/index.html"));
413 url
.SetProxy(wxT("pserv:3122"));
415 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
416 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
417 wxInputStream
*data
= url2
.GetInputStream();
418 CPPUNIT_ASSERT(data
!= NULL
);
420 #endif // wxUSE_PROTOCOL_HTTP