]>
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) \
163 uri.Resolve(masteruri, strict); \
164 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
167 #define URI_TEST_RESOLVE(string, eq) \
168 URI_TEST_RESOLVE_IMPL(string, eq, true);
170 #define URI_TEST_RESOLVE_LAX(string, eq) \
171 URI_TEST_RESOLVE_IMPL(string, eq, false);
174 //examples taken from RFC 2396.bis
176 void URITestCase::NormalResolving()
178 wxURI
masteruri("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("http://a/b/c/d;p?q");
211 URI_TEST_RESOLVE("../../../g" , "http://a/g")
212 URI_TEST_RESOLVE("../../../../g", "http://a/g")
214 URI_TEST_RESOLVE("/./g" ,"http://a/g")
215 URI_TEST_RESOLVE("/../g" ,"http://a/g")
216 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
217 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
218 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
219 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
222 void URITestCase::ReallyComplexResolving()
224 wxURI
masteruri("http://a/b/c/d;p?q");
226 //even more odder path examples
227 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
228 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
229 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
230 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
231 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
232 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
235 void URITestCase::QueryFragmentResolving()
237 wxURI
masteruri("http://a/b/c/d;p?q");
239 //query/fragment ambigiousness
240 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
241 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
242 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
243 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
246 void URITestCase::BackwardsResolving()
248 wxURI
masteruri("http://a/b/c/d;p?q");
251 URI_TEST_RESOLVE("http:g" , "http:g") //strict
253 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
256 void URITestCase::Assignment()
258 wxURI
uri1("http://mysite.com"),
259 uri2("http://mysite2.com");
263 CPPUNIT_ASSERT_EQUAL(uri1
.BuildURI(), uri2
.BuildURI());
266 void URITestCase::Comparison()
268 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
271 void URITestCase::Unescaping()
276 escaped
= "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
277 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
279 unescaped
= wxURI(escaped
).BuildUnescapedURI();
281 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
282 "escaping\\that\\seems\\broken\\sadly[1].rss",
285 CPPUNIT_ASSERT_EQUAL( unescaped
, wxURI::Unescape(escaped
) );
288 escaped
= "http://ru.wikipedia.org/wiki/"
289 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
291 unescaped
= wxURI::Unescape(escaped
);
293 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
294 "http://ru.wikipedia.org/wiki/"
295 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
296 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
301 void URITestCase::FileScheme()
303 //file:// variety (NOT CONFORMANT TO THE RFC)
304 URI_TEST_EQUAL( "file://e:/wxcode/script1.xml",
305 "e:/wxcode/script1.xml", GetPath() );
308 URI_TEST_EQUAL( "file:///e:/wxcode/script1.xml",
309 "/e:/wxcode/script1.xml", GetPath() );
312 URI_TEST_EQUAL( "file:/e:/wxcode/script1.xml",
313 "/e:/wxcode/script1.xml", GetPath() );
316 URI_TEST_EQUAL( "file:e:/wxcode/script1.xml",
317 "e:/wxcode/script1.xml", GetPath() );
325 void URITestCase::URLCompat()
327 wxURL
url("http://user:password@wxwidgets.org");
329 CPPUNIT_ASSERT(url
.GetError() == wxURL_NOERR
);
332 wxInputStream
* pInput
= url
.GetInputStream();
334 CPPUNIT_ASSERT( pInput
!= NULL
);
337 CPPUNIT_ASSERT( url
== wxURL("http://user:password@wxwidgets.org") );
339 wxURI
uri("http://user:password@wxwidgets.org");
341 CPPUNIT_ASSERT( url
== uri
);
345 CPPUNIT_ASSERT( urlcopy
== url
);
346 CPPUNIT_ASSERT( urlcopy
== uri
);
350 CPPUNIT_ASSERT( uricopy
== url
);
351 CPPUNIT_ASSERT( uricopy
== urlcopy
);
352 CPPUNIT_ASSERT( uricopy
== uri
);
353 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
355 wxURI
test("file:\"myf\"ile.txt");
357 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test
.BuildURI() );
358 CPPUNIT_ASSERT_EQUAL( "file", test
.GetScheme() );
359 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test
.GetPath() );
361 // these could be put under a named registry since they take some
364 // Test problem urls (reported not to work some time ago by a user...)
365 const wxChar
* pszProblemUrls
[] = { "http://www.csdn.net",
366 "http://www.163.com",
367 "http://www.sina.com.cn" };
369 for ( size_t i
= 0; i
< WXSIZEOF(pszProblemUrls
); ++i
)
371 wxURL
urlProblem(pszProblemUrls
[i
]);
372 CPPUNIT_ASSERT(urlProblem
.GetError() == wxURL_NOERR
);
374 wxInputStream
* is
= urlProblem
.GetInputStream();
375 CPPUNIT_ASSERT(is
!= NULL
);
377 wxFile
fOut(_T("test.html"), wxFile::write
);
378 wxASSERT(fOut
.IsOpened());
384 size_t n
= is
->LastRead();
396 // the purpose of this test is unclear, it seems to be unfinished so disabling
398 #if 0 && wxUSE_PROTOCOL_HTTP
399 void URITestCase::URLProxy()
401 wxURL
url(wxT("http://www.asite.com/index.html"));
402 url
.SetProxy(wxT("pserv:3122"));
404 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
405 wxURL
url2(wxT("http://server-name/path/to/file?query_data=value"));
406 wxInputStream
*data
= url2
.GetInputStream();
407 CPPUNIT_ASSERT(data
!= NULL
);
409 #endif // wxUSE_PROTOCOL_HTTP