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( NormalResolving
);
51 CPPUNIT_TEST( ComplexResolving
);
52 CPPUNIT_TEST( ReallyComplexResolving
);
53 CPPUNIT_TEST( QueryFragmentResolving
);
54 CPPUNIT_TEST( BackwardsResolving
);
55 CPPUNIT_TEST( Assignment
);
56 CPPUNIT_TEST( Comparison
);
57 CPPUNIT_TEST( Unescaping
);
58 CPPUNIT_TEST( FileScheme
);
60 CPPUNIT_TEST( URLCompat
);
61 #if 0 && wxUSE_PROTOCOL_HTTP
62 CPPUNIT_TEST( URLProxy
);
65 CPPUNIT_TEST_SUITE_END();
71 void NormalResolving();
72 void ComplexResolving();
73 void ReallyComplexResolving();
74 void QueryFragmentResolving();
75 void BackwardsResolving();
83 #if 0 && wxUSE_PROTOCOL_HTTP
88 DECLARE_NO_COPY_CLASS(URITestCase
)
91 // register in the unnamed registry so that these tests are run by default
92 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
94 // also include in it's own registry so that these tests can be run alone
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
97 URITestCase::URITestCase()
101 // apply the given accessor to the URI, check that the result is as expected
102 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
103 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
105 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
106 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
108 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
109 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
111 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
112 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
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 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
198 uri.Resolve(masteruri, strict); \
199 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
202 #define URI_TEST_RESOLVE(string, eq) \
203 URI_TEST_RESOLVE_IMPL(string, eq, true);
205 #define URI_TEST_RESOLVE_LAX(string, eq) \
206 URI_TEST_RESOLVE_IMPL(string, eq, false);
209 //examples taken from RFC 2396.bis
211 void URITestCase::NormalResolving()
213 wxURI
masteruri("http://a/b/c/d;p?q");
215 URI_TEST_RESOLVE("g:h" ,"g:h")
216 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
217 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
218 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
219 URI_TEST_RESOLVE("/g" ,"http://a/g")
220 URI_TEST_RESOLVE("//g" ,"http://g")
221 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
222 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
223 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
224 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
225 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
226 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
227 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
228 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
230 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
231 URI_TEST_RESOLVE("." ,"http://a/b/c/")
232 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
233 URI_TEST_RESOLVE(".." ,"http://a/b/")
234 URI_TEST_RESOLVE("../" ,"http://a/b/")
235 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
236 URI_TEST_RESOLVE("../..","http://a/")
237 URI_TEST_RESOLVE("../../" , "http://a/")
238 URI_TEST_RESOLVE("../../g" , "http://a/g")
241 void URITestCase::ComplexResolving()
243 wxURI
masteruri("http://a/b/c/d;p?q");
246 URI_TEST_RESOLVE("../../../g" , "http://a/g")
247 URI_TEST_RESOLVE("../../../../g", "http://a/g")
249 URI_TEST_RESOLVE("/./g" ,"http://a/g")
250 URI_TEST_RESOLVE("/../g" ,"http://a/g")
251 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
252 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
253 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
254 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
257 void URITestCase::ReallyComplexResolving()
259 wxURI
masteruri("http://a/b/c/d;p?q");
261 //even more odder path examples
262 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
263 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
264 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
265 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
266 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
267 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
270 void URITestCase::QueryFragmentResolving()
272 wxURI
masteruri("http://a/b/c/d;p?q");
274 //query/fragment ambigiousness
275 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
276 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
277 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
278 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
281 void URITestCase::BackwardsResolving()
283 wxURI
masteruri("http://a/b/c/d;p?q");
286 URI_TEST_RESOLVE("http:g" , "http:g") //strict
288 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
291 void URITestCase::Assignment()
293 wxURI
uri1("http://mysite.com"),
294 uri2("http://mysite2.com");
298 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
301 void URITestCase::Comparison()
303 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
306 void URITestCase::Unescaping()
311 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
312 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
314 unescaped
= wxURI(escaped
).BuildUnescapedURI();
316 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
317 "escaping\\that\\seems\\broken\\sadly[1].rss",
320 CPPUNIT_ASSERT_EQUAL( unescaped
, wxURI::Unescape(escaped
) );
323 escaped
= "http://ru.wikipedia.org/wiki/"
324 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
326 unescaped
= wxURI::Unescape(escaped
);
328 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
329 "http://ru.wikipedia.org/wiki/"
330 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
331 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
336 void URITestCase::FileScheme()
338 //file:// variety (NOT CONFORMING TO THE RFC)
339 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
340 "e:/wxcode/script1.xml" );
343 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
344 "/e:/wxcode/script1.xml" );
347 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
348 "/e:/wxcode/script1.xml" );
351 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
352 "e:/wxcode/script1.xml" );
360 void URITestCase::URLCompat()
362 wxURL
url("http://user:password@wxwidgets.org");
364 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
367 wxInputStream
* pInput
= url
.GetInputStream();
369 CPPUNIT_ASSERT( pInput
!= NULL
);
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(_T("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