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 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class URITestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( URITestCase
);
43 CPPUNIT_TEST( Server
);
44 CPPUNIT_TEST( Paths
);
45 CPPUNIT_TEST( UserAndPass
);
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
);
53 CPPUNIT_TEST( Unescaping
);
54 CPPUNIT_TEST( FileScheme
);
56 CPPUNIT_TEST( URLCompat
);
57 #if 0 && wxUSE_PROTOCOL_HTTP
58 CPPUNIT_TEST( URLProxy
);
61 CPPUNIT_TEST_SUITE_END();
68 void NormalResolving();
69 void ComplexResolving();
70 void ReallyComplexResolving();
71 void QueryFragmentResolving();
72 void BackwardsResolving();
80 #if 0 && wxUSE_PROTOCOL_HTTP
85 DECLARE_NO_COPY_CLASS(URITestCase
)
88 // register in the unnamed registry so that these tests are run by default
89 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
91 // also include in it's own registry so that these tests can be run alone
92 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
94 URITestCase::URITestCase()
98 // apply the given accessor to the URI, check that the result is as expected
99 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
100 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
102 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
103 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
105 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
106 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
108 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
109 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
111 #define URI_ASSERT_USER_EQUAL(uri, expected) \
112 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
114 void URITestCase::IPv4()
116 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
119 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
123 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
124 GetHostType() != wxURI_IPV4ADDRESS
);
127 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_ASSERT_HOSTTYPE_EQUAL
142 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
146 URI_ASSERT_HOSTTYPE_EQUAL
148 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
152 URI_ASSERT_HOSTTYPE_EQUAL
154 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
158 URI_ASSERT_HOSTTYPE_EQUAL
160 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
165 void URITestCase::Server()
167 URI_ASSERT_SERVER_EQUAL("http://foo/", "foo");
168 URI_ASSERT_SERVER_EQUAL("http://foo-bar/", "foo-bar");
169 URI_ASSERT_SERVER_EQUAL("http://foo/bar/", "foo");
170 URI_ASSERT_SERVER_EQUAL("http://192.168.1.0/", "192.168.1.0");
171 URI_ASSERT_SERVER_EQUAL("http://192.168.1.17/", "192.168.1.17");
172 URI_ASSERT_SERVER_EQUAL("http://192.168.1.255/", "192.168.1.255");
173 URI_ASSERT_SERVER_EQUAL("http://192.168.1.1/index.html", "192.168.1.1");
174 URI_ASSERT_SERVER_EQUAL("http://[aa:aa:aa:aa::aa:aa]/foo", "aa:aa:aa:aa::aa:aa");
177 void URITestCase::Paths()
179 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
182 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
185 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
188 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
191 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
192 "../joe", BuildURI());
195 void URITestCase::UserAndPass()
197 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
198 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
199 URI_ASSERT_USER_EQUAL("http://host/path/", "");
202 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
205 uri.Resolve(masteruri, strict); \
206 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
209 #define URI_TEST_RESOLVE(string, eq) \
210 URI_TEST_RESOLVE_IMPL(string, eq, true);
212 #define URI_TEST_RESOLVE_LAX(string, eq) \
213 URI_TEST_RESOLVE_IMPL(string, eq, false);
216 //examples taken from RFC 2396.bis
218 void URITestCase::NormalResolving()
220 wxURI
masteruri("http://a/b/c/d;p?q");
222 URI_TEST_RESOLVE("g:h" ,"g:h")
223 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
224 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
225 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
226 URI_TEST_RESOLVE("/g" ,"http://a/g")
227 URI_TEST_RESOLVE("//g" ,"http://g")
228 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
229 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
230 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
231 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
232 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
233 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
234 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
235 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
237 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
238 URI_TEST_RESOLVE("." ,"http://a/b/c/")
239 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
240 URI_TEST_RESOLVE(".." ,"http://a/b/")
241 URI_TEST_RESOLVE("../" ,"http://a/b/")
242 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
243 URI_TEST_RESOLVE("../..","http://a/")
244 URI_TEST_RESOLVE("../../" , "http://a/")
245 URI_TEST_RESOLVE("../../g" , "http://a/g")
248 void URITestCase::ComplexResolving()
250 wxURI
masteruri("http://a/b/c/d;p?q");
253 URI_TEST_RESOLVE("../../../g" , "http://a/g")
254 URI_TEST_RESOLVE("../../../../g", "http://a/g")
256 URI_TEST_RESOLVE("/./g" ,"http://a/g")
257 URI_TEST_RESOLVE("/../g" ,"http://a/g")
258 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
259 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
260 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
261 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
264 void URITestCase::ReallyComplexResolving()
266 wxURI
masteruri("http://a/b/c/d;p?q");
268 //even more odder path examples
269 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
270 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
271 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
272 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
273 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
274 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
277 void URITestCase::QueryFragmentResolving()
279 wxURI
masteruri("http://a/b/c/d;p?q");
281 //query/fragment ambigiousness
282 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
283 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
284 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
285 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
288 void URITestCase::BackwardsResolving()
290 wxURI
masteruri("http://a/b/c/d;p?q");
293 URI_TEST_RESOLVE("http:g" , "http:g") //strict
295 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
298 void URITestCase::Assignment()
300 wxURI
uri1("http://mysite.com"),
301 uri2("http://mysite2.com");
305 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
308 void URITestCase::Comparison()
310 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
313 void URITestCase::Unescaping()
318 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
319 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
321 unescaped
= wxURI(escaped
).BuildUnescapedURI();
323 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
324 "escaping\\that\\seems\\broken\\sadly[1].rss",
327 CPPUNIT_ASSERT_EQUAL( unescaped
, wxURI::Unescape(escaped
) );
330 escaped
= "http://ru.wikipedia.org/wiki/"
331 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
333 unescaped
= wxURI::Unescape(escaped
);
335 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
336 "http://ru.wikipedia.org/wiki/"
337 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
338 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
343 void URITestCase::FileScheme()
345 //file:// variety (NOT CONFORMING TO THE RFC)
346 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
347 "e:/wxcode/script1.xml" );
350 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
351 "/e:/wxcode/script1.xml" );
354 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
355 "/e:/wxcode/script1.xml" );
358 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
359 "e:/wxcode/script1.xml" );
367 void URITestCase::URLCompat()
369 wxURL
url("http://user:password@wxwidgets.org");
371 CPPUNIT_ASSERT( url
.GetError() == wxURL_NOERR
);
372 CPPUNIT_ASSERT( url
== wxURL("http://user:password@wxwidgets.org") );
374 wxURI
uri("http://user:password@wxwidgets.org");
376 CPPUNIT_ASSERT( url
== uri
);
380 CPPUNIT_ASSERT( urlcopy
== url
);
381 CPPUNIT_ASSERT( urlcopy
== uri
);
385 CPPUNIT_ASSERT( uricopy
== url
);
386 CPPUNIT_ASSERT( uricopy
== urlcopy
);
387 CPPUNIT_ASSERT( uricopy
== uri
);
388 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
390 wxURI
test("file:\"myf\"ile.txt");
392 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test
.BuildURI() );
393 CPPUNIT_ASSERT_EQUAL( "file", test
.GetScheme() );
394 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test
.GetPath() );
396 // these could be put under a named registry since they take some
399 // Test problem urls (reported not to work some time ago by a user...)
400 const wxChar
* pszProblemUrls
[] = { "http://www.csdn.net",
401 "http://www.163.com",
402 "http://www.sina.com.cn" };
404 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
406 wxURL
urlProblem(pszProblemUrls
[i
]);
407 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
409 wxInputStream
* is
= urlProblem
.GetInputStream();
410 CPPUNIT_ASSERT(is
!= NULL
);
412 wxFile
fOut(wxT("test.html"), wxFile::write
);
413 wxASSERT(fOut
.IsOpened());
419 size_t n
= is
->LastRead();
431 // the purpose of this test is unclear, it seems to be unfinished so disabling
433 #if 0 && wxUSE_PROTOCOL_HTTP
434 void URITestCase::URLProxy()
436 wxURL
url(wxT("http://www.asite.com/index.html"));
437 url
.SetProxy(wxT("pserv:3122"));
439 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
440 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
441 wxInputStream
*data
= url2
.GetInputStream();
442 CPPUNIT_ASSERT(data
!= NULL
);
444 #endif // wxUSE_PROTOCOL_HTTP