]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/uris.cpp
add wxURL::GetInputStream test unit; add a global IsNetworkAvailable() utility to...
[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
dd65d8c8
RN
30// ----------------------------------------------------------------------------
31// test class
32// ----------------------------------------------------------------------------
33
34class URITestCase : public CppUnit::TestCase
35{
36public:
37 URITestCase();
38
39private:
40 CPPUNIT_TEST_SUITE( URITestCase );
41 CPPUNIT_TEST( IPv4 );
42 CPPUNIT_TEST( IPv6 );
c4dbb953 43 CPPUNIT_TEST( Server );
dd65d8c8 44 CPPUNIT_TEST( Paths );
62e3e6c2 45 CPPUNIT_TEST( UserAndPass );
dd65d8c8
RN
46 CPPUNIT_TEST( NormalResolving );
47 CPPUNIT_TEST( ComplexResolving );
48 CPPUNIT_TEST( ReallyComplexResolving );
49 CPPUNIT_TEST( QueryFragmentResolving );
50 CPPUNIT_TEST( BackwardsResolving );
51 CPPUNIT_TEST( Assignment );
52 CPPUNIT_TEST( Comparison );
24ca04e7 53 CPPUNIT_TEST( Unescaping );
97ad053b 54 CPPUNIT_TEST( FileScheme );
b60b2ec8
RN
55#if TEST_URL
56 CPPUNIT_TEST( URLCompat );
402ccadc 57#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 58 CPPUNIT_TEST( URLProxy );
26531700 59#endif
b60b2ec8 60#endif
dd65d8c8
RN
61 CPPUNIT_TEST_SUITE_END();
62
63 void IPv4();
64 void IPv6();
c4dbb953 65 void Server();
dd65d8c8 66 void Paths();
62e3e6c2 67 void UserAndPass();
dd65d8c8
RN
68 void NormalResolving();
69 void ComplexResolving();
70 void ReallyComplexResolving();
71 void QueryFragmentResolving();
72 void BackwardsResolving();
73 void Assignment();
74 void Comparison();
24ca04e7 75 void Unescaping();
97ad053b 76 void FileScheme();
dd65d8c8 77
86470d43 78#if TEST_URL
b60b2ec8 79 void URLCompat();
402ccadc 80#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 81 void URLProxy();
402ccadc 82#endif
b60b2ec8
RN
83#endif
84
dd65d8c8
RN
85 DECLARE_NO_COPY_CLASS(URITestCase)
86};
87
88// register in the unnamed registry so that these tests are run by default
89CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
90
91// also include in it's own registry so that these tests can be run alone
92CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
93
94URITestCase::URITestCase()
95{
96}
97
5c3d5ef2 98// apply the given accessor to the URI, check that the result is as expected
c3a65218 99#define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
5c3d5ef2 100 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
dd65d8c8 101
c3a65218
VZ
102#define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
103 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
104
c4dbb953
VZ
105#define URI_ASSERT_SERVER_EQUAL(uri, expected) \
106 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
107
c3a65218
VZ
108#define URI_ASSERT_PATH_EQUAL(uri, expected) \
109 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
110
62e3e6c2
VZ
111#define URI_ASSERT_USER_EQUAL(uri, expected) \
112 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
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
62e3e6c2
VZ
195void URITestCase::UserAndPass()
196{
197 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
198 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
199 URI_ASSERT_USER_EQUAL("http://host/path/", "");
200}
201
5c3d5ef2 202#define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
2186321f
VZ
203 { \
204 wxURI uri(string); \
205 uri.Resolve(masteruri, strict); \
206 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
207 }
dd65d8c8 208
5c3d5ef2
VZ
209#define URI_TEST_RESOLVE(string, eq) \
210 URI_TEST_RESOLVE_IMPL(string, eq, true);
211
212#define URI_TEST_RESOLVE_LAX(string, eq) \
213 URI_TEST_RESOLVE_IMPL(string, eq, false);
dd65d8c8
RN
214
215
216//examples taken from RFC 2396.bis
217
218void URITestCase::NormalResolving()
219{
2186321f 220 wxURI masteruri("http://a/b/c/d;p?q");
2b9afe40 221
5c3d5ef2
VZ
222 URI_TEST_RESOLVE("g:h" ,"g:h")
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/b/c/g/")
226 URI_TEST_RESOLVE("/g" ,"http://a/g")
227 URI_TEST_RESOLVE("//g" ,"http://g")
228 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
229 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
230 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
231 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
232 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
233 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
234 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
235 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
236
237 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
238 URI_TEST_RESOLVE("." ,"http://a/b/c/")
239 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
240 URI_TEST_RESOLVE(".." ,"http://a/b/")
241 URI_TEST_RESOLVE("../" ,"http://a/b/")
242 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
243 URI_TEST_RESOLVE("../..","http://a/")
244 URI_TEST_RESOLVE("../../" , "http://a/")
245 URI_TEST_RESOLVE("../../g" , "http://a/g")
dd65d8c8
RN
246}
247
248void URITestCase::ComplexResolving()
249{
2186321f 250 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
251
252 //odd path examples
2186321f
VZ
253 URI_TEST_RESOLVE("../../../g" , "http://a/g")
254 URI_TEST_RESOLVE("../../../../g", "http://a/g")
255
5c3d5ef2
VZ
256 URI_TEST_RESOLVE("/./g" ,"http://a/g")
257 URI_TEST_RESOLVE("/../g" ,"http://a/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 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
dd65d8c8 262}
dd65d8c8
RN
263
264void URITestCase::ReallyComplexResolving()
265{
2186321f 266 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8
RN
267
268 //even more odder path examples
5c3d5ef2
VZ
269 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
270 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
271 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
272 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
273 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
274 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
dd65d8c8
RN
275}
276
277void URITestCase::QueryFragmentResolving()
278{
2186321f 279 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 280
2b9afe40 281 //query/fragment ambigiousness
5c3d5ef2
VZ
282 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
283 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
284 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
285 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
dd65d8c8
RN
286}
287
288void URITestCase::BackwardsResolving()
289{
2186321f 290 wxURI masteruri("http://a/b/c/d;p?q");
dd65d8c8 291
2b9afe40 292 //"NEW"
5c3d5ef2 293 URI_TEST_RESOLVE("http:g" , "http:g") //strict
2b9afe40 294 //bw compat
5c3d5ef2 295 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
dd65d8c8
RN
296}
297
298void URITestCase::Assignment()
299{
2186321f
VZ
300 wxURI uri1("http://mysite.com"),
301 uri2("http://mysite2.com");
dd65d8c8
RN
302
303 uri2 = uri1;
304
2186321f 305 CPPUNIT_ASSERT_EQUAL(uri1.BuildURI(), uri2.BuildURI());
dd65d8c8
RN
306}
307
308void URITestCase::Comparison()
309{
2186321f 310 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
dd65d8c8 311}
b60b2ec8 312
24ca04e7
VZ
313void URITestCase::Unescaping()
314{
2186321f
VZ
315 wxString escaped,
316 unescaped;
317
318 escaped = "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
319 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
320
321 unescaped = wxURI(escaped).BuildUnescapedURI();
24ca04e7 322
2186321f
VZ
323 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
324 "escaping\\that\\seems\\broken\\sadly[1].rss",
325 unescaped );
24ca04e7 326
2186321f 327 CPPUNIT_ASSERT_EQUAL( unescaped, wxURI::Unescape(escaped) );
24ca04e7 328
24ca04e7 329
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 );
24ca04e7 341}
97ad053b
VZ
342
343void URITestCase::FileScheme()
344{
c3a65218
VZ
345 //file:// variety (NOT CONFORMING TO THE RFC)
346 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
347 "e:/wxcode/script1.xml" );
97ad053b
VZ
348
349 //file:/// variety
c3a65218
VZ
350 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
351 "/e:/wxcode/script1.xml" );
97ad053b
VZ
352
353 //file:/ variety
c3a65218
VZ
354 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
355 "/e:/wxcode/script1.xml" );
97ad053b
VZ
356
357 //file: variety
c3a65218
VZ
358 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
359 "e:/wxcode/script1.xml" );
97ad053b
VZ
360}
361
b60b2ec8
RN
362#if TEST_URL
363
364#include "wx/url.h"
24ca04e7 365#include "wx/file.h"
b60b2ec8
RN
366
367void URITestCase::URLCompat()
368{
2186321f 369 wxURL url("http://user:password@wxwidgets.org");
b60b2ec8 370
1f51673b 371 CPPUNIT_ASSERT( url.GetError() == wxURL_NOERR );
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