]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/uris.cpp
adding API note
[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
6// RCS-ID: $Id$
7// Copyright: (c) 2004 Ryan Norton
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
8899b155 14#include "testprec.h"
dd65d8c8
RN
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif // WX_PRECOMP
23
24#include "wx/uri.h"
2b9afe40 25#include "wx/url.h"
dd65d8c8 26
b60b2ec8 27// Test wxURL & wxURI compat?
9fc7a1d2
VZ
28#define TEST_URL wxUSE_URL
29
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
32// so the tests fail
33#define TEST_NETWORK 0
b60b2ec8 34
dd65d8c8
RN
35// ----------------------------------------------------------------------------
36// test class
37// ----------------------------------------------------------------------------
38
39class URITestCase : public CppUnit::TestCase
40{
41public:
42 URITestCase();
43
44private:
45 CPPUNIT_TEST_SUITE( URITestCase );
46 CPPUNIT_TEST( IPv4 );
47 CPPUNIT_TEST( IPv6 );
c4dbb953 48 CPPUNIT_TEST( Server );
dd65d8c8 49 CPPUNIT_TEST( Paths );
62e3e6c2 50 CPPUNIT_TEST( UserAndPass );
dd65d8c8
RN
51 CPPUNIT_TEST( NormalResolving );
52 CPPUNIT_TEST( ComplexResolving );
53 CPPUNIT_TEST( ReallyComplexResolving );
54 CPPUNIT_TEST( QueryFragmentResolving );
55 CPPUNIT_TEST( BackwardsResolving );
56 CPPUNIT_TEST( Assignment );
57 CPPUNIT_TEST( Comparison );
24ca04e7 58 CPPUNIT_TEST( Unescaping );
97ad053b 59 CPPUNIT_TEST( FileScheme );
b60b2ec8
RN
60#if TEST_URL
61 CPPUNIT_TEST( URLCompat );
402ccadc 62#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 63 CPPUNIT_TEST( URLProxy );
26531700 64#endif
b60b2ec8 65#endif
dd65d8c8
RN
66 CPPUNIT_TEST_SUITE_END();
67
68 void IPv4();
69 void IPv6();
c4dbb953 70 void Server();
dd65d8c8 71 void Paths();
62e3e6c2 72 void UserAndPass();
dd65d8c8
RN
73 void NormalResolving();
74 void ComplexResolving();
75 void ReallyComplexResolving();
76 void QueryFragmentResolving();
77 void BackwardsResolving();
78 void Assignment();
79 void Comparison();
24ca04e7 80 void Unescaping();
97ad053b 81 void FileScheme();
dd65d8c8 82
86470d43 83#if TEST_URL
b60b2ec8 84 void URLCompat();
402ccadc 85#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 86 void URLProxy();
402ccadc 87#endif
b60b2ec8
RN
88#endif
89
dd65d8c8
RN
90 DECLARE_NO_COPY_CLASS(URITestCase)
91};
92
93// register in the unnamed registry so that these tests are run by default
94CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
95
96// also include in it's own registry so that these tests can be run alone
97CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
98
99URITestCase::URITestCase()
100{
101}
102
5c3d5ef2 103// apply the given accessor to the URI, check that the result is as expected
c3a65218 104#define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
5c3d5ef2 105 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
dd65d8c8 106
c3a65218
VZ
107#define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
109
c4dbb953
VZ
110#define URI_ASSERT_SERVER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
112
c3a65218
VZ
113#define URI_ASSERT_PATH_EQUAL(uri, expected) \
114 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
115
62e3e6c2
VZ
116#define URI_ASSERT_USER_EQUAL(uri, expected) \
117 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
118
dd65d8c8
RN
119void URITestCase::IPv4()
120{
c3a65218
VZ
121 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
122 wxURI_IPV4ADDRESS);
dd65d8c8 123
c3a65218
VZ
124 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
125 wxURI_IPV4ADDRESS);
dd65d8c8 126
c3a65218 127 // bogus ipv4
5c3d5ef2
VZ
128 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
129 GetHostType() != wxURI_IPV4ADDRESS);
dd65d8c8
RN
130}
131
132void URITestCase::IPv6()
133{
dd65d8c8
RN
134 // IPv6address = 6( h16 ":" ) ls32
135 // / "::" 5( h16 ":" ) ls32
136 // / [ h16 ] "::" 4( h16 ":" ) ls32
137 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
138 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
139 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
140 // / [ *4( h16 ":" ) h16 ] "::" ls32
141 // / [ *5( h16 ":" ) h16 ] "::" h16
142 // / [ *6( h16 ":" ) h16 ] "::"
143 // ls32 = ( h16 ":" h16 ) / IPv4address
2b9afe40 144
c3a65218
VZ
145 URI_ASSERT_HOSTTYPE_EQUAL
146 (
147 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
148 wxURI_IPV6ADDRESS
149 );
150
151 URI_ASSERT_HOSTTYPE_EQUAL
152 (
153 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
154 wxURI_IPV6ADDRESS
155 );
156
157 URI_ASSERT_HOSTTYPE_EQUAL
158 (
159 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
160 wxURI_IPV6ADDRESS
161 );
162
163 URI_ASSERT_HOSTTYPE_EQUAL
164 (
165 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
166 wxURI_IPV6ADDRESS
167 );
dd65d8c8
RN
168}
169
c4dbb953
VZ
170void URITestCase::Server()
171{
172 URI_ASSERT_SERVER_EQUAL("http://foo/", "foo");
173 URI_ASSERT_SERVER_EQUAL("http://foo-bar/", "foo-bar");
174 URI_ASSERT_SERVER_EQUAL("http://foo/bar/", "foo");
175 URI_ASSERT_SERVER_EQUAL("http://192.168.1.0/", "192.168.1.0");
176 URI_ASSERT_SERVER_EQUAL("http://192.168.1.17/", "192.168.1.17");
177 URI_ASSERT_SERVER_EQUAL("http://192.168.1.255/", "192.168.1.255");
178 URI_ASSERT_SERVER_EQUAL("http://192.168.1.1/index.html", "192.168.1.1");
179 URI_ASSERT_SERVER_EQUAL("http://[aa:aa:aa:aa::aa:aa]/foo", "aa:aa:aa:aa::aa:aa");
180}
181
dd65d8c8
RN
182void URITestCase::Paths()
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 "/");
dd65d8c8 189
c3a65218
VZ
190 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
191 "/path/");
dd65d8c8 192
c3a65218
VZ
193 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
194 "/path/");
dd65d8c8 195
c3a65218
VZ
196 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
197 "../joe", BuildURI());
dd65d8c8 198}
dd65d8c8 199
62e3e6c2
VZ
200void URITestCase::UserAndPass()
201{
202 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
203 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
204 URI_ASSERT_USER_EQUAL("http://host/path/", "");
205}
206
5c3d5ef2 207#define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
2186321f
VZ
208 { \
209 wxURI uri(string); \
210 uri.Resolve(masteruri, strict); \
211 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
212 }
dd65d8c8 213
5c3d5ef2
VZ
214#define URI_TEST_RESOLVE(string, eq) \
215 URI_TEST_RESOLVE_IMPL(string, eq, true);
216
217#define URI_TEST_RESOLVE_LAX(string, eq) \
218 URI_TEST_RESOLVE_IMPL(string, eq, false);
dd65d8c8
RN
219
220
221//examples taken from RFC 2396.bis
222
223void URITestCase::NormalResolving()
224{
2186321f 225 wxURI masteruri("http://a/b/c/d;p?q");
2b9afe40 226
5c3d5ef2
VZ
227 URI_TEST_RESOLVE("g:h" ,"g:h")
228 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
229 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
230 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
231 URI_TEST_RESOLVE("/g" ,"http://a/g")
232 URI_TEST_RESOLVE("//g" ,"http://g")
233 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
234 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
235 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
236 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
237 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
238 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
239 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
240 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
241
242 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
243 URI_TEST_RESOLVE("." ,"http://a/b/c/")
244 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
245 URI_TEST_RESOLVE(".." ,"http://a/b/")
246 URI_TEST_RESOLVE("../" ,"http://a/b/")
247 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
248 URI_TEST_RESOLVE("../..","http://a/")
249 URI_TEST_RESOLVE("../../" , "http://a/")
250 URI_TEST_RESOLVE("../../g" , "http://a/g")
dd65d8c8
RN
251}
252
253void URITestCase::ComplexResolving()
254{
2186321f 255 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
256
257 //odd path examples
2186321f
VZ
258 URI_TEST_RESOLVE("../../../g" , "http://a/g")
259 URI_TEST_RESOLVE("../../../../g", "http://a/g")
260
5c3d5ef2
VZ
261 URI_TEST_RESOLVE("/./g" ,"http://a/g")
262 URI_TEST_RESOLVE("/../g" ,"http://a/g")
263 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
264 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
265 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
266 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
dd65d8c8 267}
dd65d8c8
RN
268
269void URITestCase::ReallyComplexResolving()
270{
2186321f 271 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
272
273 //even more odder path examples
5c3d5ef2
VZ
274 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
275 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
276 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
277 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
278 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
279 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
dd65d8c8
RN
280}
281
282void URITestCase::QueryFragmentResolving()
283{
2186321f 284 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 285
2b9afe40 286 //query/fragment ambigiousness
5c3d5ef2
VZ
287 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
288 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
289 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
290 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
dd65d8c8
RN
291}
292
293void URITestCase::BackwardsResolving()
294{
2186321f 295 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 296
2b9afe40 297 //"NEW"
5c3d5ef2 298 URI_TEST_RESOLVE("http:g" , "http:g") //strict
2b9afe40 299 //bw compat
5c3d5ef2 300 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
dd65d8c8
RN
301}
302
303void URITestCase::Assignment()
304{
2186321f
VZ
305 wxURI uri1("http://mysite.com"),
306 uri2("http://mysite2.com");
dd65d8c8
RN
307
308 uri2 = uri1;
309
2186321f 310 CPPUNIT_ASSERT_EQUAL(uri1.BuildURI(), uri2.BuildURI());
dd65d8c8
RN
311}
312
313void URITestCase::Comparison()
314{
2186321f 315 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
dd65d8c8 316}
b60b2ec8 317
24ca04e7
VZ
318void URITestCase::Unescaping()
319{
2186321f
VZ
320 wxString escaped,
321 unescaped;
322
323 escaped = "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
324 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
325
326 unescaped = wxURI(escaped).BuildUnescapedURI();
24ca04e7 327
2186321f
VZ
328 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
329 "escaping\\that\\seems\\broken\\sadly[1].rss",
330 unescaped );
24ca04e7 331
2186321f 332 CPPUNIT_ASSERT_EQUAL( unescaped, wxURI::Unescape(escaped) );
24ca04e7 333
24ca04e7 334
2186321f
VZ
335 escaped = "http://ru.wikipedia.org/wiki/"
336 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
24ca04e7 337
2186321f 338 unescaped = wxURI::Unescape(escaped);
24ca04e7 339
2186321f
VZ
340 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
341 "http://ru.wikipedia.org/wiki/"
342 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
343 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
344 ),
345 unescaped );
24ca04e7 346}
97ad053b
VZ
347
348void URITestCase::FileScheme()
349{
c3a65218
VZ
350 //file:// variety (NOT CONFORMING TO THE RFC)
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 //file: variety
c3a65218
VZ
363 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
364 "e:/wxcode/script1.xml" );
97ad053b
VZ
365}
366
b60b2ec8
RN
367#if TEST_URL
368
369#include "wx/url.h"
24ca04e7 370#include "wx/file.h"
b60b2ec8
RN
371
372void URITestCase::URLCompat()
373{
2186321f 374 wxURL url("http://user:password@wxwidgets.org");
b60b2ec8
RN
375
376 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
2b9afe40 377
9fc7a1d2 378#if TEST_NETWORK
b60b2ec8
RN
379 wxInputStream* pInput = url.GetInputStream();
380
381 CPPUNIT_ASSERT( pInput != NULL );
9fc7a1d2 382#endif
b60b2ec8 383
2186321f 384 CPPUNIT_ASSERT( url == wxURL("http://user:password@wxwidgets.org") );
b60b2ec8 385
2186321f 386 wxURI uri("http://user:password@wxwidgets.org");
b60b2ec8
RN
387
388 CPPUNIT_ASSERT( url == uri );
389
390 wxURL urlcopy(uri);
391
392 CPPUNIT_ASSERT( urlcopy == url );
393 CPPUNIT_ASSERT( urlcopy == uri );
394
395 wxURI uricopy(url);
396
397 CPPUNIT_ASSERT( uricopy == url );
398 CPPUNIT_ASSERT( uricopy == urlcopy );
399 CPPUNIT_ASSERT( uricopy == uri );
2186321f 400 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
113e181a 401
2186321f 402 wxURI test("file:\"myf\"ile.txt");
113e181a 403
2186321f
VZ
404 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test.BuildURI() );
405 CPPUNIT_ASSERT_EQUAL( "file", test.GetScheme() );
406 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test.GetPath() );
24fec908 407
6c1a7bc4
MW
408 // these could be put under a named registry since they take some
409 // time to complete
410#if 0
24fec908 411 // Test problem urls (reported not to work some time ago by a user...)
2186321f
VZ
412 const wxChar* pszProblemUrls[] = { "http://www.csdn.net",
413 "http://www.163.com",
414 "http://www.sina.com.cn" };
415
24fec908
VZ
416 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
417 {
418 wxURL urlProblem(pszProblemUrls[i]);
419 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
420
421 wxInputStream* is = urlProblem.GetInputStream();
422 CPPUNIT_ASSERT(is != NULL);
423
424 wxFile fOut(_T("test.html"), wxFile::write);
425 wxASSERT(fOut.IsOpened());
426
427 char buf[1001];
428 for( ;; )
429 {
430 is->Read(buf, 1000);
431 size_t n = is->LastRead();
432 if ( n == 0 )
433 break;
434 buf[n] = 0;
435 fOut.Write(buf, n);
436 }
437
438 delete is;
439 }
6c1a7bc4 440#endif
b60b2ec8
RN
441}
442
402ccadc
VZ
443// the purpose of this test is unclear, it seems to be unfinished so disabling
444// it for now
445#if 0 && wxUSE_PROTOCOL_HTTP
e605541c
RN
446void URITestCase::URLProxy()
447{
6ca3a322
WS
448 wxURL url(wxT("http://www.asite.com/index.html"));
449 url.SetProxy(wxT("pserv:3122"));
fd725bce
WS
450
451 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
452 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
453 wxInputStream *data = url2.GetInputStream();
454 CPPUNIT_ASSERT(data != NULL);
e605541c 455}
26531700 456#endif // wxUSE_PROTOCOL_HTTP
24ca04e7
VZ
457
458#endif // TEST_URL