2 ** Check if the url by scheme repository works, e.g.
3 ** if there are some initialization order problems
4 ** (ViewOption) causing asString to format its string
5 ** differently than configured.
8 #include "zypp/base/Exception.h"
15 #include <boost/test/auto_unit_test.hpp>
17 using boost::unit_test::test_case;
20 BOOST_AUTO_TEST_CASE(test_url1)
22 std::string str, one, two;
26 // asString & asCompleteString should not print "mailto://"
27 str = "mailto:feedback@example.com?subject=hello";
29 BOOST_CHECK_EQUAL( str, url.asString() );
30 BOOST_CHECK_EQUAL( str, url.asCompleteString() );
32 // asString & asCompleteString should add empty authority
33 // "dvd://...", except we request to avoid it.
35 one = "dvd:///srv/ftp";
36 two = "dvd:///srv/ftp";
39 BOOST_CHECK_EQUAL( one, url.asString() );
40 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
41 BOOST_CHECK_EQUAL( str, url.asString(zypp::url::ViewOptions() -
42 zypp::url::ViewOption::EMPTY_AUTHORITY));
44 // asString shouldn't print the password, asCompleteString should
45 // further, the "//" at the begin of the path should become "/%2F"
46 str = "ftp://user:pass@localhost//srv/ftp";
47 one = "ftp://user@localhost/%2Fsrv/ftp";
48 two = "ftp://user:pass@localhost/%2Fsrv/ftp";
51 BOOST_CHECK_EQUAL( one, url.asString() );
52 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
54 // asString shouldn't print the password, asCompleteString should.
55 // further, the "//" at the begin of the path should be keept.
56 str = "http://user:pass@localhost//srv/ftp";
57 one = "http://user@localhost//srv/ftp";
61 BOOST_CHECK_EQUAL( one, url.asString() );
62 BOOST_CHECK_EQUAL( two, url.asCompleteString() );
64 str = "file:./srv/ftp";
65 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), str );
67 str = "ftp://foo//srv/ftp";
68 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://foo/%2Fsrv/ftp" );
70 str = "FTP://user@local%68ost/%2f/srv/ftp";
71 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "ftp://user@localhost/%2f/srv/ftp" );
73 str = "http://[::1]/foo/bar";
74 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
76 str = "http://:@just-localhost.example.net:8080/";
77 BOOST_CHECK_EQUAL( zypp::Url(str).asString(), "http://just-localhost.example.net:8080/" );
79 str = "mailto:feedback@example.com?subject=hello";
80 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
82 str = "nfs://nfs-server/foo/bar/trala";
83 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
85 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)#x";
86 BOOST_CHECK_THROW( zypp::Url(str).asString(), url::UrlNotAllowedException );
88 str = "ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)";
89 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString() );
91 // parseable but invalid, since no host avaliable
92 str = "ldap:///dc=foo,dc=bar";
93 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
94 BOOST_CHECK( !zypp::Url(str).isValid());
96 // throws: host is mandatory
97 str = "ftp:///foo/bar";
98 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
100 // throws: host is mandatory
101 str = "http:///%2f/srv/ftp";
102 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
104 // OK, host allowed in file-url
105 str = "file://localhost/some/path";
106 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
108 // throws: host not allowed
109 str = "cd://localhost/some/path";
110 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
112 // throws: no path (email)
114 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
118 BOOST_CHECK_THROW(zypp::Url(str).asString(), url::UrlNotAllowedException );
120 // OK, valid (no host, path is there)
121 str = "cd:///some/path";
122 BOOST_CHECK_EQUAL( str, zypp::Url(str).asString());
123 BOOST_CHECK( zypp::Url(str).isValid());
126 BOOST_AUTO_TEST_CASE(test_url2)
128 zypp::Url url("http://user:pass@localhost:/path/to;version=1.1?arg=val#frag");
130 BOOST_CHECK_EQUAL( url.asString(),
131 "http://user@localhost/path/to?arg=val#frag" );
133 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
134 zypp::url::ViewOptions::WITH_PASSWORD),
135 "http://user:pass@localhost/path/to?arg=val#frag");
137 BOOST_CHECK_EQUAL( url.asString(zypp::url::ViewOptions() +
138 zypp::url::ViewOptions::WITH_PATH_PARAMS),
139 "http://user@localhost/path/to;version=1.1?arg=val#frag");
141 BOOST_CHECK_EQUAL( url.asCompleteString(),
142 "http://user:pass@localhost/path/to;version=1.1?arg=val#frag");
145 BOOST_AUTO_TEST_CASE(test_url3)
147 zypp::Url url("http://localhost/path/to#frag");
151 // will be encoded as "hoho=ha%20ha"
154 url.setQueryParam(key, val);
155 BOOST_CHECK_EQUAL( url.asString(),
156 "http://localhost/path/to?hoho=ha%20ha#frag");
158 // will be encoded as "foo%3Dbar%26key=foo%26bar%3Dvalue"
160 val = "foo&bar=value";
161 url.setQueryParam(key, val);
162 BOOST_CHECK_EQUAL( url.asString(),
163 "http://localhost/path/to?foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
165 // will be encoded as "foo%25bar=is%25de%25ad"
168 url.setQueryParam(key, val);
169 BOOST_CHECK_EQUAL( url.asString(),
170 "http://localhost/path/to?foo%25bar=is%25de%25ad&foo%3Dbar%26key=foo%26bar%3Dvalue&hoho=ha%20ha#frag");
172 // get encoded query parameters and compare with results:
173 zypp::url::ParamVec params( url.getQueryStringVec());
174 const char * const result[] = {
175 "foo%25bar=is%25de%25ad",
176 "foo%3Dbar%26key=foo%26bar%3Dvalue",
179 BOOST_CHECK( params.size() == (sizeof(result)/sizeof(result[0])));
180 for( size_t i=0; i<params.size(); i++)
182 BOOST_CHECK_EQUAL( params[i], result[i]);
186 BOOST_AUTO_TEST_CASE( test_url4)
190 zypp::Url url("ldap://example.net/dc=example,dc=net?cn,sn?sub?(cn=*)");
192 // fetch query params as vector
193 zypp::url::ParamVec pvec( url.getQueryStringVec());
194 BOOST_CHECK( pvec.size() == 3);
195 BOOST_CHECK_EQUAL( pvec[0], "cn,sn");
196 BOOST_CHECK_EQUAL( pvec[1], "sub");
197 BOOST_CHECK_EQUAL( pvec[2], "(cn=*)");
199 // fetch the query params map
200 // with its special ldap names/keys
201 zypp::url::ParamMap pmap( url.getQueryStringMap());
202 zypp::url::ParamMap::const_iterator m;
203 for(m=pmap.begin(); m!=pmap.end(); ++m)
205 if("attrs" == m->first)
207 BOOST_CHECK_EQUAL( m->second, "cn,sn");
210 if("filter" == m->first)
212 BOOST_CHECK_EQUAL( m->second, "(cn=*)");
215 if("scope" == m->first)
217 BOOST_CHECK_EQUAL( m->second, "sub");
221 BOOST_FAIL("Unexpected LDAP query parameter name in the map!");
225 url.setQueryParam("attrs", "cn,sn,uid");
226 url.setQueryParam("filter", "(|(sn=foo)(cn=bar))");
228 BOOST_CHECK_EQUAL(url.getQueryParam("attrs"), "cn,sn,uid");
229 BOOST_CHECK_EQUAL(url.getQueryParam("filter"), "(|(sn=foo)(cn=bar))");
232 catch(const zypp::url::UrlException &e)
239 // vim: set ts=2 sts=2 sw=2 ai et: