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( Server
);
49 CPPUNIT_TEST( Paths
);
50 CPPUNIT_TEST( UserAndPass
);
51 CPPUNIT_TEST( NormalResolving
);
52 CPPUNIT_TEST( ComplexResolving
);
53 CPPUNIT_TEST( ReallyComplexResolving
);
54 CPPUNIT_TEST( QueryFragmentResolving
);
55 CPPUNIT_TEST( BackwardsResolving
);
56 CPPUNIT_TEST( Assignment
);
57 CPPUNIT_TEST( Comparison
);
58 CPPUNIT_TEST( Unescaping
);
59 CPPUNIT_TEST( FileScheme
);
61 CPPUNIT_TEST( URLCompat
);
62 #if 0 && wxUSE_PROTOCOL_HTTP
63 CPPUNIT_TEST( URLProxy
);
66 CPPUNIT_TEST_SUITE_END();
73 void NormalResolving();
74 void ComplexResolving();
75 void ReallyComplexResolving();
76 void QueryFragmentResolving();
77 void BackwardsResolving();
85 #if 0 && wxUSE_PROTOCOL_HTTP
90 DECLARE_NO_COPY_CLASS(URITestCase
)
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
96 // also include in it's own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
99 URITestCase::URITestCase()
103 // apply the given accessor to the URI, check that the result is as expected
104 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
105 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
107 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
110 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
113 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
114 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
116 #define URI_ASSERT_USER_EQUAL(uri, expected) \
117 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
119 void URITestCase::IPv4()
121 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
124 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
128 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
129 GetHostType() != wxURI_IPV4ADDRESS
);
132 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_ASSERT_HOSTTYPE_EQUAL
147 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
151 URI_ASSERT_HOSTTYPE_EQUAL
153 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
157 URI_ASSERT_HOSTTYPE_EQUAL
159 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
163 URI_ASSERT_HOSTTYPE_EQUAL
165 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
170 void URITestCase::Server()
172 URI_ASSERT_SERVER_EQUAL("http://foo/", "foo");
173 URI_ASSERT_SERVER_EQUAL("http://foo-bar/", "foo-bar");
174 URI_ASSERT_SERVER_EQUAL("http://foo/bar/", "foo");
175 URI_ASSERT_SERVER_EQUAL("http://192.168.1.0/", "192.168.1.0");
176 URI_ASSERT_SERVER_EQUAL("http://192.168.1.17/", "192.168.1.17");
177 URI_ASSERT_SERVER_EQUAL("http://192.168.1.255/", "192.168.1.255");
178 URI_ASSERT_SERVER_EQUAL("http://192.168.1.1/index.html", "192.168.1.1");
179 URI_ASSERT_SERVER_EQUAL("http://[aa:aa:aa:aa::aa:aa]/foo", "aa:aa:aa:aa::aa:aa");
182 void URITestCase::Paths()
184 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
187 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
190 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
193 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
196 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
197 "../joe", BuildURI());
200 void URITestCase::UserAndPass()
202 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
203 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
204 URI_ASSERT_USER_EQUAL("http://host/path/", "");
207 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
210 uri.Resolve(masteruri, strict); \
211 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
214 #define URI_TEST_RESOLVE(string, eq) \
215 URI_TEST_RESOLVE_IMPL(string, eq, true);
217 #define URI_TEST_RESOLVE_LAX(string, eq) \
218 URI_TEST_RESOLVE_IMPL(string, eq, false);
221 //examples taken from RFC 2396.bis
223 void URITestCase::NormalResolving()
225 wxURI
masteruri("http://a/b/c/d;p?q");
227 URI_TEST_RESOLVE("g:h" ,"g:h")
228 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
229 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
230 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
231 URI_TEST_RESOLVE("/g" ,"http://a/g")
232 URI_TEST_RESOLVE("//g" ,"http://g")
233 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
234 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
235 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
236 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
237 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
238 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
239 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
240 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
242 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
243 URI_TEST_RESOLVE("." ,"http://a/b/c/")
244 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
245 URI_TEST_RESOLVE(".." ,"http://a/b/")
246 URI_TEST_RESOLVE("../" ,"http://a/b/")
247 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
248 URI_TEST_RESOLVE("../..","http://a/")
249 URI_TEST_RESOLVE("../../" , "http://a/")
250 URI_TEST_RESOLVE("../../g" , "http://a/g")
253 void URITestCase::ComplexResolving()
255 wxURI
masteruri("http://a/b/c/d;p?q");
258 URI_TEST_RESOLVE("../../../g" , "http://a/g")
259 URI_TEST_RESOLVE("../../../../g", "http://a/g")
261 URI_TEST_RESOLVE("/./g" ,"http://a/g")
262 URI_TEST_RESOLVE("/../g" ,"http://a/g")
263 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
264 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
265 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
266 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
269 void URITestCase::ReallyComplexResolving()
271 wxURI
masteruri("http://a/b/c/d;p?q");
273 //even more odder path examples
274 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
275 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
276 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
277 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
278 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
279 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
282 void URITestCase::QueryFragmentResolving()
284 wxURI
masteruri("http://a/b/c/d;p?q");
286 //query/fragment ambigiousness
287 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
288 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
289 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
290 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
293 void URITestCase::BackwardsResolving()
295 wxURI
masteruri("http://a/b/c/d;p?q");
298 URI_TEST_RESOLVE("http:g" , "http:g") //strict
300 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
303 void URITestCase::Assignment()
305 wxURI
uri1("http://mysite.com"),
306 uri2("http://mysite2.com");
310 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
313 void URITestCase::Comparison()
315 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
318 void URITestCase::Unescaping()
323 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
324 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
326 unescaped
= wxURI(escaped
).BuildUnescapedURI();
328 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
329 "escaping\\that\\seems\\broken\\sadly[1].rss",
332 CPPUNIT_ASSERT_EQUAL( unescaped
, wxURI::Unescape(escaped
) );
335 escaped
= "http://ru.wikipedia.org/wiki/"
336 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
338 unescaped
= wxURI::Unescape(escaped
);
340 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
341 "http://ru.wikipedia.org/wiki/"
342 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
343 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
348 void URITestCase::FileScheme()
350 //file:// variety (NOT CONFORMING TO THE RFC)
351 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
352 "e:/wxcode/script1.xml" );
355 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
356 "/e:/wxcode/script1.xml" );
359 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
360 "/e:/wxcode/script1.xml" );
363 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
364 "e:/wxcode/script1.xml" );
372 void URITestCase::URLCompat()
374 wxURL
url("http://user:password@wxwidgets.org");
376 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
379 wxInputStream
* pInput
= url
.GetInputStream();
381 CPPUNIT_ASSERT( pInput
!= NULL
);
384 CPPUNIT_ASSERT( url
== wxURL("http://user:password@wxwidgets.org") );
386 wxURI
uri("http://user:password@wxwidgets.org");
388 CPPUNIT_ASSERT( url
== uri
);
392 CPPUNIT_ASSERT( urlcopy
== url
);
393 CPPUNIT_ASSERT( urlcopy
== uri
);
397 CPPUNIT_ASSERT( uricopy
== url
);
398 CPPUNIT_ASSERT( uricopy
== urlcopy
);
399 CPPUNIT_ASSERT( uricopy
== uri
);
400 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
402 wxURI
test("file:\"myf\"ile.txt");
404 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test
.BuildURI() );
405 CPPUNIT_ASSERT_EQUAL( "file", test
.GetScheme() );
406 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test
.GetPath() );
408 // these could be put under a named registry since they take some
411 // Test problem urls (reported not to work some time ago by a user...)
412 const wxChar
* pszProblemUrls
[] = { "http://www.csdn.net",
413 "http://www.163.com",
414 "http://www.sina.com.cn" };
416 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
418 wxURL
urlProblem(pszProblemUrls
[i
]);
419 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
421 wxInputStream
* is
= urlProblem
.GetInputStream();
422 CPPUNIT_ASSERT(is
!= NULL
);
424 wxFile
fOut(_T("test.html"), wxFile::write
);
425 wxASSERT(fOut
.IsOpened());
431 size_t n
= is
->LastRead();
443 // the purpose of this test is unclear, it seems to be unfinished so disabling
445 #if 0 && wxUSE_PROTOCOL_HTTP
446 void URITestCase::URLProxy()
448 wxURL
url(wxT("http://www.asite.com/index.html"));
449 url
.SetProxy(wxT("pserv:3122"));
451 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
452 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
453 wxInputStream
*data
= url2
.GetInputStream();
454 CPPUNIT_ASSERT(data
!= NULL
);
456 #endif // wxUSE_PROTOCOL_HTTP