]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/uris.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / uris / uris.cpp
CommitLineData
dd65d8c8
RN
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/uris/uris.cpp
3// Purpose: wxURI unit test
4// Author: Ryan Norton
5// Created: 2004-08-14
dd65d8c8
RN
6// Copyright: (c) 2004 Ryan Norton
7///////////////////////////////////////////////////////////////////////////////
8
9// ----------------------------------------------------------------------------
10// headers
11// ----------------------------------------------------------------------------
12
8899b155 13#include "testprec.h"
dd65d8c8
RN
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"
2b9afe40 24#include "wx/url.h"
dd65d8c8 25
b60b2ec8 26// Test wxURL & wxURI compat?
9fc7a1d2
VZ
27#define TEST_URL wxUSE_URL
28
dd65d8c8
RN
29// ----------------------------------------------------------------------------
30// test class
31// ----------------------------------------------------------------------------
32
33class URITestCase : public CppUnit::TestCase
34{
35public:
36 URITestCase();
37
38private:
39 CPPUNIT_TEST_SUITE( URITestCase );
40 CPPUNIT_TEST( IPv4 );
41 CPPUNIT_TEST( IPv6 );
c4dbb953 42 CPPUNIT_TEST( Server );
dd65d8c8 43 CPPUNIT_TEST( Paths );
62e3e6c2 44 CPPUNIT_TEST( UserAndPass );
dd65d8c8
RN
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 );
24ca04e7 52 CPPUNIT_TEST( Unescaping );
97ad053b 53 CPPUNIT_TEST( FileScheme );
b60b2ec8
RN
54#if TEST_URL
55 CPPUNIT_TEST( URLCompat );
402ccadc 56#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 57 CPPUNIT_TEST( URLProxy );
26531700 58#endif
b60b2ec8 59#endif
dd65d8c8
RN
60 CPPUNIT_TEST_SUITE_END();
61
62 void IPv4();
63 void IPv6();
c4dbb953 64 void Server();
dd65d8c8 65 void Paths();
62e3e6c2 66 void UserAndPass();
dd65d8c8
RN
67 void NormalResolving();
68 void ComplexResolving();
69 void ReallyComplexResolving();
70 void QueryFragmentResolving();
71 void BackwardsResolving();
72 void Assignment();
73 void Comparison();
24ca04e7 74 void Unescaping();
97ad053b 75 void FileScheme();
dd65d8c8 76
86470d43 77#if TEST_URL
b60b2ec8 78 void URLCompat();
402ccadc 79#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 80 void URLProxy();
402ccadc 81#endif
b60b2ec8
RN
82#endif
83
dd65d8c8
RN
84 DECLARE_NO_COPY_CLASS(URITestCase)
85};
86
87// register in the unnamed registry so that these tests are run by default
88CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
89
e3778b4d 90// also include in its own registry so that these tests can be run alone
dd65d8c8
RN
91CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
92
93URITestCase::URITestCase()
94{
95}
96
5c3d5ef2 97// apply the given accessor to the URI, check that the result is as expected
c3a65218 98#define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
5c3d5ef2 99 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
dd65d8c8 100
c3a65218
VZ
101#define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
102 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
103
c4dbb953
VZ
104#define URI_ASSERT_SERVER_EQUAL(uri, expected) \
105 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
106
c3a65218
VZ
107#define URI_ASSERT_PATH_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
109
62e3e6c2
VZ
110#define URI_ASSERT_USER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
112
dd65d8c8
RN
113void URITestCase::IPv4()
114{
c3a65218
VZ
115 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
116 wxURI_IPV4ADDRESS);
dd65d8c8 117
c3a65218
VZ
118 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
119 wxURI_IPV4ADDRESS);
dd65d8c8 120
c3a65218 121 // bogus ipv4
5c3d5ef2
VZ
122 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
123 GetHostType() != wxURI_IPV4ADDRESS);
dd65d8c8
RN
124}
125
126void URITestCase::IPv6()
127{
dd65d8c8
RN
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
2b9afe40 138
c3a65218
VZ
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 );
dd65d8c8
RN
162}
163
c4dbb953
VZ
164void 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
dd65d8c8
RN
176void URITestCase::Paths()
177{
c3a65218
VZ
178 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
179 "/path");
dd65d8c8 180
c3a65218
VZ
181 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
182 "/");
dd65d8c8 183
c3a65218
VZ
184 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
185 "/path/");
dd65d8c8 186
c3a65218
VZ
187 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
188 "/path/");
dd65d8c8 189
c3a65218
VZ
190 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
191 "../joe", BuildURI());
dd65d8c8 192}
dd65d8c8 193
62e3e6c2
VZ
194void 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
5c3d5ef2 201#define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
2186321f
VZ
202 { \
203 wxURI uri(string); \
204 uri.Resolve(masteruri, strict); \
205 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
206 }
dd65d8c8 207
5c3d5ef2
VZ
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);
dd65d8c8
RN
213
214
215//examples taken from RFC 2396.bis
216
217void URITestCase::NormalResolving()
218{
2186321f 219 wxURI masteruri("http://a/b/c/d;p?q");
2b9afe40 220
5c3d5ef2
VZ
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")
dd65d8c8
RN
245}
246
247void URITestCase::ComplexResolving()
248{
2186321f 249 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
250
251 //odd path examples
2186321f
VZ
252 URI_TEST_RESOLVE("../../../g" , "http://a/g")
253 URI_TEST_RESOLVE("../../../../g", "http://a/g")
254
5c3d5ef2
VZ
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")
dd65d8c8 261}
dd65d8c8
RN
262
263void URITestCase::ReallyComplexResolving()
264{
2186321f 265 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
266
267 //even more odder path examples
5c3d5ef2
VZ
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")
dd65d8c8
RN
274}
275
276void URITestCase::QueryFragmentResolving()
277{
2186321f 278 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 279
2b9afe40 280 //query/fragment ambigiousness
5c3d5ef2
VZ
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")
dd65d8c8
RN
285}
286
287void URITestCase::BackwardsResolving()
288{
2186321f 289 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 290
2b9afe40 291 //"NEW"
5c3d5ef2 292 URI_TEST_RESOLVE("http:g" , "http:g") //strict
2b9afe40 293 //bw compat
5c3d5ef2 294 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
dd65d8c8
RN
295}
296
297void URITestCase::Assignment()
298{
2186321f
VZ
299 wxURI uri1("http://mysite.com"),
300 uri2("http://mysite2.com");
dd65d8c8
RN
301
302 uri2 = uri1;
303
2186321f 304 CPPUNIT_ASSERT_EQUAL(uri1.BuildURI(), uri2.BuildURI());
dd65d8c8
RN
305}
306
307void URITestCase::Comparison()
308{
2186321f 309 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
dd65d8c8 310}
b60b2ec8 311
24ca04e7
VZ
312void URITestCase::Unescaping()
313{
2186321f
VZ
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();
24ca04e7 321
2186321f
VZ
322 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
323 "escaping\\that\\seems\\broken\\sadly[1].rss",
324 unescaped );
24ca04e7 325
2186321f 326 CPPUNIT_ASSERT_EQUAL( unescaped, wxURI::Unescape(escaped) );
24ca04e7 327
24ca04e7 328
87f528f1 329#if wxUSE_UNICODE
2186321f
VZ
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";
24ca04e7 332
2186321f 333 unescaped = wxURI::Unescape(escaped);
24ca04e7 334
2186321f
VZ
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 );
87f528f1 341#endif // wxUSE_UNICODE
24ca04e7 342}
97ad053b
VZ
343
344void URITestCase::FileScheme()
345{
c3a65218
VZ
346 //file:// variety (NOT CONFORMING TO THE RFC)
347 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
348 "e:/wxcode/script1.xml" );
97ad053b
VZ
349
350 //file:/// variety
c3a65218
VZ
351 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
352 "/e:/wxcode/script1.xml" );
97ad053b
VZ
353
354 //file:/ variety
c3a65218
VZ
355 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
356 "/e:/wxcode/script1.xml" );
97ad053b
VZ
357
358 //file: variety
c3a65218
VZ
359 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
360 "e:/wxcode/script1.xml" );
97ad053b
VZ
361}
362
b60b2ec8
RN
363#if TEST_URL
364
365#include "wx/url.h"
24ca04e7 366#include "wx/file.h"
b60b2ec8
RN
367
368void URITestCase::URLCompat()
369{
2186321f 370 wxURL url("http://user:password@wxwidgets.org");
b60b2ec8 371
1f51673b 372 CPPUNIT_ASSERT( url.GetError() == wxURL_NOERR );
2186321f 373 CPPUNIT_ASSERT( url == wxURL("http://user:password@wxwidgets.org") );
b60b2ec8 374
2186321f 375 wxURI uri("http://user:password@wxwidgets.org");
b60b2ec8
RN
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 );
2186321f 389 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
113e181a 390
2186321f 391 wxURI test("file:\"myf\"ile.txt");
113e181a 392
2186321f
VZ
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() );
24fec908 396
6c1a7bc4
MW
397 // these could be put under a named registry since they take some
398 // time to complete
399#if 0
24fec908 400 // Test problem urls (reported not to work some time ago by a user...)
2186321f
VZ
401 const wxChar* pszProblemUrls[] = { "http://www.csdn.net",
402 "http://www.163.com",
403 "http://www.sina.com.cn" };
404
24fec908
VZ
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
9a83f860 413 wxFile fOut(wxT("test.html"), wxFile::write);
24fec908
VZ
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 }
6c1a7bc4 429#endif
b60b2ec8
RN
430}
431
402ccadc
VZ
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
e605541c
RN
435void URITestCase::URLProxy()
436{
6ca3a322
WS
437 wxURL url(wxT("http://www.asite.com/index.html"));
438 url.SetProxy(wxT("pserv:3122"));
fd725bce
WS
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);
e605541c 444}
26531700 445#endif // wxUSE_PROTOCOL_HTTP
24ca04e7
VZ
446
447#endif // TEST_URL