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()
99 // apply the given accessor to the URI, check that the result is as expected
100 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
101 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
103 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
104 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
106 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
107 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
109 void URITestCase
::IPv4()
111 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
114 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
118 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
119 GetHostType() != wxURI_IPV4ADDRESS
);
122 void URITestCase
::IPv6()
124 // IPv6address = 6( h16 ":" ) ls32
125 // / "::" 5( h16 ":" ) ls32
126 // / [ h16 ] "::" 4( h16 ":" ) ls32
127 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
128 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
129 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
130 // / [ *4( h16 ":" ) h16 ] "::" ls32
131 // / [ *5( h16 ":" ) h16 ] "::" h16
132 // / [ *6( h16 ":" ) h16 ] "::"
133 // ls32 = ( h16 ":" h16 ) / IPv4address
135 URI_ASSERT_HOSTTYPE_EQUAL
137 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
141 URI_ASSERT_HOSTTYPE_EQUAL
143 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
147 URI_ASSERT_HOSTTYPE_EQUAL
149 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
153 URI_ASSERT_HOSTTYPE_EQUAL
155 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
160 void URITestCase
::Paths()
162 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
165 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
168 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
171 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
174 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
175 "../joe", BuildURI());
178 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
181 uri.Resolve(masteruri, strict); \
182 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
185 #define URI_TEST_RESOLVE(string, eq) \
186 URI_TEST_RESOLVE_IMPL(string, eq, true);
188 #define URI_TEST_RESOLVE_LAX(string, eq) \
189 URI_TEST_RESOLVE_IMPL(string, eq, false);
192 //examples taken from RFC 2396.bis
194 void URITestCase
::NormalResolving()
196 wxURI
masteruri("http://a/b/c/d;p?q");
198 URI_TEST_RESOLVE("g:h" ,"g:h")
199 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
200 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
201 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
202 URI_TEST_RESOLVE("/g" ,"http://a/g")
203 URI_TEST_RESOLVE("//g" ,"http://g")
204 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
205 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
206 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
207 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
208 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
209 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
210 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
211 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
213 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
214 URI_TEST_RESOLVE("." ,"http://a/b/c/")
215 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
216 URI_TEST_RESOLVE(".." ,"http://a/b/")
217 URI_TEST_RESOLVE("../" ,"http://a/b/")
218 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
219 URI_TEST_RESOLVE("../..","http://a/")
220 URI_TEST_RESOLVE("../../" , "http://a/")
221 URI_TEST_RESOLVE("../../g" , "http://a/g")
224 void URITestCase
::ComplexResolving()
226 wxURI
masteruri("http://a/b/c/d;p?q");
229 URI_TEST_RESOLVE("../../../g" , "http://a/g")
230 URI_TEST_RESOLVE("../../../../g", "http://a/g")
232 URI_TEST_RESOLVE("/./g" ,"http://a/g")
233 URI_TEST_RESOLVE("/../g" ,"http://a/g")
234 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
235 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
236 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
237 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
240 void URITestCase
::ReallyComplexResolving()
242 wxURI
masteruri("http://a/b/c/d;p?q");
244 //even more odder path examples
245 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
246 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
247 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
248 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
249 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
250 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
253 void URITestCase
::QueryFragmentResolving()
255 wxURI
masteruri("http://a/b/c/d;p?q");
257 //query/fragment ambigiousness
258 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
259 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
260 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
261 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
264 void URITestCase
::BackwardsResolving()
266 wxURI
masteruri("http://a/b/c/d;p?q");
269 URI_TEST_RESOLVE("http:g" , "http:g") //strict
271 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
274 void URITestCase
::Assignment()
276 wxURI
uri1("http://mysite.com"),
277 uri2("http://mysite2.com");
281 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
284 void URITestCase
::Comparison()
286 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
289 void URITestCase
::Unescaping()
294 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
295 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
297 unescaped
= wxURI(escaped
).BuildUnescapedURI();
299 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
300 "escaping\\that\\seems\\broken\\sadly[1].rss",
303 CPPUNIT_ASSERT_EQUAL( unescaped
, wxURI
::Unescape(escaped
) );
306 escaped
= "http://ru.wikipedia.org/wiki/"
307 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
309 unescaped
= wxURI
::Unescape(escaped
);
311 CPPUNIT_ASSERT_EQUAL( wxString
::FromUTF8(
312 "http://ru.wikipedia.org/wiki/"
313 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
314 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
319 void URITestCase
::FileScheme()
321 //file:// variety (NOT CONFORMING TO THE RFC)
322 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
323 "e:/wxcode/script1.xml" );
326 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
327 "/e:/wxcode/script1.xml" );
330 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
331 "/e:/wxcode/script1.xml" );
334 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
335 "e:/wxcode/script1.xml" );
343 void URITestCase
::URLCompat()
345 wxURL
url("http://user:password@wxwidgets.org");
347 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
350 wxInputStream
* pInput
= url
.GetInputStream();
352 CPPUNIT_ASSERT( pInput
!= NULL
);
355 CPPUNIT_ASSERT( url
== wxURL("http://user:password@wxwidgets.org") );
357 wxURI
uri("http://user:password@wxwidgets.org");
359 CPPUNIT_ASSERT( url
== uri
);
363 CPPUNIT_ASSERT( urlcopy
== url
);
364 CPPUNIT_ASSERT( urlcopy
== uri
);
368 CPPUNIT_ASSERT( uricopy
== url
);
369 CPPUNIT_ASSERT( uricopy
== urlcopy
);
370 CPPUNIT_ASSERT( uricopy
== uri
);
371 CPPUNIT_ASSERT_EQUAL( " A ", wxURI
::Unescape("%20%41%20") );
373 wxURI
test("file:\"myf\"ile.txt");
375 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test
.BuildURI() );
376 CPPUNIT_ASSERT_EQUAL( "file", test
.GetScheme() );
377 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test
.GetPath() );
379 // these could be put under a named registry since they take some
382 // Test problem urls (reported not to work some time ago by a user...)
383 const wxChar
* pszProblemUrls
[] = { "http://www.csdn.net",
384 "http://www.163.com",
385 "http://www.sina.com.cn" };
387 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
389 wxURL
urlProblem(pszProblemUrls
[i
]);
390 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
392 wxInputStream
* is
= urlProblem
.GetInputStream();
393 CPPUNIT_ASSERT(is
!= NULL
);
395 wxFile
fOut(_T("test.html"), wxFile
::write
);
396 wxASSERT(fOut
.IsOpened());
402 size_t n
= is
->LastRead();
414 // the purpose of this test is unclear, it seems to be unfinished so disabling
416 #if 0 && wxUSE_PROTOCOL_HTTP
417 void URITestCase
::URLProxy()
419 wxURL
url(wxT("http://www.asite.com/index.html"));
420 url
.SetProxy(wxT("pserv:3122"));
422 wxURL
::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
423 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
424 wxInputStream
*data
= url2
.GetInputStream();
425 CPPUNIT_ASSERT(data
!= NULL
);
427 #endif // wxUSE_PROTOCOL_HTTP