diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index 58c87123a..0e476de54 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -18,6 +18,7 @@ #include "catch2/catch.hpp" #include "zm_utils.h" +#include TEST_CASE("trimSet") { REQUIRE(trimSet("", "") == ""); @@ -159,3 +160,81 @@ TEST_CASE("UriDecode") { REQUIRE(UriDecode("abcABC123-_.~%21%28%29%26%3d%20") == "abcABC123-_.~!()&= "); REQUIRE(UriDecode("abcABC123-_.~%21%28%29%26%3d+") == "abcABC123-_.~!()&= "); } + +TEST_CASE("QueryString") { + SECTION("no value") { + std::stringstream str("name1="); + QueryString qs(str); + + REQUIRE(qs.size() == 1); + REQUIRE(qs.has("name1") == true); + + const QueryParameter *p = qs.get("name1"); + REQUIRE(p != nullptr); + REQUIRE(p->name() == "name1"); + REQUIRE(p->size() == 0); + } + + SECTION("no value and ampersand") { + std::stringstream str("name1=&"); + QueryString qs(str); + + REQUIRE(qs.size() == 1); + REQUIRE(qs.has("name1") == true); + + const QueryParameter *p = qs.get("name1"); + REQUIRE(p != nullptr); + REQUIRE(p->name() == "name1"); + REQUIRE(p->size() == 0); + } + + SECTION("one parameter, one value") { + std::stringstream str("name1=value1"); + QueryString qs(str); + + REQUIRE(qs.size() == 1); + REQUIRE(qs.has("name1") == true); + + const QueryParameter *p = qs.get("name1"); + REQUIRE(p != nullptr); + REQUIRE(p->name() == "name1"); + REQUIRE(p->size() == 1); + REQUIRE(p->values()[0] == "value1"); + } + + SECTION("one parameter, multiple values") { + std::stringstream str("name1=value1&name1=value2"); + QueryString qs(str); + + REQUIRE(qs.size() == 1); + REQUIRE(qs.has("name1") == true); + + const QueryParameter *p = qs.get("name1"); + REQUIRE(p != nullptr); + REQUIRE(p->name() == "name1"); + REQUIRE(p->size() == 2); + REQUIRE(p->values()[0] == "value1"); + REQUIRE(p->values()[1] == "value2"); + } + + SECTION("multiple parameters, multiple values") { + std::stringstream str("name1=value1&name2=value2"); + QueryString qs(str); + + REQUIRE(qs.size() == 2); + REQUIRE(qs.has("name1") == true); + REQUIRE(qs.has("name2") == true); + + const QueryParameter *p1 = qs.get("name1"); + REQUIRE(p1 != nullptr); + REQUIRE(p1->name() == "name1"); + REQUIRE(p1->size() == 1); + REQUIRE(p1->values()[0] == "value1"); + + const QueryParameter *p2 = qs.get("name2"); + REQUIRE(p2 != nullptr); + REQUIRE(p2->name() == "name2"); + REQUIRE(p2->size() == 1); + REQUIRE(p2->values()[0] == "value2"); + } +}