]> granicus.if.org Git - icinga2/blob - test/remote-url.cpp
Update copyright headers for 2016
[icinga2] / test / remote-url.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "base/array.hpp"
21 #include "remote/url.hpp"
22 #include <boost/test/unit_test.hpp>
23 #include <boost/foreach.hpp>
24 #include <boost/assign/list_of.hpp>
25
26 using namespace icinga;
27
28 BOOST_AUTO_TEST_SUITE(remote_url)
29
30 BOOST_AUTO_TEST_CASE(id_and_path)
31 {
32         Url::Ptr url = new Url("http://icinga.org/foo/bar/baz?hurr=durr");
33
34         BOOST_CHECK(url->GetScheme() == "http");
35
36         BOOST_CHECK(url->GetAuthority() == "icinga.org");
37
38         std::vector<String> PathCorrect;
39         PathCorrect.push_back("foo");
40         PathCorrect.push_back("bar");
41         PathCorrect.push_back("baz");
42
43         BOOST_CHECK(url->GetPath() == PathCorrect);
44 }
45
46 BOOST_AUTO_TEST_CASE(get_and_set)
47 {
48         Url::Ptr url = new Url();
49         url->SetScheme("ftp");
50         url->SetUsername("Horst");
51         url->SetPassword("Seehofer");
52         url->SetHost("koenigreich.bayern");
53         url->SetPort("1918");
54         std::vector<String> p = boost::assign::list_of("path")("to")("münchen");
55         url->SetPath(p);
56         BOOST_CHECK(url->Format(true) == "ftp://Horst:Seehofer@koenigreich.bayern:1918/path/to/m%C3%BCnchen");
57
58         std::map<String, std::vector<String> > m;
59         std::vector<String> v1 = boost::assign::list_of("hip")("hip")("hurra");
60         std::vector<String> v2 = boost::assign::list_of("äü^ä+#ül-");
61         std::vector<String> v3 = boost::assign::list_of("1")("2");
62         m.insert(std::pair<String, std::vector<String> >("shout", v1));
63         m.insert(std::pair<String, std::vector<String> >("sonderzeichen", v2));
64         url->SetQuery(m);
65         url->SetQueryElements("count", v3);
66         url->AddQueryElement("count", "3");
67
68         std::map<String, std::vector<String> > mn = url->GetQuery();
69         BOOST_CHECK(mn["shout"][0] == v1[0]);
70         BOOST_CHECK(mn["sonderzeichen"][0] == v2[0]);
71         BOOST_CHECK(mn["count"][2] == "3");
72 }
73
74 BOOST_AUTO_TEST_CASE(parameters)
75 {
76         Url::Ptr url = new Url("https://icinga.org/hya/?rain=karl&rair=robert&foo[]=bar");
77
78         BOOST_CHECK(url->GetQueryElement("rair") == "robert");
79         BOOST_CHECK(url->GetQueryElement("rain") == "karl");
80         std::vector<String> test = url->GetQueryElements("foo");
81         BOOST_CHECK(test.size() == 1);
82         BOOST_CHECK(test[0] == "bar");
83 }
84
85 BOOST_AUTO_TEST_CASE(format)
86 {
87         Url::Ptr url = new Url("http://foo.bar/baz/?hop=top&flop=sop#iLIKEtrains");
88         BOOST_CHECK(new Url(url->Format()));
89
90         url = new Url("//main.args/////////?k[]=one&k[]=two#three");
91         BOOST_CHECK(new Url(url->Format()));
92
93         url = new Url("/foo/bar/index.php?blaka");
94         BOOST_CHECK(new Url(url->Format()));
95
96         url = new Url("/");
97         BOOST_CHECK(url->Format() == "/");
98 }
99
100 BOOST_AUTO_TEST_CASE(illegal_legal_strings)
101 {
102         BOOST_CHECK(new Url("/?foo=barr&foo[]=bazz"));
103         BOOST_CHECK_THROW(new Url("/?]=gar"), std::invalid_argument);
104         BOOST_CHECK_THROW(new Url("/#?[]"), std::invalid_argument);
105         BOOST_CHECK(new Url("/?foo=bar&foo=ba"));
106         BOOST_CHECK_THROW(new Url("/?foo=bar&[]=d"), std::invalid_argument);
107         BOOST_CHECK(new Url("/?fo=&bar=garOA"));
108         BOOST_CHECK(new Url("https://127.0.0.1:5665/demo?type=Service&filter=service.state%3E0"));
109         BOOST_CHECK(new Url("/?foo=baz??&\?\?=/?"));
110         BOOST_CHECK(new Url("/"));
111         BOOST_CHECK(new Url("///////"));
112         BOOST_CHECK(new Url("/??[]=?#?=?"));
113         BOOST_CHECK(new Url("http://foo/#bar"));
114         BOOST_CHECK(new Url("//foo/"));
115 }
116
117 BOOST_AUTO_TEST_SUITE_END()