]> granicus.if.org Git - icinga2/commitdiff
Allow to disable brackets for the Url class and Format()
authorMichael Friedrich <michael.friedrich@icinga.com>
Fri, 6 Apr 2018 12:49:15 +0000 (14:49 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Fri, 6 Apr 2018 13:22:17 +0000 (15:22 +0200)
This commit also adds unit tests.

refs #5706

lib/remote/url.cpp
lib/remote/url.hpp
test/remote-url.cpp

index 262de301b97a84b13a7dedbfaa3fa634dac8908b..f6c84ece15144d0af49acc338ed58a1482052080 100644 (file)
@@ -205,6 +205,11 @@ void Url::SetQuery(const std::map<String, std::vector<String> >& query)
        m_Query = query;
 }
 
+void Url::SetArrayFormatUseBrackets(bool useBrackets)
+{
+       m_ArrayFormatUseBrackets = useBrackets;
+}
+
 void Url::AddQueryElement(const String& name, const String& value)
 {
        auto it = m_Query.find(name);
@@ -272,8 +277,11 @@ String Url::Format(bool onlyPathAndQuery, bool printCredentials) const
                                        temp += "&";
 
                                temp += key;
-                               if (kv.second.size() > 1)
-                                       temp += "[]";
+
+                               if (m_ArrayFormatUseBrackets) {
+                                       if (kv.second.size() > 1)
+                                               temp += "[]";
+                               }
 
                                if (!s.IsEmpty())
                                        temp += "=" + Utility::EscapeString(s, ACQUERY_ENCODE, false);
index ac022f4276bc7e89c9d0e175e44930996445264f..20386980c3bc20f1a7452f7066d3b5c510c24049 100644 (file)
@@ -65,6 +65,7 @@ public:
        void SetPort(const String& port);
        void SetPath(const std::vector<String>& path);
        void SetQuery(const std::map<String, std::vector<String> >& query);
+       void SetArrayFormatUseBrackets(bool useBrackets = true);
 
        void AddQueryElement(const String& name, const String& query);
        void SetQueryElements(const String& name, const std::vector<String>& query);
@@ -78,6 +79,7 @@ private:
        String m_Port;
        std::vector<String> m_Path;
        std::map<String, std::vector<String> > m_Query;
+       bool m_ArrayFormatUseBrackets;
        String m_Fragment;
 
        bool ParseScheme(const String& scheme);
index d70751872345a9baae9e66d185ea6e779dfaae29..60df7c13056bee4633e3b286dab4b1cd19fdcc51 100644 (file)
@@ -95,6 +95,14 @@ BOOST_AUTO_TEST_CASE(format)
 
        url = new Url("/");
        BOOST_CHECK(url->Format(false, false) == "/");
+
+       url = new Url("https://nsclient:8443/query/check_cpu?time%5B%5D=1m&time=5m&time%5B%5D=15m");
+       url->SetArrayFormatUseBrackets(false);
+       BOOST_CHECK(url2 = new Url(url->Format(false, false)));
+
+       url = new Url("https://icinga2/query?a[]=1&a[]=2&a[]=3");
+       url->SetArrayFormatUseBrackets(true);
+       BOOST_CHECK(url2 = new Url(url->Format(false, false)));
 }
 
 BOOST_AUTO_TEST_CASE(illegal_legal_strings)