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