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