1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxURI unit test
6 // Copyright: (c) 2004 Ryan Norton
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
26 // Test wxURL & wxURI compat?
27 #define TEST_URL wxUSE_URL
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class URITestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( URITestCase
);
42 CPPUNIT_TEST( Server
);
43 CPPUNIT_TEST( Paths
);
44 CPPUNIT_TEST( UserAndPass
);
45 CPPUNIT_TEST( NormalResolving
);
46 CPPUNIT_TEST( ComplexResolving
);
47 CPPUNIT_TEST( ReallyComplexResolving
);
48 CPPUNIT_TEST( QueryFragmentResolving
);
49 CPPUNIT_TEST( BackwardsResolving
);
50 CPPUNIT_TEST( Assignment
);
51 CPPUNIT_TEST( Comparison
);
52 CPPUNIT_TEST( Unescaping
);
53 CPPUNIT_TEST( FileScheme
);
55 CPPUNIT_TEST( URLCompat
);
56 #if 0 && wxUSE_PROTOCOL_HTTP
57 CPPUNIT_TEST( URLProxy
);
60 CPPUNIT_TEST_SUITE_END();
67 void NormalResolving();
68 void ComplexResolving();
69 void ReallyComplexResolving();
70 void QueryFragmentResolving();
71 void BackwardsResolving();
79 #if 0 && wxUSE_PROTOCOL_HTTP
84 DECLARE_NO_COPY_CLASS(URITestCase
)
87 // register in the unnamed registry so that these tests are run by default
88 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
90 // also include in its own registry so that these tests can be run alone
91 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
93 URITestCase::URITestCase()
97 // apply the given accessor to the URI, check that the result is as expected
98 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
99 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
101 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
102 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
104 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
105 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
107 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
110 #define URI_ASSERT_USER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
113 void URITestCase::IPv4()
115 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
118 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
122 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
123 GetHostType() != wxURI_IPV4ADDRESS
);
126 void URITestCase::IPv6()
128 // IPv6address = 6( h16 ":" ) ls32
129 // / "::" 5( h16 ":" ) ls32
130 // / [ h16 ] "::" 4( h16 ":" ) ls32
131 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
132 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
133 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
134 // / [ *4( h16 ":" ) h16 ] "::" ls32
135 // / [ *5( h16 ":" ) h16 ] "::" h16
136 // / [ *6( h16 ":" ) h16 ] "::"
137 // ls32 = ( h16 ":" h16 ) / IPv4address
139 URI_ASSERT_HOSTTYPE_EQUAL
141 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
145 URI_ASSERT_HOSTTYPE_EQUAL
147 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
151 URI_ASSERT_HOSTTYPE_EQUAL
153 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
157 URI_ASSERT_HOSTTYPE_EQUAL
159 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
164 void URITestCase::Server()
166 URI_ASSERT_SERVER_EQUAL("http://foo/", "foo");
167 URI_ASSERT_SERVER_EQUAL("http://foo-bar/", "foo-bar");
168 URI_ASSERT_SERVER_EQUAL("http://foo/bar/", "foo");
169 URI_ASSERT_SERVER_EQUAL("http://192.168.1.0/", "192.168.1.0");
170 URI_ASSERT_SERVER_EQUAL("http://192.168.1.17/", "192.168.1.17");
171 URI_ASSERT_SERVER_EQUAL("http://192.168.1.255/", "192.168.1.255");
172 URI_ASSERT_SERVER_EQUAL("http://192.168.1.1/index.html", "192.168.1.1");
173 URI_ASSERT_SERVER_EQUAL("http://[aa:aa:aa:aa::aa:aa]/foo", "aa:aa:aa:aa::aa:aa");
176 void URITestCase::Paths()
178 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
181 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
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_PART_EQUAL("path/john/../../../joe",
191 "../joe", BuildURI());
194 void URITestCase::UserAndPass()
196 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
197 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
198 URI_ASSERT_USER_EQUAL("http://host/path/", "");
201 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
204 uri.Resolve(masteruri, strict); \
205 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
208 #define URI_TEST_RESOLVE(string, eq) \
209 URI_TEST_RESOLVE_IMPL(string, eq, true);
211 #define URI_TEST_RESOLVE_LAX(string, eq) \
212 URI_TEST_RESOLVE_IMPL(string, eq, false);
215 //examples taken from RFC 2396.bis
217 void URITestCase::NormalResolving()
219 wxURI
masteruri("http://a/b/c/d;p?q");
221 URI_TEST_RESOLVE("g:h" ,"g:h")
222 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
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/g")
226 URI_TEST_RESOLVE("//g" ,"http://g")
227 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
228 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
229 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
230 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
231 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
232 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
233 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
234 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
236 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
237 URI_TEST_RESOLVE("." ,"http://a/b/c/")
238 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
239 URI_TEST_RESOLVE(".." ,"http://a/b/")
240 URI_TEST_RESOLVE("../" ,"http://a/b/")
241 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
242 URI_TEST_RESOLVE("../..","http://a/")
243 URI_TEST_RESOLVE("../../" , "http://a/")
244 URI_TEST_RESOLVE("../../g" , "http://a/g")
247 void URITestCase::ComplexResolving()
249 wxURI
masteruri("http://a/b/c/d;p?q");
252 URI_TEST_RESOLVE("../../../g" , "http://a/g")
253 URI_TEST_RESOLVE("../../../../g", "http://a/g")
255 URI_TEST_RESOLVE("/./g" ,"http://a/g")
256 URI_TEST_RESOLVE("/../g" ,"http://a/g")
257 URI_TEST_RESOLVE("g." ,"http://a/b/c/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")
263 void URITestCase::ReallyComplexResolving()
265 wxURI
masteruri("http://a/b/c/d;p?q");
267 //even more odder path examples
268 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
269 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
270 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
271 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
272 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
273 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
276 void URITestCase::QueryFragmentResolving()
278 wxURI
masteruri("http://a/b/c/d;p?q");
280 //query/fragment ambigiousness
281 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
282 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
283 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
284 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
287 void URITestCase::BackwardsResolving()
289 wxURI
masteruri("http://a/b/c/d;p?q");
292 URI_TEST_RESOLVE("http:g" , "http:g") //strict
294 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
297 void URITestCase::Assignment()
299 wxURI
uri1("http://mysite.com"),
300 uri2("http://mysite2.com");
304 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
307 void URITestCase::Comparison()
309 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
312 void URITestCase::Unescaping()
317 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
318 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
320 unescaped
= wxURI(escaped
).BuildUnescapedURI();
322 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
323 "escaping\\that\\seems\\broken\\sadly[1].rss",
326 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"
341 #endif // wxUSE_UNICODE
344 void URITestCase::FileScheme()
346 //file:// variety (NOT CONFORMING TO THE RFC)
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" );
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" );
368 void URITestCase::URLCompat()
370 wxURL
url("http://user:password@wxwidgets.org");
372 CPPUNIT_ASSERT( url
.GetError() == wxURL_NOERR
);
373 CPPUNIT_ASSERT( url
== wxURL("http://user:password@wxwidgets.org") );
375 wxURI
uri("http://user:password@wxwidgets.org");
377 CPPUNIT_ASSERT( url
== uri
);
381 CPPUNIT_ASSERT( urlcopy
== url
);
382 CPPUNIT_ASSERT( urlcopy
== uri
);
386 CPPUNIT_ASSERT( uricopy
== url
);
387 CPPUNIT_ASSERT( uricopy
== urlcopy
);
388 CPPUNIT_ASSERT( uricopy
== uri
);
389 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
391 wxURI
test("file:\"myf\"ile.txt");
393 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test
.BuildURI() );
394 CPPUNIT_ASSERT_EQUAL( "file", test
.GetScheme() );
395 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test
.GetPath() );
397 // these could be put under a named registry since they take some
400 // Test problem urls (reported not to work some time ago by a user...)
401 const wxChar
* pszProblemUrls
[] = { "http://www.csdn.net",
402 "http://www.163.com",
403 "http://www.sina.com.cn" };
405 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
407 wxURL
urlProblem(pszProblemUrls
[i
]);
408 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
410 wxInputStream
* is
= urlProblem
.GetInputStream();
411 CPPUNIT_ASSERT(is
!= NULL
);
413 wxFile
fOut(wxT("test.html"), wxFile::write
);
414 wxASSERT(fOut
.IsOpened());
420 size_t n
= is
->LastRead();
432 // the purpose of this test is unclear, it seems to be unfinished so disabling
434 #if 0 && wxUSE_PROTOCOL_HTTP
435 void URITestCase::URLProxy()
437 wxURL
url(wxT("http://www.asite.com/index.html"));
438 url
.SetProxy(wxT("pserv:3122"));
440 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
441 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
442 wxInputStream
*data
= url2
.GetInputStream();
443 CPPUNIT_ASSERT(data
!= NULL
);
445 #endif // wxUSE_PROTOCOL_HTTP