]>
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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
26 #include "wx/cppunit.h"
28 // Test wxURL & wxURI compat?
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class URITestCase
: public CppUnit::TestCase
41 CPPUNIT_TEST_SUITE( URITestCase
);
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
);
53 CPPUNIT_TEST( URLCompat
);
55 CPPUNIT_TEST_SUITE_END();
60 void NormalResolving();
61 void ComplexResolving();
62 void ReallyComplexResolving();
63 void QueryFragmentResolving();
64 void BackwardsResolving();
72 DECLARE_NO_COPY_CLASS(URITestCase
)
75 // register in the unnamed registry so that these tests are run by default
76 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
78 // also include in it's own registry so that these tests can be run alone
79 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
81 URITestCase::URITestCase()
86 #define URI_TEST(uristring, cond) \
87 uri = new wxURI(wxT(uristring));\
88 CPPUNIT_ASSERT(cond);\
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());
100 void URITestCase::IPv4()
105 URI_TEST("http://user:password@192.168.1.100:5050/path",
106 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
108 URI_TEST("http://user:password@192.255.1.100:5050/path",
109 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
112 URI_TEST("http://user:password@192.256.1.100:5050/path",
113 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
116 void URITestCase::IPv6()
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
131 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
132 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
134 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
135 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
137 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
138 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
140 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
141 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
144 void URITestCase::Paths()
149 URI_TEST("http://user:password@192.256.1.100:5050/../path",
150 uri
->GetPath() == wxT("/path"));
152 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
153 uri
->GetPath() == wxT("/"));
155 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
156 uri
->GetPath() == wxT("/path/"));
158 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
159 uri
->GetPath() == wxT("/path/"));
161 URI_TEST("path/john/../../../joe",
162 uri
->BuildURI() == wxT("../joe"));
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));\
172 #define URI_TEST(string, eq) \
173 URI_TEST_RESOLVE(string, eq, true);
176 //examples taken from RFC 2396.bis
178 void URITestCase::NormalResolving()
180 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
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")
209 void URITestCase::ComplexResolving()
211 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
223 //"../../../g" = "http://a/g"
224 //"../../../../g" = "http://a/g"
226 void URITestCase::ReallyComplexResolving()
228 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
240 void URITestCase::QueryFragmentResolving()
242 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
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")
252 void URITestCase::BackwardsResolving()
254 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
258 URI_TEST("http:g" , "http:g") //strict
260 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
263 void URITestCase::Assignment()
265 wxURI
uri1(wxT("http://mysite.com")),
266 uri2(wxT("http://mysite2.com"));
270 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
273 void URITestCase::Comparison()
275 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
282 void URITestCase::URLCompat()
284 wxURL
url(wxT("http://user:password@wxwidgets.org"));
286 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
288 wxInputStream
* pInput
= url
.GetInputStream();
290 CPPUNIT_ASSERT( pInput
!= NULL
);
292 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
294 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
296 CPPUNIT_ASSERT( url
== uri
);
300 CPPUNIT_ASSERT( urlcopy
== url
);
301 CPPUNIT_ASSERT( urlcopy
== uri
);
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 ") );