]>
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
);
52 CPPUNIT_TEST( FileScheme
);
54 CPPUNIT_TEST( URLCompat
);
55 #if wxUSE_PROTOCOL_HTTP
56 CPPUNIT_TEST( URLProxy
);
59 CPPUNIT_TEST_SUITE_END();
64 void NormalResolving();
65 void ComplexResolving();
66 void ReallyComplexResolving();
67 void QueryFragmentResolving();
68 void BackwardsResolving();
79 DECLARE_NO_COPY_CLASS(URITestCase
)
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase
);
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase
, "URITestCase" );
88 URITestCase::URITestCase()
93 #define URI_TEST(uristring, cond) \
94 uri = new wxURI(wxT(uristring));\
95 CPPUNIT_ASSERT(cond);\
98 #define URI_PRINT(uri)\
99 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
100 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
101 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
102 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
103 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
104 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
105 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
107 void URITestCase::IPv4()
112 URI_TEST("http://user:password@192.168.1.100:5050/path",
113 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
115 URI_TEST("http://user:password@192.255.1.100:5050/path",
116 uri
->GetHostType() == wxURI_IPV4ADDRESS
);
119 URI_TEST("http://user:password@192.256.1.100:5050/path",
120 uri
->GetHostType() != wxURI_IPV4ADDRESS
);
123 void URITestCase::IPv6()
127 // IPv6address = 6( h16 ":" ) ls32
128 // / "::" 5( h16 ":" ) ls32
129 // / [ h16 ] "::" 4( h16 ":" ) ls32
130 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
131 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
132 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
133 // / [ *4( h16 ":" ) h16 ] "::" ls32
134 // / [ *5( h16 ":" ) h16 ] "::" h16
135 // / [ *6( h16 ":" ) h16 ] "::"
136 // ls32 = ( h16 ":" h16 ) / IPv4address
138 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
139 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
141 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
142 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
144 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
145 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
147 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
148 uri
->GetHostType() == wxURI_IPV6ADDRESS
);
151 void URITestCase::Paths()
156 URI_TEST("http://user:password@192.256.1.100:5050/../path",
157 uri
->GetPath() == wxT("/path"));
159 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
160 uri
->GetPath() == wxT("/"));
162 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
163 uri
->GetPath() == wxT("/path/"));
165 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
166 uri
->GetPath() == wxT("/path/"));
168 URI_TEST("path/john/../../../joe",
169 uri
->BuildURI() == wxT("../joe"));
173 #define URI_TEST_RESOLVE(string, eq, strict) \
174 uri = new wxURI(wxT(string));\
175 uri->Resolve(masteruri, strict);\
176 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
179 #define URI_TEST(string, eq) \
180 URI_TEST_RESOLVE(string, eq, true);
183 //examples taken from RFC 2396.bis
185 void URITestCase::NormalResolving()
187 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
190 URI_TEST("g:h" ,"g:h")
191 URI_TEST("g" ,"http://a/b/c/g")
192 URI_TEST("./g" ,"http://a/b/c/g")
193 URI_TEST("g/" ,"http://a/b/c/g/")
194 URI_TEST("/g" ,"http://a/g")
195 URI_TEST("//g" ,"http://g")
196 URI_TEST("?y" ,"http://a/b/c/d;p?y")
197 URI_TEST("g?y" ,"http://a/b/c/g?y")
198 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
199 URI_TEST("g#s" ,"http://a/b/c/g#s")
200 URI_TEST("g?y#s","http://a/b/c/g?y#s")
201 URI_TEST(";x" ,"http://a/b/c/;x")
202 URI_TEST("g;x" ,"http://a/b/c/g;x")
203 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
205 URI_TEST("" ,"http://a/b/c/d;p?q")
206 URI_TEST("." ,"http://a/b/c/")
207 URI_TEST("./" ,"http://a/b/c/")
208 URI_TEST(".." ,"http://a/b/")
209 URI_TEST("../" ,"http://a/b/")
210 URI_TEST("../g" ,"http://a/b/g")
211 URI_TEST("../..","http://a/")
212 URI_TEST("../../" , "http://a/")
213 URI_TEST("../../g" , "http://a/g")
216 void URITestCase::ComplexResolving()
218 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
222 URI_TEST("/./g" ,"http://a/g")
223 URI_TEST("/../g" ,"http://a/g")
224 URI_TEST("g." ,"http://a/b/c/g.")
225 URI_TEST(".g" ,"http://a/b/c/.g")
226 URI_TEST("g.." ,"http://a/b/c/g..")
227 URI_TEST("..g" ,"http://a/b/c/..g")
230 //"../../../g" = "http://a/g"
231 //"../../../../g" = "http://a/g"
233 void URITestCase::ReallyComplexResolving()
235 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
238 //even more odder path examples
239 URI_TEST("./../g" ,"http://a/b/g")
240 URI_TEST("./g/." ,"http://a/b/c/g/")
241 URI_TEST("g/./h" ,"http://a/b/c/g/h")
242 URI_TEST("g/../h" ,"http://a/b/c/h")
243 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
244 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
247 void URITestCase::QueryFragmentResolving()
249 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
252 //query/fragment ambigiousness
253 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
254 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
255 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
256 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
259 void URITestCase::BackwardsResolving()
261 wxURI
masteruri(wxT("http://a/b/c/d;p?q"));
265 URI_TEST("http:g" , "http:g") //strict
267 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
270 void URITestCase::Assignment()
272 wxURI
uri1(wxT("http://mysite.com")),
273 uri2(wxT("http://mysite2.com"));
277 CPPUNIT_ASSERT(uri1
.BuildURI() == uri2
.BuildURI());
280 void URITestCase::Comparison()
282 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
285 void URITestCase::Unescaping()
287 wxString orig
= wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
288 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
290 wxString works
= wxURI(orig
).BuildUnescapedURI();
292 CPPUNIT_ASSERT(orig
.IsSameAs(works
) == false);
294 wxString orig2
= wxT("http://test.com/of/file%3A%2F%")
295 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
296 wxT("5Cbroken%5Csadly%5B1%5D.rss");
298 wxString works2
= wxURI::Unescape(orig2
);
299 wxString broken2
= wxURI(orig2
).BuildUnescapedURI();
301 CPPUNIT_ASSERT(works2
.IsSameAs(broken2
));
305 void URITestCase::FileScheme()
307 //file:// variety (NOT CONFORMANT TO THE RFC)
308 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
309 == wxT("e:/wxcode/script1.xml") );
312 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
313 == wxT("/e:/wxcode/script1.xml") );
316 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
317 == wxT("/e:/wxcode/script1.xml") );
320 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
321 == wxT("e:/wxcode/script1.xml") );
326 const wxChar
* pszProblemUrls
[] = { wxT("http://www.csdn.net"),
327 wxT("http://www.163.com"),
328 wxT("http://www.sina.com.cn") };
333 void URITestCase::URLCompat()
335 wxURL
url(wxT("http://user:password@wxwidgets.org"));
337 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
339 wxInputStream
* pInput
= url
.GetInputStream();
341 CPPUNIT_ASSERT( pInput
!= NULL
);
343 CPPUNIT_ASSERT( url
== wxURL(wxT("http://user:password@wxwidgets.org")) );
345 wxURI
uri(wxT("http://user:password@wxwidgets.org"));
347 CPPUNIT_ASSERT( url
== uri
);
351 CPPUNIT_ASSERT( urlcopy
== url
);
352 CPPUNIT_ASSERT( urlcopy
== uri
);
356 CPPUNIT_ASSERT( uricopy
== url
);
357 CPPUNIT_ASSERT( uricopy
== urlcopy
);
358 CPPUNIT_ASSERT( uricopy
== uri
);
359 #if WXWIN_COMPATIBILITY_2_4
360 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
362 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
364 wxURI
test(wxT("file:\"myf\"ile.txt"));
366 CPPUNIT_ASSERT( test
.BuildURI() == wxT("file:%22myf%22ile.txt") );
367 CPPUNIT_ASSERT( test
.GetScheme() == wxT("file") );
368 CPPUNIT_ASSERT( test
.GetPath() == wxT("%22myf%22ile.txt") );
370 // these could be put under a named registry since they take some
373 // Test problem urls (reported not to work some time ago by a user...)
374 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
376 wxURL
urlProblem(pszProblemUrls
[i
]);
377 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
379 wxInputStream
* is
= urlProblem
.GetInputStream();
380 CPPUNIT_ASSERT(is
!= NULL
);
382 wxFile
fOut(_T("test.html"), wxFile::write
);
383 wxASSERT(fOut
.IsOpened());
389 size_t n
= is
->LastRead();
401 #if wxUSE_PROTOCOL_HTTP
402 void URITestCase::URLProxy()
404 wxURL
url(wxT("http://www.asite.com/index.html"));
405 url
.SetProxy(wxT("pserv:3122"));
407 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
408 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
409 wxInputStream
*data
= url2
.GetInputStream();
410 CPPUNIT_ASSERT(data
!= NULL
);
412 #endif // wxUSE_PROTOCOL_HTTP