]> granicus.if.org Git - icinga2/blob - test/remote-url.cpp
Url#m_Query: preserve order
[icinga2] / test / remote-url.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
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 <BoostTestTargetConfig.h>
23
24 using namespace icinga;
25
26 BOOST_AUTO_TEST_SUITE(remote_url)
27
28 BOOST_AUTO_TEST_CASE(id_and_path)
29 {
30         Url::Ptr url = new Url("http://icinga.com/foo/bar/baz?hurr=durr");
31
32         BOOST_CHECK(url->GetScheme() == "http");
33
34         BOOST_CHECK(url->GetAuthority() == "icinga.com");
35
36         std::vector<String> PathCorrect;
37         PathCorrect.emplace_back("foo");
38         PathCorrect.emplace_back("bar");
39         PathCorrect.emplace_back("baz");
40
41         BOOST_CHECK(url->GetPath() == PathCorrect);
42 }
43
44 BOOST_AUTO_TEST_CASE(get_and_set)
45 {
46         Url::Ptr url = new Url();
47         url->SetScheme("ftp");
48         url->SetUsername("Horst");
49         url->SetPassword("Seehofer");
50         url->SetHost("koenigreich.bayern");
51         url->SetPort("1918");
52         url->SetPath({ "path", "to", "münchen" });
53
54         BOOST_CHECK(url->Format(false, true) == "ftp://Horst:Seehofer@koenigreich.bayern:1918/path/to/m%C3%BCnchen");
55
56         url->SetQuery({
57                 {"shout", "hip"},
58                 {"shout", "hip"},
59                 {"shout", "hurra"},
60                 {"sonderzeichen", "äü^ä+#ül-"}
61         });
62         url->AddQueryElement("count", "3");
63
64         auto mn (url->GetQuery());
65
66         BOOST_CHECK(mn.size() == 5);
67
68         BOOST_CHECK(mn[0].first == "shout");
69         BOOST_CHECK(mn[0].second == "hip");
70
71         BOOST_CHECK(mn[1].first == "shout");
72         BOOST_CHECK(mn[1].second == "hip");
73
74         BOOST_CHECK(mn[2].first == "shout");
75         BOOST_CHECK(mn[2].second == "hurra");
76
77         BOOST_CHECK(mn[3].first == "sonderzeichen");
78         BOOST_CHECK(mn[3].second == "äü^ä+#ül-");
79
80         BOOST_CHECK(mn[4].first == "count");
81         BOOST_CHECK(mn[4].second == "3");
82 }
83
84 BOOST_AUTO_TEST_CASE(parameters)
85 {
86         Url::Ptr url = new Url("https://icinga.com/hya/?rain=karl&rair=robert&foo[]=bar");
87
88         auto query (url->GetQuery());
89
90         BOOST_CHECK(query.size() == 3);
91
92         BOOST_CHECK(query[0].first == "rain");
93         BOOST_CHECK(query[0].second == "karl");
94
95         BOOST_CHECK(query[1].first == "rair");
96         BOOST_CHECK(query[1].second == "robert");
97
98         BOOST_CHECK(query[2].first == "foo");
99         BOOST_CHECK(query[2].second == "bar");
100 }
101
102 BOOST_AUTO_TEST_CASE(format)
103 {
104         Url::Ptr url = new Url("http://foo.bar/baz/?hop=top&flop=sop#iLIKEtrains");
105         Url::Ptr url2;
106         BOOST_CHECK(url2 = new Url(url->Format(false, false)));
107
108         url = new Url("//main.args/////////?k[]=one&k[]=two#three");
109         BOOST_CHECK(url2 = new Url(url->Format(false, false)));
110
111         url = new Url("/foo/bar/index.php?blaka");
112         BOOST_CHECK(url2 = new Url(url->Format(false, false)));
113         BOOST_CHECK(url->Format(false, false) == "/foo/bar/index.php?blaka");
114
115         url = new Url("/");
116         BOOST_CHECK(url->Format(false, false) == "/");
117
118         url = new Url("https://nsclient:8443/query/check_cpu?time%5B%5D=1m&time=5m&time%5B%5D=15m");
119         url->SetArrayFormatUseBrackets(false);
120         BOOST_CHECK(url2 = new Url(url->Format(false, false)));
121
122         url = new Url("https://icinga2/query?a[]=1&a[]=2&a[]=3");
123         url->SetArrayFormatUseBrackets(true);
124         BOOST_CHECK(url2 = new Url(url->Format(false, false)));
125 }
126
127 BOOST_AUTO_TEST_CASE(illegal_legal_strings)
128 {
129         Url::Ptr url;
130         BOOST_CHECK(url = new Url("/?foo=barr&foo[]=bazz"));
131         BOOST_CHECK_THROW(url = new Url("/?]=gar"), std::invalid_argument);
132         BOOST_CHECK_THROW(url = new Url("/#?[]"), std::invalid_argument);
133         BOOST_CHECK(url = new Url("/?foo=bar&foo=ba"));
134         BOOST_CHECK_THROW(url = new Url("/?foo=bar&[]=d"), std::invalid_argument);
135         BOOST_CHECK(url = new Url("/?fo=&bar=garOA"));
136         BOOST_CHECK(url = new Url("https://127.0.0.1:5665/demo?type=Service&filter=service.state%3E0"));
137         BOOST_CHECK(url = new Url("/?foo=baz??&\?\?=/?"));
138         BOOST_CHECK(url = new Url("/"));
139         BOOST_CHECK(url = new Url("///////"));
140         BOOST_CHECK(url = new Url("/??[]=?#?=?"));
141         BOOST_CHECK(url = new Url("http://foo/#bar"));
142         BOOST_CHECK(url = new Url("//foo/"));
143 }
144
145 BOOST_AUTO_TEST_SUITE_END()