From: Michael Friedrich Date: Fri, 6 Apr 2018 12:49:15 +0000 (+0200) Subject: Allow to disable brackets for the Url class and Format() X-Git-Tag: v2.8.3~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9f418f535624eedbe4f22c2b2a833078062fc745;p=icinga2 Allow to disable brackets for the Url class and Format() This commit also adds unit tests. refs #5706 refs #6207 --- diff --git a/lib/remote/url.cpp b/lib/remote/url.cpp index 16a0dbd88..03e8d5e6a 100644 --- a/lib/remote/url.cpp +++ b/lib/remote/url.cpp @@ -208,6 +208,11 @@ void Url::SetQuery(const std::map >& 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); @@ -276,8 +281,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); diff --git a/lib/remote/url.hpp b/lib/remote/url.hpp index 43b169fc2..5c145ae78 100644 --- a/lib/remote/url.hpp +++ b/lib/remote/url.hpp @@ -65,6 +65,7 @@ public: void SetPort(const String& port); void SetPath(const std::vector& path); void SetQuery(const std::map >& query); + void SetArrayFormatUseBrackets(bool useBrackets = true); void AddQueryElement(const String& name, const String& query); void SetQueryElements(const String& name, const std::vector& query); @@ -78,6 +79,7 @@ private: String m_Port; std::vector m_Path; std::map > m_Query; + bool m_ArrayFormatUseBrackets; String m_Fragment; bool ParseScheme(const String& scheme); diff --git a/test/remote-url.cpp b/test/remote-url.cpp index fce18d7ed..645b858c0 100644 --- a/test/remote-url.cpp +++ b/test/remote-url.cpp @@ -96,6 +96,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)