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