]>
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 ( 0 && 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
);
54 CPPUNIT_TEST_SUITE_END();
59 void NormalResolving();
60 void ComplexResolving();
61 void ReallyComplexResolving();
62 void QueryFragmentResolving();
63 void BackwardsResolving();
71 DECLARE_NO_COPY_CLASS(URITestCase
)
74 // register in the unnamed registry so that these tests are run by default
75 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
77 // also include in it's own registry so that these tests can be run alone
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
80 URITestCase::URITestCase()
85 #define URI_TEST(uristring, cond) \
86 uri = new wxURI(wxT(uristring));\
87 CPPUNIT_ASSERT(cond);\
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());
99 void URITestCase::IPv4()
104 URI_TEST("http://user:password@192.168.1.100:5050/path",
105 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
107 URI_TEST("http://user:password@192.255.1.100:5050/path",
108 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
111 URI_TEST("http://user:password@192.256.1.100:5050/path",
112 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
115 void URITestCase::IPv6()
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
130 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
131 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
133 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
134 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
136 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
137 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
139 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
140 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
143 void URITestCase::Paths()
148 URI_TEST("http://user:password@192.256.1.100:5050/../path",
149 uri
->GetPath() == wxT("/path"));
151 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
152 uri
->GetPath() == wxT("/"));
154 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
155 uri
->GetPath() == wxT("/path/"));
157 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
158 uri
->GetPath() == wxT("/path/"));
160 URI_TEST("path/john/../../../joe",
161 uri
->BuildURI() == wxT("../joe"));
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));\
171 #define URI_TEST(string, eq) \
172 URI_TEST_RESOLVE(string, eq, true);
175 //examples taken from RFC 2396.bis
177 void URITestCase::NormalResolving()
179 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
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")
208 void URITestCase::ComplexResolving()
210 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
222 //"../../../g" = "http://a/g"
223 //"../../../../g" = "http://a/g"
225 void URITestCase::ReallyComplexResolving()
227 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
239 void URITestCase::QueryFragmentResolving()
241 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
251 void URITestCase::BackwardsResolving()
253 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
257 URI_TEST("http:g" , "http:g") //strict
259 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
262 void URITestCase::Assignment()
264 wxURI
uri1(wxT("http://mysite.com")),
265 uri2(wxT("http://mysite2.com"));
269 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
272 void URITestCase::Comparison()
274 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
281 void URITestCase::URLCompat()
283 wxURL
url(wxT("http://user:password@wxwidgets.org"));
285 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
287 wxInputStream
* pInput
= url
.GetInputStream();
289 CPPUNIT_ASSERT( pInput
!= NULL
);
291 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
293 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
295 CPPUNIT_ASSERT( url
== uri
);
299 CPPUNIT_ASSERT( urlcopy
== url
);
300 CPPUNIT_ASSERT( urlcopy
== uri
);
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 ") );
310 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
312 wxURI
test(wxT("file:\"myf\"ile.txt"));
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") );