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