]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / tests / uris / uris.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxURI unit test
4 // Author: Ryan Norton
5 // Created: 2004-08-14
6 // Copyright: (c) 2004 Ryan Norton
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/uri.h"
24 #include "wx/url.h"
25
26 // Test wxURL & wxURI compat?
27 #define TEST_URL wxUSE_URL
28
29 // ----------------------------------------------------------------------------
30 // test class
31 // ----------------------------------------------------------------------------
32
33 class URITestCase : public CppUnit::TestCase
34 {
35 public:
36 URITestCase();
37
38 private:
39 CPPUNIT_TEST_SUITE( URITestCase );
40 CPPUNIT_TEST( IPv4 );
41 CPPUNIT_TEST( IPv6 );
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 );
54 #if TEST_URL
55 CPPUNIT_TEST( URLCompat );
56 #if 0 && wxUSE_PROTOCOL_HTTP
57 CPPUNIT_TEST( URLProxy );
58 #endif
59 #endif
60 CPPUNIT_TEST_SUITE_END();
61
62 void IPv4();
63 void IPv6();
64 void Server();
65 void Paths();
66 void UserAndPass();
67 void NormalResolving();
68 void ComplexResolving();
69 void ReallyComplexResolving();
70 void QueryFragmentResolving();
71 void BackwardsResolving();
72 void Assignment();
73 void Comparison();
74 void Unescaping();
75 void FileScheme();
76
77 #if TEST_URL
78 void URLCompat();
79 #if 0 && wxUSE_PROTOCOL_HTTP
80 void URLProxy();
81 #endif
82 #endif
83
84 DECLARE_NO_COPY_CLASS(URITestCase)
85 };
86
87 // register in the unnamed registry so that these tests are run by default
88 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
89
90 // also include in its own registry so that these tests can be run alone
91 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
92
93 URITestCase::URITestCase()
94 {
95 }
96
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)
100
101 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
102 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
103
104 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
105 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
106
107 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
109
110 #define URI_ASSERT_USER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
112
113 void URITestCase::IPv4()
114 {
115 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
116 wxURI_IPV4ADDRESS);
117
118 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
119 wxURI_IPV4ADDRESS);
120
121 // bogus ipv4
122 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
123 GetHostType() != wxURI_IPV4ADDRESS);
124 }
125
126 void URITestCase::IPv6()
127 {
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
138
139 URI_ASSERT_HOSTTYPE_EQUAL
140 (
141 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
142 wxURI_IPV6ADDRESS
143 );
144
145 URI_ASSERT_HOSTTYPE_EQUAL
146 (
147 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
148 wxURI_IPV6ADDRESS
149 );
150
151 URI_ASSERT_HOSTTYPE_EQUAL
152 (
153 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
154 wxURI_IPV6ADDRESS
155 );
156
157 URI_ASSERT_HOSTTYPE_EQUAL
158 (
159 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
160 wxURI_IPV6ADDRESS
161 );
162 }
163
164 void URITestCase::Server()
165 {
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");
174 }
175
176 void URITestCase::Paths()
177 {
178 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
179 "/path");
180
181 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
182 "/");
183
184 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
185 "/path/");
186
187 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
188 "/path/");
189
190 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
191 "../joe", BuildURI());
192 }
193
194 void URITestCase::UserAndPass()
195 {
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/", "");
199 }
200
201 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
202 { \
203 wxURI uri(string); \
204 uri.Resolve(masteruri, strict); \
205 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
206 }
207
208 #define URI_TEST_RESOLVE(string, eq) \
209 URI_TEST_RESOLVE_IMPL(string, eq, true);
210
211 #define URI_TEST_RESOLVE_LAX(string, eq) \
212 URI_TEST_RESOLVE_IMPL(string, eq, false);
213
214
215 //examples taken from RFC 2396.bis
216
217 void URITestCase::NormalResolving()
218 {
219 wxURI masteruri("http://a/b/c/d;p?q");
220
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")
235
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")
245 }
246
247 void URITestCase::ComplexResolving()
248 {
249 wxURI masteruri("http://a/b/c/d;p?q");
250
251 //odd path examples
252 URI_TEST_RESOLVE("../../../g" , "http://a/g")
253 URI_TEST_RESOLVE("../../../../g", "http://a/g")
254
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")
261 }
262
263 void URITestCase::ReallyComplexResolving()
264 {
265 wxURI masteruri("http://a/b/c/d;p?q");
266
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")
274 }
275
276 void URITestCase::QueryFragmentResolving()
277 {
278 wxURI masteruri("http://a/b/c/d;p?q");
279
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")
285 }
286
287 void URITestCase::BackwardsResolving()
288 {
289 wxURI masteruri("http://a/b/c/d;p?q");
290
291 //"NEW"
292 URI_TEST_RESOLVE("http:g" , "http:g") //strict
293 //bw compat
294 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
295 }
296
297 void URITestCase::Assignment()
298 {
299 wxURI uri1("http://mysite.com"),
300 uri2("http://mysite2.com");
301
302 uri2 = uri1;
303
304 CPPUNIT_ASSERT_EQUAL(uri1.BuildURI(), uri2.BuildURI());
305 }
306
307 void URITestCase::Comparison()
308 {
309 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
310 }
311
312 void URITestCase::Unescaping()
313 {
314 wxString escaped,
315 unescaped;
316
317 escaped = "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
318 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
319
320 unescaped = wxURI(escaped).BuildUnescapedURI();
321
322 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
323 "escaping\\that\\seems\\broken\\sadly[1].rss",
324 unescaped );
325
326 CPPUNIT_ASSERT_EQUAL( unescaped, wxURI::Unescape(escaped) );
327
328
329 #if wxUSE_UNICODE
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";
332
333 unescaped = wxURI::Unescape(escaped);
334
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"
339 ),
340 unescaped );
341 #endif // wxUSE_UNICODE
342 }
343
344 void URITestCase::FileScheme()
345 {
346 //file:// variety (NOT CONFORMING TO THE RFC)
347 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
348 "e:/wxcode/script1.xml" );
349
350 //file:/// variety
351 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
352 "/e:/wxcode/script1.xml" );
353
354 //file:/ variety
355 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
356 "/e:/wxcode/script1.xml" );
357
358 //file: variety
359 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
360 "e:/wxcode/script1.xml" );
361 }
362
363 #if TEST_URL
364
365 #include "wx/url.h"
366 #include "wx/file.h"
367
368 void URITestCase::URLCompat()
369 {
370 wxURL url("http://user:password@wxwidgets.org");
371
372 CPPUNIT_ASSERT( url.GetError() == wxURL_NOERR );
373 CPPUNIT_ASSERT( url == wxURL("http://user:password@wxwidgets.org") );
374
375 wxURI uri("http://user:password@wxwidgets.org");
376
377 CPPUNIT_ASSERT( url == uri );
378
379 wxURL urlcopy(uri);
380
381 CPPUNIT_ASSERT( urlcopy == url );
382 CPPUNIT_ASSERT( urlcopy == uri );
383
384 wxURI uricopy(url);
385
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") );
390
391 wxURI test("file:\"myf\"ile.txt");
392
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() );
396
397 // these could be put under a named registry since they take some
398 // time to complete
399 #if 0
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" };
404
405 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
406 {
407 wxURL urlProblem(pszProblemUrls[i]);
408 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
409
410 wxInputStream* is = urlProblem.GetInputStream();
411 CPPUNIT_ASSERT(is != NULL);
412
413 wxFile fOut(wxT("test.html"), wxFile::write);
414 wxASSERT(fOut.IsOpened());
415
416 char buf[1001];
417 for( ;; )
418 {
419 is->Read(buf, 1000);
420 size_t n = is->LastRead();
421 if ( n == 0 )
422 break;
423 buf[n] = 0;
424 fOut.Write(buf, n);
425 }
426
427 delete is;
428 }
429 #endif
430 }
431
432 // the purpose of this test is unclear, it seems to be unfinished so disabling
433 // it for now
434 #if 0 && wxUSE_PROTOCOL_HTTP
435 void URITestCase::URLProxy()
436 {
437 wxURL url(wxT("http://www.asite.com/index.html"));
438 url.SetProxy(wxT("pserv:3122"));
439
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);
444 }
445 #endif // wxUSE_PROTOCOL_HTTP
446
447 #endif // TEST_URL