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