]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
splitted gizmos library into gizmos and gizmos_xrc in order to fix broken compilation...
[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 ( 1 && wxUSE_URL )
29
30 // ----------------------------------------------------------------------------
31 // test class
32 // ----------------------------------------------------------------------------
33
34 class URITestCase : public CppUnit::TestCase
35 {
36 public:
37 URITestCase();
38
39 private:
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 );
51 #if TEST_URL
52 CPPUNIT_TEST( URLCompat );
53 #if wxUSE_PROTOCOL_HTTP
54 CPPUNIT_TEST( URLProxy );
55 #endif
56 #endif
57 CPPUNIT_TEST_SUITE_END();
58
59 void IPv4();
60 void IPv6();
61 void Paths();
62 void NormalResolving();
63 void ComplexResolving();
64 void ReallyComplexResolving();
65 void QueryFragmentResolving();
66 void BackwardsResolving();
67 void Assignment();
68 void Comparison();
69
70 #if TEST_URL
71 void URLCompat();
72 void URLProxy();
73 #endif
74
75 DECLARE_NO_COPY_CLASS(URITestCase)
76 };
77
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
80
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
83
84 URITestCase::URITestCase()
85 {
86 }
87
88
89 #define URI_TEST(uristring, cond) \
90 uri = new wxURI(wxT(uristring));\
91 CPPUNIT_ASSERT(cond);\
92 delete uri;
93
94 #define URI_PRINT(uri)\
95 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
96 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
97 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
98 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
99 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
100 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
101 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
102
103 void URITestCase::IPv4()
104 {
105 wxURI* uri;
106
107
108 URI_TEST("http://user:password@192.168.1.100:5050/path",
109 uri->GetHostType() == wxURI_IPV4ADDRESS);
110
111 URI_TEST("http://user:password@192.255.1.100:5050/path",
112 uri->GetHostType() == wxURI_IPV4ADDRESS);
113
114 //bogus ipv4
115 URI_TEST("http://user:password@192.256.1.100:5050/path",
116 uri->GetHostType() != wxURI_IPV4ADDRESS);
117 }
118
119 void URITestCase::IPv6()
120 {
121 wxURI* uri;
122
123 // IPv6address = 6( h16 ":" ) ls32
124 // / "::" 5( h16 ":" ) ls32
125 // / [ h16 ] "::" 4( h16 ":" ) ls32
126 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
127 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
128 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
129 // / [ *4( h16 ":" ) h16 ] "::" ls32
130 // / [ *5( h16 ":" ) h16 ] "::" h16
131 // / [ *6( h16 ":" ) h16 ] "::"
132 // ls32 = ( h16 ":" h16 ) / IPv4address
133
134 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
135 uri->GetHostType() == wxURI_IPV6ADDRESS);
136
137 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
138 uri->GetHostType() == wxURI_IPV6ADDRESS);
139
140 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
141 uri->GetHostType() == wxURI_IPV6ADDRESS);
142
143 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
144 uri->GetHostType() == wxURI_IPV6ADDRESS);
145 }
146
147 void URITestCase::Paths()
148 {
149 wxURI* uri;
150
151 //path tests
152 URI_TEST("http://user:password@192.256.1.100:5050/../path",
153 uri->GetPath() == wxT("/path"));
154
155 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
156 uri->GetPath() == wxT("/"));
157
158 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
159 uri->GetPath() == wxT("/path/"));
160
161 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
162 uri->GetPath() == wxT("/path/"));
163
164 URI_TEST("path/john/../../../joe",
165 uri->BuildURI() == wxT("../joe"));
166 }
167 #undef URI_TEST
168
169 #define URI_TEST_RESOLVE(string, eq, strict) \
170 uri = new wxURI(wxT(string));\
171 uri->Resolve(masteruri, strict);\
172 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
173 delete uri;
174
175 #define URI_TEST(string, eq) \
176 URI_TEST_RESOLVE(string, eq, true);
177
178
179 //examples taken from RFC 2396.bis
180
181 void URITestCase::NormalResolving()
182 {
183 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
184 wxURI* uri;
185
186 URI_TEST("g:h" ,"g:h")
187 URI_TEST("g" ,"http://a/b/c/g")
188 URI_TEST("./g" ,"http://a/b/c/g")
189 URI_TEST("g/" ,"http://a/b/c/g/")
190 URI_TEST("/g" ,"http://a/g")
191 URI_TEST("//g" ,"http://g")
192 URI_TEST("?y" ,"http://a/b/c/d;p?y")
193 URI_TEST("g?y" ,"http://a/b/c/g?y")
194 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
195 URI_TEST("g#s" ,"http://a/b/c/g#s")
196 URI_TEST("g?y#s","http://a/b/c/g?y#s")
197 URI_TEST(";x" ,"http://a/b/c/;x")
198 URI_TEST("g;x" ,"http://a/b/c/g;x")
199 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
200
201 URI_TEST("" ,"http://a/b/c/d;p?q")
202 URI_TEST("." ,"http://a/b/c/")
203 URI_TEST("./" ,"http://a/b/c/")
204 URI_TEST(".." ,"http://a/b/")
205 URI_TEST("../" ,"http://a/b/")
206 URI_TEST("../g" ,"http://a/b/g")
207 URI_TEST("../..","http://a/")
208 URI_TEST("../../" , "http://a/")
209 URI_TEST("../../g" , "http://a/g")
210 }
211
212 void URITestCase::ComplexResolving()
213 {
214 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
215 wxURI* uri;
216
217 //odd path examples
218 URI_TEST("/./g" ,"http://a/g")
219 URI_TEST("/../g" ,"http://a/g")
220 URI_TEST("g." ,"http://a/b/c/g.")
221 URI_TEST(".g" ,"http://a/b/c/.g")
222 URI_TEST("g.." ,"http://a/b/c/g..")
223 URI_TEST("..g" ,"http://a/b/c/..g")
224 }
225 //Should Fail
226 //"../../../g" = "http://a/g"
227 //"../../../../g" = "http://a/g"
228
229 void URITestCase::ReallyComplexResolving()
230 {
231 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
232 wxURI* uri;
233
234 //even more odder path examples
235 URI_TEST("./../g" ,"http://a/b/g")
236 URI_TEST("./g/." ,"http://a/b/c/g/")
237 URI_TEST("g/./h" ,"http://a/b/c/g/h")
238 URI_TEST("g/../h" ,"http://a/b/c/h")
239 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
240 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
241 }
242
243 void URITestCase::QueryFragmentResolving()
244 {
245 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
246 wxURI* uri;
247
248 //query/fragment ambigiousness
249 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
250 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
251 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
252 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
253 }
254
255 void URITestCase::BackwardsResolving()
256 {
257 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
258 wxURI* uri;
259
260 //"NEW"
261 URI_TEST("http:g" , "http:g") //strict
262 //bw compat
263 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
264 }
265
266 void URITestCase::Assignment()
267 {
268 wxURI uri1(wxT("http://mysite.com")),
269 uri2(wxT("http://mysite2.com"));
270
271 uri2 = uri1;
272
273 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
274 }
275
276 void URITestCase::Comparison()
277 {
278 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
279 }
280
281 #if TEST_URL
282
283 #include "wx/url.h"
284
285 void URITestCase::URLCompat()
286 {
287 wxURL url(wxT("http://user:password@wxwidgets.org"));
288
289 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
290
291 wxInputStream* pInput = url.GetInputStream();
292
293 CPPUNIT_ASSERT( pInput != NULL );
294
295 CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
296
297 wxURI uri(wxT("http://user:password@wxwidgets.org"));
298
299 CPPUNIT_ASSERT( url == uri );
300
301 wxURL urlcopy(uri);
302
303 CPPUNIT_ASSERT( urlcopy == url );
304 CPPUNIT_ASSERT( urlcopy == uri );
305
306 wxURI uricopy(url);
307
308 CPPUNIT_ASSERT( uricopy == url );
309 CPPUNIT_ASSERT( uricopy == urlcopy );
310 CPPUNIT_ASSERT( uricopy == uri );
311 #if WXWIN_COMPATIBILITY_2_4
312 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
313 #endif
314 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
315
316 wxURI test(wxT("file:\"myf\"ile.txt"));
317
318 CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
319 CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
320 CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
321 }
322
323 #if wxUSE_PROTOCOL_HTTP
324 void URITestCase::URLProxy()
325 {
326 wxURL url(wxT("http://www.asite.com/index.html"));
327 url.SetProxy(wxT("pserv:3122"));
328 }
329 #endif // wxUSE_PROTOCOL_HTTP
330 #endif
331