]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/uris.cpp
Better 'missing WXUNUSED' workaround (perhaps unnecessary if nomore standalone option...
[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?
e605541c 28#define TEST_URL ( 1 && wxUSE_URL )
b60b2ec8 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 );
43 CPPUNIT_TEST( Paths );
44 CPPUNIT_TEST( NormalResolving );
45 CPPUNIT_TEST( ComplexResolving );
46 CPPUNIT_TEST( ReallyComplexResolving );
47 CPPUNIT_TEST( QueryFragmentResolving );
48 CPPUNIT_TEST( BackwardsResolving );
49 CPPUNIT_TEST( Assignment );
50 CPPUNIT_TEST( Comparison );
24ca04e7 51 CPPUNIT_TEST( Unescaping );
b60b2ec8
RN
52#if TEST_URL
53 CPPUNIT_TEST( URLCompat );
26531700 54#if wxUSE_PROTOCOL_HTTP
e605541c 55 CPPUNIT_TEST( URLProxy );
26531700 56#endif
b60b2ec8 57#endif
dd65d8c8
RN
58 CPPUNIT_TEST_SUITE_END();
59
60 void IPv4();
61 void IPv6();
62 void Paths();
63 void NormalResolving();
64 void ComplexResolving();
65 void ReallyComplexResolving();
66 void QueryFragmentResolving();
67 void BackwardsResolving();
68 void Assignment();
69 void Comparison();
24ca04e7 70 void Unescaping();
dd65d8c8 71
86470d43 72#if TEST_URL
b60b2ec8 73 void URLCompat();
e605541c 74 void URLProxy();
b60b2ec8
RN
75#endif
76
dd65d8c8
RN
77 DECLARE_NO_COPY_CLASS(URITestCase)
78};
79
80// register in the unnamed registry so that these tests are run by default
81CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
82
83// also include in it's own registry so that these tests can be run alone
84CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
85
86URITestCase::URITestCase()
87{
88}
89
90
91#define URI_TEST(uristring, cond) \
92 uri = new wxURI(wxT(uristring));\
93 CPPUNIT_ASSERT(cond);\
94 delete uri;
95
96#define URI_PRINT(uri)\
97 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
98 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
99 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
100 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
101 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
102 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
103 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
104
105void URITestCase::IPv4()
106{
107 wxURI* uri;
108
109
2b9afe40 110 URI_TEST("http://user:password@192.168.1.100:5050/path",
dd65d8c8
RN
111 uri->GetHostType() == wxURI_IPV4ADDRESS);
112
113 URI_TEST("http://user:password@192.255.1.100:5050/path",
114 uri->GetHostType() == wxURI_IPV4ADDRESS);
115
116 //bogus ipv4
117 URI_TEST("http://user:password@192.256.1.100:5050/path",
118 uri->GetHostType() != wxURI_IPV4ADDRESS);
119}
120
121void URITestCase::IPv6()
122{
123 wxURI* uri;
124
125 // IPv6address = 6( h16 ":" ) ls32
126 // / "::" 5( h16 ":" ) ls32
127 // / [ h16 ] "::" 4( h16 ":" ) ls32
128 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
129 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
130 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
131 // / [ *4( h16 ":" ) h16 ] "::" ls32
132 // / [ *5( h16 ":" ) h16 ] "::" h16
133 // / [ *6( h16 ":" ) h16 ] "::"
134 // ls32 = ( h16 ":" h16 ) / IPv4address
2b9afe40
WS
135
136 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
dd65d8c8
RN
137 uri->GetHostType() == wxURI_IPV6ADDRESS);
138
2b9afe40 139 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
dd65d8c8
RN
140 uri->GetHostType() == wxURI_IPV6ADDRESS);
141
2b9afe40 142 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
dd65d8c8
RN
143 uri->GetHostType() == wxURI_IPV6ADDRESS);
144
2b9afe40 145 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
dd65d8c8
RN
146 uri->GetHostType() == wxURI_IPV6ADDRESS);
147}
148
149void URITestCase::Paths()
150{
151 wxURI* uri;
152
153 //path tests
2b9afe40 154 URI_TEST("http://user:password@192.256.1.100:5050/../path",
dd65d8c8
RN
155 uri->GetPath() == wxT("/path"));
156
2b9afe40 157 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
dd65d8c8
RN
158 uri->GetPath() == wxT("/"));
159
2b9afe40 160 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
dd65d8c8
RN
161 uri->GetPath() == wxT("/path/"));
162
2b9afe40 163 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
dd65d8c8 164 uri->GetPath() == wxT("/path/"));
2b9afe40
WS
165
166 URI_TEST("path/john/../../../joe",
86470d43 167 uri->BuildURI() == wxT("../joe"));
dd65d8c8
RN
168}
169#undef URI_TEST
170
171#define URI_TEST_RESOLVE(string, eq, strict) \
172 uri = new wxURI(wxT(string));\
173 uri->Resolve(masteruri, strict);\
86470d43 174 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
dd65d8c8
RN
175 delete uri;
176
177#define URI_TEST(string, eq) \
178 URI_TEST_RESOLVE(string, eq, true);
179
180
181//examples taken from RFC 2396.bis
182
183void URITestCase::NormalResolving()
184{
185 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
186 wxURI* uri;
2b9afe40 187
dd65d8c8 188 URI_TEST("g:h" ,"g:h")
2b9afe40
WS
189 URI_TEST("g" ,"http://a/b/c/g")
190 URI_TEST("./g" ,"http://a/b/c/g")
191 URI_TEST("g/" ,"http://a/b/c/g/")
192 URI_TEST("/g" ,"http://a/g")
193 URI_TEST("//g" ,"http://g")
194 URI_TEST("?y" ,"http://a/b/c/d;p?y")
dd65d8c8 195 URI_TEST("g?y" ,"http://a/b/c/g?y")
2b9afe40
WS
196 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
197 URI_TEST("g#s" ,"http://a/b/c/g#s")
198 URI_TEST("g?y#s","http://a/b/c/g?y#s")
199 URI_TEST(";x" ,"http://a/b/c/;x")
200 URI_TEST("g;x" ,"http://a/b/c/g;x")
201 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
202
203 URI_TEST("" ,"http://a/b/c/d;p?q")
204 URI_TEST("." ,"http://a/b/c/")
205 URI_TEST("./" ,"http://a/b/c/")
206 URI_TEST(".." ,"http://a/b/")
207 URI_TEST("../" ,"http://a/b/")
208 URI_TEST("../g" ,"http://a/b/g")
209 URI_TEST("../..","http://a/")
210 URI_TEST("../../" , "http://a/")
211 URI_TEST("../../g" , "http://a/g")
dd65d8c8
RN
212}
213
214void URITestCase::ComplexResolving()
215{
216 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
217 wxURI* uri;
218
219 //odd path examples
2b9afe40
WS
220 URI_TEST("/./g" ,"http://a/g")
221 URI_TEST("/../g" ,"http://a/g")
222 URI_TEST("g." ,"http://a/b/c/g.")
223 URI_TEST(".g" ,"http://a/b/c/.g")
224 URI_TEST("g.." ,"http://a/b/c/g..")
225 URI_TEST("..g" ,"http://a/b/c/..g")
dd65d8c8
RN
226}
227 //Should Fail
228 //"../../../g" = "http://a/g"
229 //"../../../../g" = "http://a/g"
230
231void URITestCase::ReallyComplexResolving()
232{
233 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
234 wxURI* uri;
235
236 //even more odder path examples
2b9afe40
WS
237 URI_TEST("./../g" ,"http://a/b/g")
238 URI_TEST("./g/." ,"http://a/b/c/g/")
239 URI_TEST("g/./h" ,"http://a/b/c/g/h")
240 URI_TEST("g/../h" ,"http://a/b/c/h")
241 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
242 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
dd65d8c8
RN
243}
244
245void URITestCase::QueryFragmentResolving()
246{
247 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
248 wxURI* uri;
249
2b9afe40
WS
250 //query/fragment ambigiousness
251 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
252 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
253 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
254 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
dd65d8c8
RN
255}
256
257void URITestCase::BackwardsResolving()
258{
259 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
260 wxURI* uri;
261
2b9afe40
WS
262 //"NEW"
263 URI_TEST("http:g" , "http:g") //strict
264 //bw compat
265 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
dd65d8c8
RN
266}
267
268void URITestCase::Assignment()
269{
270 wxURI uri1(wxT("http://mysite.com")),
271 uri2(wxT("http://mysite2.com"));
272
273 uri2 = uri1;
274
86470d43 275 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
dd65d8c8
RN
276}
277
278void URITestCase::Comparison()
279{
280 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
281}
b60b2ec8 282
24ca04e7
VZ
283void URITestCase::Unescaping()
284{
285 wxString orig = wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
286 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
287
288 wxString works= wxURI(orig).BuildUnescapedURI();
289
290 CPPUNIT_ASSERT(orig.IsSameAs(works) == false);
291
292 wxString orig2 = wxT("http://test.com/of/file%3A%2F%")
293 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
294 wxT("5Cbroken%5Csadly%5B1%5D.rss");
295
296 wxString works2 = wxURI::Unescape(orig2);
297 wxString broken2 = wxURI(orig2).BuildUnescapedURI();
298
299 CPPUNIT_ASSERT(works2.IsSameAs(broken2));
300
301}
b60b2ec8
RN
302#if TEST_URL
303
24fec908
VZ
304const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
305 wxT("http://www.163.com"),
306 wxT("http://www.sina.com.cn") };
307
b60b2ec8 308#include "wx/url.h"
24ca04e7 309#include "wx/file.h"
b60b2ec8
RN
310
311void URITestCase::URLCompat()
312{
313 wxURL url(wxT("http://user:password@wxwidgets.org"));
314
315 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
2b9afe40 316
b60b2ec8
RN
317 wxInputStream* pInput = url.GetInputStream();
318
319 CPPUNIT_ASSERT( pInput != NULL );
320
321 CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
322
323 wxURI uri(wxT("http://user:password@wxwidgets.org"));
324
325 CPPUNIT_ASSERT( url == uri );
326
327 wxURL urlcopy(uri);
328
329 CPPUNIT_ASSERT( urlcopy == url );
330 CPPUNIT_ASSERT( urlcopy == uri );
331
332 wxURI uricopy(url);
333
334 CPPUNIT_ASSERT( uricopy == url );
335 CPPUNIT_ASSERT( uricopy == urlcopy );
336 CPPUNIT_ASSERT( uricopy == uri );
86470d43
RN
337#if WXWIN_COMPATIBILITY_2_4
338 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
339#endif
113e181a
RN
340 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
341
342 wxURI test(wxT("file:\"myf\"ile.txt"));
343
344 CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
345 CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
346 CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
24fec908
VZ
347
348 // Test problem urls (reported not to work some time ago by a user...)
349 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
350 {
351 wxURL urlProblem(pszProblemUrls[i]);
352 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
353
354 wxInputStream* is = urlProblem.GetInputStream();
355 CPPUNIT_ASSERT(is != NULL);
356
357 wxFile fOut(_T("test.html"), wxFile::write);
358 wxASSERT(fOut.IsOpened());
359
360 char buf[1001];
361 for( ;; )
362 {
363 is->Read(buf, 1000);
364 size_t n = is->LastRead();
365 if ( n == 0 )
366 break;
367 buf[n] = 0;
368 fOut.Write(buf, n);
369 }
370
371 delete is;
372 }
b60b2ec8
RN
373}
374
26531700 375#if wxUSE_PROTOCOL_HTTP
e605541c
RN
376void URITestCase::URLProxy()
377{
6ca3a322
WS
378 wxURL url(wxT("http://www.asite.com/index.html"));
379 url.SetProxy(wxT("pserv:3122"));
fd725bce
WS
380
381 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
382 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
383 wxInputStream *data = url2.GetInputStream();
384 CPPUNIT_ASSERT(data != NULL);
e605541c 385}
26531700 386#endif // wxUSE_PROTOCOL_HTTP
24ca04e7
VZ
387
388#endif // TEST_URL