]>
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 wxUSE_URL
30 // Define this as 1 to test network connections, this is disabled by default as
31 // some machines running automatic builds don't allow outgoing connections and
33 #define TEST_NETWORK 0
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class URITestCase
: public CppUnit::TestCase
45 CPPUNIT_TEST_SUITE( URITestCase
);
48 CPPUNIT_TEST( Paths
);
49 CPPUNIT_TEST( NormalResolving
);
50 CPPUNIT_TEST( ComplexResolving
);
51 CPPUNIT_TEST( ReallyComplexResolving
);
52 CPPUNIT_TEST( QueryFragmentResolving
);
53 CPPUNIT_TEST( BackwardsResolving
);
54 CPPUNIT_TEST( Assignment
);
55 CPPUNIT_TEST( Comparison
);
56 CPPUNIT_TEST( Unescaping
);
57 CPPUNIT_TEST( FileScheme
);
59 CPPUNIT_TEST( URLCompat
);
60 #if 0 && wxUSE_PROTOCOL_HTTP
61 CPPUNIT_TEST( URLProxy
);
64 CPPUNIT_TEST_SUITE_END();
69 void NormalResolving();
70 void ComplexResolving();
71 void ReallyComplexResolving();
72 void QueryFragmentResolving();
73 void BackwardsResolving();
81 #if 0 && wxUSE_PROTOCOL_HTTP
86 DECLARE_NO_COPY_CLASS(URITestCase
)
89 // register in the unnamed registry so that these tests are run by default
90 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
92 // also include in it's own registry so that these tests can be run alone
93 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
95 URITestCase::URITestCase()
99 // apply the given accessor to the URI, check that the result is as expected
100 #define URI_TEST_EQUAL(uri, expected, accessor) \
101 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
103 void URITestCase::IPv4()
105 URI_TEST_EQUAL("http://user:password@192.168.1.100:5050/path",
106 wxURI_IPV4ADDRESS
, GetHostType());
108 URI_TEST_EQUAL("http://user:password@192.255.1.100:5050/path",
109 wxURI_IPV4ADDRESS
, GetHostType());
112 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
113 GetHostType() != wxURI_IPV4ADDRESS
);
116 void URITestCase::IPv6()
118 // IPv6address = 6( h16 ":" ) ls32
119 // / "::" 5( h16 ":" ) ls32
120 // / [ h16 ] "::" 4( h16 ":" ) ls32
121 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
122 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
123 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
124 // / [ *4( h16 ":" ) h16 ] "::" ls32
125 // / [ *5( h16 ":" ) h16 ] "::" h16
126 // / [ *6( h16 ":" ) h16 ] "::"
127 // ls32 = ( h16 ":" h16 ) / IPv4address
129 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
130 wxURI_IPV6ADDRESS
, GetHostType());
132 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
133 wxURI_IPV6ADDRESS
, GetHostType());
135 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
136 wxURI_IPV6ADDRESS
, GetHostType());
138 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
139 wxURI_IPV6ADDRESS
, GetHostType());
142 void URITestCase::Paths()
144 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/../path",
147 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/../",
150 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/.",
151 "/path/", GetPath());
153 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/./",
154 "/path/", GetPath());
156 URI_TEST_EQUAL("path/john/../../../joe",
157 "../joe", BuildURI());
160 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
161 uri = new wxURI(wxT(string));\
162 uri->Resolve(masteruri, strict);\
163 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
166 #define URI_TEST_RESOLVE(string, eq) \
167 URI_TEST_RESOLVE_IMPL(string, eq, true);
169 #define URI_TEST_RESOLVE_LAX(string, eq) \
170 URI_TEST_RESOLVE_IMPL(string, eq, false);
173 //examples taken from RFC 2396.bis
175 void URITestCase::NormalResolving()
177 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
180 URI_TEST_RESOLVE("g:h" ,"g:h")
181 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
182 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
183 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
184 URI_TEST_RESOLVE("/g" ,"http://a/g")
185 URI_TEST_RESOLVE("//g" ,"http://g")
186 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
187 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
188 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
189 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
190 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
191 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
192 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
193 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
195 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
196 URI_TEST_RESOLVE("." ,"http://a/b/c/")
197 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
198 URI_TEST_RESOLVE(".." ,"http://a/b/")
199 URI_TEST_RESOLVE("../" ,"http://a/b/")
200 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
201 URI_TEST_RESOLVE("../..","http://a/")
202 URI_TEST_RESOLVE("../../" , "http://a/")
203 URI_TEST_RESOLVE("../../g" , "http://a/g")
206 void URITestCase::ComplexResolving()
208 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
212 URI_TEST_RESOLVE("/./g" ,"http://a/g")
213 URI_TEST_RESOLVE("/../g" ,"http://a/g")
214 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
215 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
216 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
217 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
220 //"../../../g" = "http://a/g"
221 //"../../../../g" = "http://a/g"
223 void URITestCase::ReallyComplexResolving()
225 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
228 //even more odder path examples
229 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
230 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
231 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
232 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
233 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
234 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
237 void URITestCase::QueryFragmentResolving()
239 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
242 //query/fragment ambigiousness
243 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
244 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
245 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
246 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
249 void URITestCase::BackwardsResolving()
251 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
255 URI_TEST_RESOLVE("http:g" , "http:g") //strict
257 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
260 void URITestCase::Assignment()
262 wxURI
uri1(wxT("http://mysite.com")),
263 uri2(wxT("http://mysite2.com"));
267 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
270 void URITestCase::Comparison()
272 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
275 void URITestCase::Unescaping()
277 wxString orig
= wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
278 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
280 wxString works
= wxURI(orig
).BuildUnescapedURI();
282 CPPUNIT_ASSERT(orig
.IsSameAs(works
) == false);
284 wxString orig2
= wxT("http://test.com/of/file%3A%2F%")
285 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
286 wxT("5Cbroken%5Csadly%5B1%5D.rss");
288 wxString works2
= wxURI::Unescape(orig2
);
289 wxString broken2
= wxURI(orig2
).BuildUnescapedURI();
291 CPPUNIT_ASSERT(works2
.IsSameAs(broken2
));
295 void URITestCase::FileScheme()
297 //file:// variety (NOT CONFORMANT TO THE RFC)
298 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
299 == wxT("e:/wxcode/script1.xml") );
302 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
303 == wxT("/e:/wxcode/script1.xml") );
306 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
307 == wxT("/e:/wxcode/script1.xml") );
310 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
311 == wxT("e:/wxcode/script1.xml") );
316 const wxChar
* pszProblemUrls
[] = { wxT("http://www.csdn.net"),
317 wxT("http://www.163.com"),
318 wxT("http://www.sina.com.cn") };
323 void URITestCase::URLCompat()
325 wxURL
url(wxT("http://user:password@wxwidgets.org"));
327 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
330 wxInputStream
* pInput
= url
.GetInputStream();
332 CPPUNIT_ASSERT( pInput
!= NULL
);
335 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
337 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
339 CPPUNIT_ASSERT( url
== uri
);
343 CPPUNIT_ASSERT( urlcopy
== url
);
344 CPPUNIT_ASSERT( urlcopy
== uri
);
348 CPPUNIT_ASSERT( uricopy
== url
);
349 CPPUNIT_ASSERT( uricopy
== urlcopy
);
350 CPPUNIT_ASSERT( uricopy
== uri
);
351 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
353 wxURI
test(wxT("file:\"myf\"ile.txt"));
355 CPPUNIT_ASSERT( test
.BuildURI() == wxT("file:%22myf%22ile.txt") );
356 CPPUNIT_ASSERT( test
.GetScheme() == wxT("file") );
357 CPPUNIT_ASSERT( test
.GetPath() == wxT("%22myf%22ile.txt") );
359 // these could be put under a named registry since they take some
362 // Test problem urls (reported not to work some time ago by a user...)
363 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
365 wxURL
urlProblem(pszProblemUrls
[i
]);
366 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
368 wxInputStream
* is
= urlProblem
.GetInputStream();
369 CPPUNIT_ASSERT(is
!= NULL
);
371 wxFile
fOut(_T("test.html"), wxFile::write
);
372 wxASSERT(fOut
.IsOpened());
378 size_t n
= is
->LastRead();
390 // the purpose of this test is unclear, it seems to be unfinished so disabling
392 #if 0 && wxUSE_PROTOCOL_HTTP
393 void URITestCase::URLProxy()
395 wxURL
url(wxT("http://www.asite.com/index.html"));
396 url
.SetProxy(wxT("pserv:3122"));
398 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
399 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
400 wxInputStream
*data
= url2
.GetInputStream();
401 CPPUNIT_ASSERT(data
!= NULL
);
403 #endif // wxUSE_PROTOCOL_HTTP