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