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