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