]>
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
);
51 CPPUNIT_TEST( Unescaping
);
53 CPPUNIT_TEST( URLCompat
);
54 #if wxUSE_PROTOCOL_HTTP
55 CPPUNIT_TEST( URLProxy
);
58 CPPUNIT_TEST_SUITE_END();
63 void NormalResolving();
64 void ComplexResolving();
65 void ReallyComplexResolving();
66 void QueryFragmentResolving();
67 void BackwardsResolving();
77 DECLARE_NO_COPY_CLASS(URITestCase
)
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
86 URITestCase::URITestCase()
91 #define URI_TEST(uristring, cond) \
92 uri = new wxURI(wxT(uristring));\
93 CPPUNIT_ASSERT(cond);\
96 #define URI_PRINT(uri)\
97 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
98 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
99 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
100 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
101 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
102 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
103 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
105 void URITestCase::IPv4()
110 URI_TEST("http://user:password@192.168.1.100:5050/path",
111 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
113 URI_TEST("http://user:password@192.255.1.100:5050/path",
114 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
117 URI_TEST("http://user:password@192.256.1.100:5050/path",
118 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
121 void URITestCase::IPv6()
125 // IPv6address = 6( h16 ":" ) ls32
126 // / "::" 5( h16 ":" ) ls32
127 // / [ h16 ] "::" 4( h16 ":" ) ls32
128 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
129 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
130 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
131 // / [ *4( h16 ":" ) h16 ] "::" ls32
132 // / [ *5( h16 ":" ) h16 ] "::" h16
133 // / [ *6( h16 ":" ) h16 ] "::"
134 // ls32 = ( h16 ":" h16 ) / IPv4address
136 URI_TEST("http://user:password@[aa:aa: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:aa:aa]:5050/path",
140 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
142 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
143 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
145 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
146 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
149 void URITestCase::Paths()
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("/"));
160 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
161 uri
->GetPath() == wxT("/path/"));
163 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
164 uri
->GetPath() == wxT("/path/"));
166 URI_TEST("path/john/../../../joe",
167 uri
->BuildURI() == wxT("../joe"));
171 #define URI_TEST_RESOLVE(string, eq, strict) \
172 uri = new wxURI(wxT(string));\
173 uri->Resolve(masteruri, strict);\
174 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
177 #define URI_TEST(string, eq) \
178 URI_TEST_RESOLVE(string, eq, true);
181 //examples taken from RFC 2396.bis
183 void URITestCase::NormalResolving()
185 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
188 URI_TEST("g:h" ,"g:h")
189 URI_TEST("g" ,"http://a/b/c/g")
190 URI_TEST("./g" ,"http://a/b/c/g")
191 URI_TEST("g/" ,"http://a/b/c/g/")
192 URI_TEST("/g" ,"http://a/g")
193 URI_TEST("//g" ,"http://g")
194 URI_TEST("?y" ,"http://a/b/c/d;p?y")
195 URI_TEST("g?y" ,"http://a/b/c/g?y")
196 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
197 URI_TEST("g#s" ,"http://a/b/c/g#s")
198 URI_TEST("g?y#s","http://a/b/c/g?y#s")
199 URI_TEST(";x" ,"http://a/b/c/;x")
200 URI_TEST("g;x" ,"http://a/b/c/g;x")
201 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
203 URI_TEST("" ,"http://a/b/c/d;p?q")
204 URI_TEST("." ,"http://a/b/c/")
205 URI_TEST("./" ,"http://a/b/c/")
206 URI_TEST(".." ,"http://a/b/")
207 URI_TEST("../" ,"http://a/b/")
208 URI_TEST("../g" ,"http://a/b/g")
209 URI_TEST("../..","http://a/")
210 URI_TEST("../../" , "http://a/")
211 URI_TEST("../../g" , "http://a/g")
214 void URITestCase::ComplexResolving()
216 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
220 URI_TEST("/./g" ,"http://a/g")
221 URI_TEST("/../g" ,"http://a/g")
222 URI_TEST("g." ,"http://a/b/c/g.")
223 URI_TEST(".g" ,"http://a/b/c/.g")
224 URI_TEST("g.." ,"http://a/b/c/g..")
225 URI_TEST("..g" ,"http://a/b/c/..g")
228 //"../../../g" = "http://a/g"
229 //"../../../../g" = "http://a/g"
231 void URITestCase::ReallyComplexResolving()
233 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
236 //even more odder path examples
237 URI_TEST("./../g" ,"http://a/b/g")
238 URI_TEST("./g/." ,"http://a/b/c/g/")
239 URI_TEST("g/./h" ,"http://a/b/c/g/h")
240 URI_TEST("g/../h" ,"http://a/b/c/h")
241 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
242 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
245 void URITestCase::QueryFragmentResolving()
247 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
250 //query/fragment ambigiousness
251 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
252 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
253 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
254 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
257 void URITestCase::BackwardsResolving()
259 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
263 URI_TEST("http:g" , "http:g") //strict
265 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
268 void URITestCase::Assignment()
270 wxURI
uri1(wxT("http://mysite.com")),
271 uri2(wxT("http://mysite2.com"));
275 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
278 void URITestCase::Comparison()
280 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
283 void URITestCase::Unescaping()
285 wxString orig
= wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
286 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
288 wxString works
= wxURI(orig
).BuildUnescapedURI();
290 CPPUNIT_ASSERT(orig
.IsSameAs(works
) == false);
292 wxString orig2
= wxT("http://test.com/of/file%3A%2F%")
293 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
294 wxT("5Cbroken%5Csadly%5B1%5D.rss");
296 wxString works2
= wxURI::Unescape(orig2
);
297 wxString broken2
= wxURI(orig2
).BuildUnescapedURI();
299 CPPUNIT_ASSERT(works2
.IsSameAs(broken2
));
304 const wxChar
* pszProblemUrls
[] = { wxT("http://www.csdn.net"),
305 wxT("http://www.163.com"),
306 wxT("http://www.sina.com.cn") };
311 void URITestCase::URLCompat()
313 wxURL
url(wxT("http://user:password@wxwidgets.org"));
315 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
317 wxInputStream
* pInput
= url
.GetInputStream();
319 CPPUNIT_ASSERT( pInput
!= NULL
);
321 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
323 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
325 CPPUNIT_ASSERT( url
== uri
);
329 CPPUNIT_ASSERT( urlcopy
== url
);
330 CPPUNIT_ASSERT( urlcopy
== uri
);
334 CPPUNIT_ASSERT( uricopy
== url
);
335 CPPUNIT_ASSERT( uricopy
== urlcopy
);
336 CPPUNIT_ASSERT( uricopy
== uri
);
337 #if WXWIN_COMPATIBILITY_2_4
338 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
340 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
342 wxURI
test(wxT("file:\"myf\"ile.txt"));
344 CPPUNIT_ASSERT( test
.BuildURI() == wxT("file:%22myf%22ile.txt") );
345 CPPUNIT_ASSERT( test
.GetScheme() == wxT("file") );
346 CPPUNIT_ASSERT( test
.GetPath() == wxT("%22myf%22ile.txt") );
348 // Test problem urls (reported not to work some time ago by a user...)
349 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
351 wxURL
urlProblem(pszProblemUrls
[i
]);
352 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
354 wxInputStream
* is
= urlProblem
.GetInputStream();
355 CPPUNIT_ASSERT(is
!= NULL
);
357 wxFile
fOut(_T("test.html"), wxFile::write
);
358 wxASSERT(fOut
.IsOpened());
364 size_t n
= is
->LastRead();
375 #if wxUSE_PROTOCOL_HTTP
376 void URITestCase::URLProxy()
378 wxURL
url(wxT("http://www.asite.com/index.html"));
379 url
.SetProxy(wxT("pserv:3122"));
381 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
382 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
383 wxInputStream
*data
= url2
.GetInputStream();
384 CPPUNIT_ASSERT(data
!= NULL
);
386 #endif // wxUSE_PROTOCOL_HTTP