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