1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/array.hpp"
21 #include "remote/url.hpp"
22 #include <BoostTestTargetConfig.h>
24 using namespace icinga;
26 BOOST_AUTO_TEST_SUITE(remote_url)
28 BOOST_AUTO_TEST_CASE(id_and_path)
30 Url::Ptr url = new Url("http://icinga.com/foo/bar/baz?hurr=durr");
32 BOOST_CHECK(url->GetScheme() == "http");
34 BOOST_CHECK(url->GetAuthority() == "icinga.com");
36 std::vector<String> PathCorrect;
37 PathCorrect.push_back("foo");
38 PathCorrect.push_back("bar");
39 PathCorrect.push_back("baz");
41 BOOST_CHECK(url->GetPath() == PathCorrect);
44 BOOST_AUTO_TEST_CASE(get_and_set)
46 Url::Ptr url = new Url();
47 url->SetScheme("ftp");
48 url->SetUsername("Horst");
49 url->SetPassword("Seehofer");
50 url->SetHost("koenigreich.bayern");
52 url->SetPath({ "path", "to", "münchen" });
54 BOOST_CHECK(url->Format(false, true) == "ftp://Horst:Seehofer@koenigreich.bayern:1918/path/to/m%C3%BCnchen");
56 std::map<String, std::vector<String> > m;
57 std::vector<String> v1 { "hip", "hip", "hurra" };
58 std::vector<String> v2 { "äü^ä+#ül-" };
59 std::vector<String> v3 { "1", "2" };
60 m.insert(std::make_pair("shout", v1));
61 m.insert(std::make_pair("sonderzeichen", v2));
63 url->SetQueryElements("count", v3);
64 url->AddQueryElement("count", "3");
66 std::map<String, std::vector<String> > mn = url->GetQuery();
67 BOOST_CHECK(mn["shout"][0] == v1[0]);
68 BOOST_CHECK(mn["sonderzeichen"][0] == v2[0]);
69 BOOST_CHECK(mn["count"][2] == "3");
72 BOOST_AUTO_TEST_CASE(parameters)
74 Url::Ptr url = new Url("https://icinga.com/hya/?rain=karl&rair=robert&foo[]=bar");
76 BOOST_CHECK(url->GetQueryElement("rair") == "robert");
77 BOOST_CHECK(url->GetQueryElement("rain") == "karl");
78 std::vector<String> test = url->GetQueryElements("foo");
79 BOOST_CHECK(test.size() == 1);
80 BOOST_CHECK(test[0] == "bar");
83 BOOST_AUTO_TEST_CASE(format)
85 Url::Ptr url = new Url("http://foo.bar/baz/?hop=top&flop=sop#iLIKEtrains");
87 BOOST_CHECK(url2 = new Url(url->Format(false, false)));
89 url = new Url("//main.args/////////?k[]=one&k[]=two#three");
90 BOOST_CHECK(url2 = new Url(url->Format(false, false)));
92 url = new Url("/foo/bar/index.php?blaka");
93 BOOST_CHECK(url2 = new Url(url->Format(false, false)));
94 BOOST_CHECK(url->Format(false, false) == "/foo/bar/index.php?blaka");
97 BOOST_CHECK(url->Format(false, false) == "/");
100 BOOST_AUTO_TEST_CASE(illegal_legal_strings)
103 BOOST_CHECK(url = new Url("/?foo=barr&foo[]=bazz"));
104 BOOST_CHECK_THROW(url = new Url("/?]=gar"), std::invalid_argument);
105 BOOST_CHECK_THROW(url = new Url("/#?[]"), std::invalid_argument);
106 BOOST_CHECK(url = new Url("/?foo=bar&foo=ba"));
107 BOOST_CHECK_THROW(url = new Url("/?foo=bar&[]=d"), std::invalid_argument);
108 BOOST_CHECK(url = new Url("/?fo=&bar=garOA"));
109 BOOST_CHECK(url = new Url("https://127.0.0.1:5665/demo?type=Service&filter=service.state%3E0"));
110 BOOST_CHECK(url = new Url("/?foo=baz??&\?\?=/?"));
111 BOOST_CHECK(url = new Url("/"));
112 BOOST_CHECK(url = new Url("///////"));
113 BOOST_CHECK(url = new Url("/??[]=?#?=?"));
114 BOOST_CHECK(url = new Url("http://foo/#bar"));
115 BOOST_CHECK(url = new Url("//foo/"));
118 BOOST_AUTO_TEST_SUITE_END()