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