]>
git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxURI unit test
7 // Copyright: (c) 2004 Ryan Norton
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
27 // Test wxURL & wxURI compat?
28 #define TEST_URL ( 1 && wxUSE_URL )
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class URITestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( URITestCase
);
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
);
52 CPPUNIT_TEST( URLCompat
);
53 #if wxUSE_PROTOCOL_HTTP
54 CPPUNIT_TEST( URLProxy
);
57 CPPUNIT_TEST_SUITE_END();
62 void NormalResolving();
63 void ComplexResolving();
64 void ReallyComplexResolving();
65 void QueryFragmentResolving();
66 void BackwardsResolving();
75 DECLARE_NO_COPY_CLASS(URITestCase
)
78 // register in the unnamed registry so that these tests are run by default
79 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
81 // also include in it's own registry so that these tests can be run alone
82 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
84 URITestCase::URITestCase()
89 #define URI_TEST(uristring, cond) \
90 uri = new wxURI(wxT(uristring));\
91 CPPUNIT_ASSERT(cond);\
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());
103 void URITestCase::IPv4()
108 URI_TEST("http://user:password@192.168.1.100:5050/path",
109 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
111 URI_TEST("http://user:password@192.255.1.100:5050/path",
112 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
115 URI_TEST("http://user:password@192.256.1.100:5050/path",
116 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
119 void URITestCase::IPv6()
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
134 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
135 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
137 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
138 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
140 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
141 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
143 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
144 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
147 void URITestCase::Paths()
152 URI_TEST("http://user:password@192.256.1.100:5050/../path",
153 uri
->GetPath() == wxT("/path"));
155 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
156 uri
->GetPath() == wxT("/"));
158 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
159 uri
->GetPath() == wxT("/path/"));
161 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
162 uri
->GetPath() == wxT("/path/"));
164 URI_TEST("path/john/../../../joe",
165 uri
->BuildURI() == wxT("../joe"));
169 #define URI_TEST_RESOLVE(string, eq, strict) \
170 uri = new wxURI(wxT(string));\
171 uri->Resolve(masteruri, strict);\
172 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
175 #define URI_TEST(string, eq) \
176 URI_TEST_RESOLVE(string, eq, true);
179 //examples taken from RFC 2396.bis
181 void URITestCase::NormalResolving()
183 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
186 URI_TEST("g:h" ,"g:h")
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")
193 URI_TEST("g?y" ,"http://a/b/c/g?y")
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")
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")
212 void URITestCase::ComplexResolving()
214 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
226 //"../../../g" = "http://a/g"
227 //"../../../../g" = "http://a/g"
229 void URITestCase::ReallyComplexResolving()
231 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
234 //even more odder path examples
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")
243 void URITestCase::QueryFragmentResolving()
245 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
255 void URITestCase::BackwardsResolving()
257 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
261 URI_TEST("http:g" , "http:g") //strict
263 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
266 void URITestCase::Assignment()
268 wxURI
uri1(wxT("http://mysite.com")),
269 uri2(wxT("http://mysite2.com"));
273 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
276 void URITestCase::Comparison()
278 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
285 void URITestCase::URLCompat()
287 wxURL
url(wxT("http://user:password@wxwidgets.org"));
289 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
291 wxInputStream
* pInput
= url
.GetInputStream();
293 CPPUNIT_ASSERT( pInput
!= NULL
);
295 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
297 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
299 CPPUNIT_ASSERT( url
== uri
);
303 CPPUNIT_ASSERT( urlcopy
== url
);
304 CPPUNIT_ASSERT( urlcopy
== uri
);
308 CPPUNIT_ASSERT( uricopy
== url
);
309 CPPUNIT_ASSERT( uricopy
== urlcopy
);
310 CPPUNIT_ASSERT( uricopy
== uri
);
311 #if WXWIN_COMPATIBILITY_2_4
312 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
314 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
316 wxURI
test(wxT("file:\"myf\"ile.txt"));
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") );
323 #if wxUSE_PROTOCOL_HTTP
324 void URITestCase::URLProxy()
326 wxURL
url(wxT("http://www.asite.com/index.html"));
327 url
.SetProxy(wxT("pserv:3122"));
329 #endif // wxUSE_PROTOCOL_HTTP