]>
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 ); | |
b60b2ec8 RN |
51 | #if TEST_URL |
52 | CPPUNIT_TEST( URLCompat ); | |
26531700 | 53 | #if wxUSE_PROTOCOL_HTTP |
e605541c | 54 | CPPUNIT_TEST( URLProxy ); |
26531700 | 55 | #endif |
b60b2ec8 | 56 | #endif |
dd65d8c8 RN |
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 | ||
86470d43 | 70 | #if TEST_URL |
b60b2ec8 | 71 | void URLCompat(); |
e605541c | 72 | void URLProxy(); |
b60b2ec8 RN |
73 | #endif |
74 | ||
dd65d8c8 RN |
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 | ||
2b9afe40 | 108 | URI_TEST("http://user:password@192.168.1.100:5050/path", |
dd65d8c8 RN |
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 | |
2b9afe40 WS |
133 | |
134 | URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path", | |
dd65d8c8 RN |
135 | uri->GetHostType() == wxURI_IPV6ADDRESS); |
136 | ||
2b9afe40 | 137 | URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path", |
dd65d8c8 RN |
138 | uri->GetHostType() == wxURI_IPV6ADDRESS); |
139 | ||
2b9afe40 | 140 | URI_TEST("http://user:password@[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]:5050/path", |
dd65d8c8 RN |
144 | uri->GetHostType() == wxURI_IPV6ADDRESS); |
145 | } | |
146 | ||
147 | void URITestCase::Paths() | |
148 | { | |
149 | wxURI* uri; | |
150 | ||
151 | //path tests | |
2b9afe40 | 152 | URI_TEST("http://user:password@192.256.1.100:5050/../path", |
dd65d8c8 RN |
153 | uri->GetPath() == wxT("/path")); |
154 | ||
2b9afe40 | 155 | URI_TEST("http://user:password@192.256.1.100:5050/path/../", |
dd65d8c8 RN |
156 | uri->GetPath() == wxT("/")); |
157 | ||
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 | 162 | uri->GetPath() == wxT("/path/")); |
2b9afe40 WS |
163 | |
164 | URI_TEST("path/john/../../../joe", | |
86470d43 | 165 | uri->BuildURI() == wxT("../joe")); |
dd65d8c8 RN |
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);\ | |
86470d43 | 172 | CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\ |
dd65d8c8 RN |
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; | |
2b9afe40 | 185 | |
dd65d8c8 | 186 | URI_TEST("g:h" ,"g:h") |
2b9afe40 WS |
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") | |
dd65d8c8 | 193 | URI_TEST("g?y" ,"http://a/b/c/g?y") |
2b9afe40 WS |
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") | |
dd65d8c8 RN |
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 | |
2b9afe40 WS |
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") | |
dd65d8c8 RN |
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 | |
2b9afe40 WS |
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") | |
dd65d8c8 RN |
241 | } |
242 | ||
243 | void URITestCase::QueryFragmentResolving() | |
244 | { | |
245 | wxURI masteruri(wxT("http://a/b/c/d;p?q")); | |
246 | wxURI* uri; | |
247 | ||
2b9afe40 WS |
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") | |
dd65d8c8 RN |
253 | } |
254 | ||
255 | void URITestCase::BackwardsResolving() | |
256 | { | |
257 | wxURI masteruri(wxT("http://a/b/c/d;p?q")); | |
258 | wxURI* uri; | |
259 | ||
2b9afe40 WS |
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); | |
dd65d8c8 RN |
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 | ||
86470d43 | 273 | CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI()); |
dd65d8c8 RN |
274 | } |
275 | ||
276 | void URITestCase::Comparison() | |
277 | { | |
278 | CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com"))); | |
279 | } | |
b60b2ec8 RN |
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); | |
2b9afe40 | 290 | |
b60b2ec8 RN |
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 ); | |
86470d43 RN |
311 | #if WXWIN_COMPATIBILITY_2_4 |
312 | CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") ); | |
313 | #endif | |
113e181a RN |
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") ); | |
b60b2ec8 RN |
321 | } |
322 | ||
26531700 | 323 | #if wxUSE_PROTOCOL_HTTP |
e605541c RN |
324 | void URITestCase::URLProxy() |
325 | { | |
6ca3a322 WS |
326 | wxURL url(wxT("http://www.asite.com/index.html")); |
327 | url.SetProxy(wxT("pserv:3122")); | |
e605541c | 328 | } |
26531700 | 329 | #endif // wxUSE_PROTOCOL_HTTP |
b60b2ec8 | 330 | #endif |
e605541c | 331 |