tests/utils: Add tests for QueryString

This commit is contained in:
Peter Keresztes Schmidt 2021-04-03 22:29:31 +02:00
parent fa7c420442
commit 3968a411a0
1 changed files with 79 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include "catch2/catch.hpp" #include "catch2/catch.hpp"
#include "zm_utils.h" #include "zm_utils.h"
#include <sstream>
TEST_CASE("trimSet") { TEST_CASE("trimSet") {
REQUIRE(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%20") == "abcABC123-_.~!()&= ");
REQUIRE(UriDecode("abcABC123-_.~%21%28%29%26%3d+") == "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");
}
}